9
9
ViewChild ,
10
10
ViewChildren ,
11
11
ViewEncapsulation ,
12
+ ChangeDetectionStrategy ,
12
13
} from '@angular/core' ;
13
14
import { TestBed , ComponentFixture , fakeAsync , flush , tick } from '@angular/core/testing' ;
14
15
import { DragDropModule } from './drag-drop-module' ;
@@ -533,6 +534,27 @@ describe('CdkDrag', () => {
533
534
expect ( dropZone . element . nativeElement . classList ) . not . toContain ( 'cdk-drop-dragging' ) ;
534
535
} ) ) ;
535
536
537
+ it ( 'should toggle a class when the user starts dragging an item with OnPush change detection' ,
538
+ fakeAsync ( ( ) => {
539
+ const fixture = createComponent ( DraggableInOnPushDropZone ) ;
540
+ fixture . detectChanges ( ) ;
541
+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
542
+ const dropZone = fixture . componentInstance . dropInstance ;
543
+
544
+ expect ( dropZone . element . nativeElement . classList ) . not . toContain ( 'cdk-drop-list-dragging' ) ;
545
+
546
+ startDraggingViaMouse ( fixture , item ) ;
547
+
548
+ expect ( dropZone . element . nativeElement . classList ) . toContain ( 'cdk-drop-list-dragging' ) ;
549
+
550
+ dispatchMouseEvent ( document , 'mouseup' ) ;
551
+ fixture . detectChanges ( ) ;
552
+ flush ( ) ;
553
+ fixture . detectChanges ( ) ;
554
+
555
+ expect ( dropZone . element . nativeElement . classList ) . not . toContain ( 'cdk-drop-dragging' ) ;
556
+ } ) ) ;
557
+
536
558
it ( 'should not toggle dragging class if the element was not dragged more than the threshold' ,
537
559
fakeAsync ( ( ) => {
538
560
const fixture = createComponent ( DraggableInDropZone , [ ] , 5 ) ;
@@ -1807,22 +1829,22 @@ class StandaloneDraggableWithMultipleHandles {
1807
1829
@ViewChildren ( CdkDragHandle ) handles : QueryList < CdkDragHandle > ;
1808
1830
}
1809
1831
1810
- @ Component ( {
1811
- template : `
1812
- <div
1813
- cdkDropList
1814
- style="width: 100px; background: pink; "
1815
- [id ]="dropZoneId "
1816
- [cdkDropListData]="items"
1817
- (cdkDropListDropped)="droppedSpy($event)">
1818
- <div
1819
- *ngFor="let item of items"
1820
- cdkDrag
1821
- [cdkDragData]=" item"
1822
- style="width: 100%; height: ${ ITEM_HEIGHT } px; background: red;">{{item}} </div>
1823
- </div>
1824
- `
1825
- } )
1832
+ const DROP_ZONE_FIXTURE_TEMPLATE = `
1833
+ <div
1834
+ cdkDropList
1835
+ style="width: 100px; background: pink;"
1836
+ [id]="dropZoneId "
1837
+ [cdkDropListData ]="items "
1838
+ (cdkDropListDropped)="droppedSpy($event)">
1839
+ <div
1840
+ *ngFor="let item of items"
1841
+ cdkDrag
1842
+ [cdkDragData]="item"
1843
+ style="width: 100%; height: ${ ITEM_HEIGHT } px; background: red;">{{ item}}</div>
1844
+ </div>
1845
+ ` ;
1846
+
1847
+ @ Component ( { template : DROP_ZONE_FIXTURE_TEMPLATE } )
1826
1848
class DraggableInDropZone {
1827
1849
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
1828
1850
@ViewChild ( CdkDropList ) dropInstance : CdkDropList ;
@@ -1833,6 +1855,12 @@ class DraggableInDropZone {
1833
1855
} ) ;
1834
1856
}
1835
1857
1858
+ @Component ( {
1859
+ template : DROP_ZONE_FIXTURE_TEMPLATE ,
1860
+ changeDetection : ChangeDetectionStrategy . OnPush ,
1861
+ } )
1862
+ class DraggableInOnPushDropZone extends DraggableInDropZone { }
1863
+
1836
1864
1837
1865
@Component ( {
1838
1866
encapsulation : ViewEncapsulation . None ,
0 commit comments