99 ViewChild ,
1010 ViewChildren ,
1111 ViewEncapsulation ,
12+ ChangeDetectionStrategy ,
1213} from '@angular/core' ;
1314import { TestBed , ComponentFixture , fakeAsync , flush , tick } from '@angular/core/testing' ;
1415import { DragDropModule } from './drag-drop-module' ;
@@ -547,6 +548,27 @@ describe('CdkDrag', () => {
547548 expect ( dropZone . element . nativeElement . classList ) . not . toContain ( 'cdk-drop-dragging' ) ;
548549 } ) ) ;
549550
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+
550572 it ( 'should not toggle dragging class if the element was not dragged more than the threshold' ,
551573 fakeAsync ( ( ) => {
552574 const fixture = createComponent ( DraggableInDropZone , [ ] , 5 ) ;
@@ -1991,24 +2013,24 @@ class StandaloneDraggableWithMultipleHandles {
19912013 @ViewChildren ( CdkDragHandle ) handles : QueryList < CdkDragHandle > ;
19922014}
19932015
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)">
19962023 <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 } )
20122034class DraggableInDropZone {
20132035 @ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
20142036 @ViewChild ( CdkDropList ) dropInstance : CdkDropList ;
@@ -2024,6 +2046,12 @@ class DraggableInDropZone {
20242046 } ) ;
20252047}
20262048
2049+ @Component ( {
2050+ template : DROP_ZONE_FIXTURE_TEMPLATE ,
2051+ changeDetection : ChangeDetectionStrategy . OnPush ,
2052+ } )
2053+ class DraggableInOnPushDropZone extends DraggableInDropZone { }
2054+
20272055
20282056@Component ( {
20292057 encapsulation : ViewEncapsulation . None ,
0 commit comments