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' ;
@@ -547,6 +548,27 @@ describe('CdkDrag', () => {
547
548
expect ( dropZone . element . nativeElement . classList ) . not . toContain ( 'cdk-drop-dragging' ) ;
548
549
} ) ) ;
549
550
551
+ it ( 'should toggle a class when the user starts dragging an item with OnPush change detection' ,
552
+ fakeAsync ( ( ) => {
553
+ const fixture = createComponent ( DraggableInOnPushDropZone ) ;
554
+ fixture . detectChanges ( ) ;
555
+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
556
+ const dropZone = fixture . componentInstance . dropInstance ;
557
+
558
+ expect ( dropZone . element . nativeElement . classList ) . not . toContain ( 'cdk-drop-list-dragging' ) ;
559
+
560
+ startDraggingViaMouse ( fixture , item ) ;
561
+
562
+ expect ( dropZone . element . nativeElement . classList ) . toContain ( 'cdk-drop-list-dragging' ) ;
563
+
564
+ dispatchMouseEvent ( document , 'mouseup' ) ;
565
+ fixture . detectChanges ( ) ;
566
+ flush ( ) ;
567
+ fixture . detectChanges ( ) ;
568
+
569
+ expect ( dropZone . element . nativeElement . classList ) . not . toContain ( 'cdk-drop-dragging' ) ;
570
+ } ) ) ;
571
+
550
572
it ( 'should not toggle dragging class if the element was not dragged more than the threshold' ,
551
573
fakeAsync ( ( ) => {
552
574
const fixture = createComponent ( DraggableInDropZone , [ ] , 5 ) ;
@@ -1991,24 +2013,24 @@ class StandaloneDraggableWithMultipleHandles {
1991
2013
@ViewChildren ( CdkDragHandle ) handles : QueryList < CdkDragHandle > ;
1992
2014
}
1993
2015
1994
- @Component ( {
1995
- template : `
2016
+ const DROP_ZONE_FIXTURE_TEMPLATE = `
2017
+ <div
2018
+ cdkDropList
2019
+ style="width: 100px; background: pink;"
2020
+ [id]="dropZoneId"
2021
+ [cdkDropListData]="items"
2022
+ (cdkDropListDropped)="droppedSpy($event)">
1996
2023
<div
1997
- cdkDropList
1998
- style="width: 100px; background: pink;"
1999
- [id]="dropZoneId"
2000
- [cdkDropListData]="items"
2001
- (cdkDropListDropped)="droppedSpy($event)">
2002
- <div
2003
- *ngFor="let item of items"
2004
- cdkDrag
2005
- [cdkDragData]="item"
2006
- [style.height.px]="item.height"
2007
- [style.margin-bottom.px]="item.margin"
2008
- style="width: 100%; background: red;">{{item.value}}</div>
2009
- </div>
2010
- `
2011
- } )
2024
+ *ngFor="let item of items"
2025
+ cdkDrag
2026
+ [cdkDragData]="item"
2027
+ [style.height.px]="item.height"
2028
+ [style.margin-bottom.px]="item.margin"
2029
+ style="width: 100%; background: red;">{{item.value}}</div>
2030
+ </div>
2031
+ ` ;
2032
+
2033
+ @Component ( { template : DROP_ZONE_FIXTURE_TEMPLATE } )
2012
2034
class DraggableInDropZone {
2013
2035
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
2014
2036
@ViewChild ( CdkDropList ) dropInstance : CdkDropList ;
@@ -2024,6 +2046,12 @@ class DraggableInDropZone {
2024
2046
} ) ;
2025
2047
}
2026
2048
2049
+ @Component ( {
2050
+ template : DROP_ZONE_FIXTURE_TEMPLATE ,
2051
+ changeDetection : ChangeDetectionStrategy . OnPush ,
2052
+ } )
2053
+ class DraggableInOnPushDropZone extends DraggableInDropZone { }
2054
+
2027
2055
2028
2056
@Component ( {
2029
2057
encapsulation : ViewEncapsulation . None ,
0 commit comments