@@ -340,6 +340,29 @@ describe('CdkDrag', () => {
340
340
. toEqual ( [ 'One' , 'Two' , 'Zero' , 'Three' ] ) ;
341
341
} ) ) ;
342
342
343
+ it ( 'should not move the original element from its initial DOM position' , fakeAsync ( ( ) => {
344
+ const fixture = createComponent ( DraggableInDropZone ) ;
345
+ fixture . detectChanges ( ) ;
346
+ const root = fixture . nativeElement as HTMLElement ;
347
+ let dragElements = Array . from ( root . querySelectorAll ( '.cdk-drag' ) ) ;
348
+
349
+ expect ( dragElements . map ( el => el . textContent ) ) . toEqual ( [ 'Zero' , 'One' , 'Two' , 'Three' ] ) ;
350
+
351
+ // Stub out the original call so the list doesn't get re-rendered.
352
+ // We're testing the DOM order explicitly.
353
+ fixture . componentInstance . droppedSpy . and . callFake ( ( ) => { } ) ;
354
+
355
+ const thirdItemRect = dragElements [ 2 ] . getBoundingClientRect ( ) ;
356
+
357
+ dragElementViaMouse ( fixture , fixture . componentInstance . dragItems . first . element . nativeElement ,
358
+ thirdItemRect . left + 1 , thirdItemRect . top + 1 ) ;
359
+ flush ( ) ;
360
+ fixture . detectChanges ( ) ;
361
+
362
+ dragElements = Array . from ( root . querySelectorAll ( '.cdk-drag' ) ) ;
363
+ expect ( dragElements . map ( el => el . textContent ) ) . toEqual ( [ 'Zero' , 'One' , 'Two' , 'Three' ] ) ;
364
+ } ) ) ;
365
+
343
366
it ( 'should dispatch the `dropped` event in a horizontal drop zone' , fakeAsync ( ( ) => {
344
367
const fixture = createComponent ( DraggableInHorizontalDropZone ) ;
345
368
fixture . detectChanges ( ) ;
@@ -677,14 +700,14 @@ describe('CdkDrag', () => {
677
700
const initialRect = item . element . nativeElement . getBoundingClientRect ( ) ;
678
701
const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
679
702
680
- expect ( dropZones [ 0 ] . contains ( item . element . nativeElement ) )
681
- . toBe ( true , 'Expected placeholder to be inside the first container.' ) ;
682
703
dispatchMouseEvent ( item . element . nativeElement , 'mousedown' ) ;
683
704
fixture . detectChanges ( ) ;
684
705
685
706
const placeholder = dropZones [ 0 ] . querySelector ( '.cdk-drag-placeholder' ) ! ;
686
707
687
708
expect ( placeholder ) . toBeTruthy ( ) ;
709
+ expect ( dropZones [ 0 ] . contains ( placeholder ) )
710
+ . toBe ( true , 'Expected placeholder to be inside the first container.' ) ;
688
711
689
712
dispatchMouseEvent ( document , 'mousemove' , targetRect . left + 1 , targetRect . top + 1 ) ;
690
713
fixture . detectChanges ( ) ;
@@ -708,18 +731,25 @@ describe('CdkDrag', () => {
708
731
const fixture = createComponent ( ConnectedDropZones ) ;
709
732
fixture . detectChanges ( ) ;
710
733
711
- const groups = fixture . componentInstance . groupedDragItems ;
734
+ const groups = fixture . componentInstance . groupedDragItems . slice ( ) ;
712
735
const element = groups [ 0 ] [ 1 ] . element . nativeElement ;
713
- const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
736
+ const dropInstances = fixture . componentInstance . dropInstances . toArray ( ) ;
714
737
const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
715
738
716
- expect ( dropZones [ 0 ] . contains ( element ) ) . toBe ( true ) ;
717
-
718
739
dragElementViaMouse ( fixture , element , targetRect . left + 1 , targetRect . top + 1 ) ;
719
740
flush ( ) ;
720
741
fixture . detectChanges ( ) ;
721
742
722
- expect ( dropZones [ 1 ] . contains ( element ) ) . toBe ( true ) ;
743
+ const event = fixture . componentInstance . droppedSpy . calls . mostRecent ( ) . args [ 0 ] ;
744
+
745
+ expect ( event ) . toBeTruthy ( ) ;
746
+ expect ( event ) . toEqual ( {
747
+ previousIndex : 1 ,
748
+ currentIndex : 3 ,
749
+ item : groups [ 0 ] [ 1 ] ,
750
+ container : dropInstances [ 1 ] ,
751
+ previousContainer : dropInstances [ 0 ]
752
+ } ) ;
723
753
} ) ) ;
724
754
725
755
it ( 'should not be able to transfer an item into a container that is not in `connectedTo`' ,
@@ -730,18 +760,25 @@ describe('CdkDrag', () => {
730
760
fixture . componentInstance . dropInstances . forEach ( d => d . connectedTo = [ ] ) ;
731
761
fixture . detectChanges ( ) ;
732
762
733
- const groups = fixture . componentInstance . groupedDragItems ;
763
+ const groups = fixture . componentInstance . groupedDragItems . slice ( ) ;
734
764
const element = groups [ 0 ] [ 1 ] . element . nativeElement ;
735
- const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
765
+ const dropInstances = fixture . componentInstance . dropInstances . toArray ( ) ;
736
766
const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
737
767
738
- expect ( dropZones [ 0 ] . contains ( element ) ) . toBe ( true ) ;
739
-
740
768
dragElementViaMouse ( fixture , element , targetRect . left + 1 , targetRect . top + 1 ) ;
741
769
flush ( ) ;
742
770
fixture . detectChanges ( ) ;
743
771
744
- expect ( dropZones [ 0 ] . contains ( element ) ) . toBe ( true ) ;
772
+ const event = fixture . componentInstance . droppedSpy . calls . mostRecent ( ) . args [ 0 ] ;
773
+
774
+ expect ( event ) . toBeTruthy ( ) ;
775
+ expect ( event ) . toEqual ( {
776
+ previousIndex : 1 ,
777
+ currentIndex : 1 ,
778
+ item : groups [ 0 ] [ 1 ] ,
779
+ container : dropInstances [ 0 ] ,
780
+ previousContainer : dropInstances [ 0 ]
781
+ } ) ;
745
782
} ) ) ;
746
783
747
784
@@ -751,20 +788,17 @@ describe('CdkDrag', () => {
751
788
752
789
const groups = fixture . componentInstance . groupedDragItems ;
753
790
const element = groups [ 0 ] [ 1 ] . element . nativeElement ;
754
- const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
755
- const targetRect = dropZones [ 1 ] . getBoundingClientRect ( ) ;
756
-
757
- expect ( dropZones [ 0 ] . contains ( element ) ) . toBe ( true ) ;
791
+ const dropZone = fixture . componentInstance . dropInstances . toArray ( ) [ 1 ] . element . nativeElement ;
792
+ const targetRect = dropZone . getBoundingClientRect ( ) ;
758
793
759
794
// Drag the element into the drop zone and move it to the top.
760
795
[ 1 , - 1 ] . forEach ( offset => {
761
796
dragElementViaMouse ( fixture , element , targetRect . left + offset , targetRect . top + offset ) ;
762
797
flush ( ) ;
763
798
fixture . detectChanges ( ) ;
764
- expect ( dropZones [ 1 ] . contains ( element ) ) . toBe ( true ) ;
765
799
} ) ;
766
800
767
- assertDownwardSorting ( fixture , Array . from ( dropZones [ 1 ] . querySelectorAll ( '.cdk-drag' ) ) ) ;
801
+ assertDownwardSorting ( fixture , Array . from ( dropZone . querySelectorAll ( '.cdk-drag' ) ) ) ;
768
802
} ) ) ;
769
803
770
804
it ( 'should be able to return the last item inside its initial container' , fakeAsync ( ( ) => {
@@ -780,15 +814,16 @@ describe('CdkDrag', () => {
780
814
const initialRect = item . element . nativeElement . getBoundingClientRect ( ) ;
781
815
const targetRect = groups [ 1 ] [ 0 ] . element . nativeElement . getBoundingClientRect ( ) ;
782
816
783
- expect ( dropZones [ 0 ] . contains ( item . element . nativeElement ) )
784
- . toBe ( true , 'Expected placeholder to be inside the first container.' ) ;
785
817
dispatchMouseEvent ( item . element . nativeElement , 'mousedown' ) ;
786
818
fixture . detectChanges ( ) ;
787
819
788
820
const placeholder = dropZones [ 0 ] . querySelector ( '.cdk-drag-placeholder' ) ! ;
789
821
790
822
expect ( placeholder ) . toBeTruthy ( ) ;
791
823
824
+ expect ( dropZones [ 0 ] . contains ( placeholder ) )
825
+ . toBe ( true , 'Expected placeholder to be inside the first container.' ) ;
826
+
792
827
dispatchMouseEvent ( document , 'mousemove' , targetRect . left + 1 , targetRect . top + 1 ) ;
793
828
fixture . detectChanges ( ) ;
794
829
@@ -820,25 +855,32 @@ describe('CdkDrag', () => {
820
855
const fixture = createComponent ( ConnectedDropZones ) ;
821
856
fixture . detectChanges ( ) ;
822
857
823
- const dropZones = fixture . componentInstance . dropInstances . toArray ( ) ;
858
+ const dropInstances = fixture . componentInstance . dropInstances . toArray ( ) ;
824
859
825
- dropZones [ 0 ] . id = 'todo' ;
826
- dropZones [ 1 ] . id = 'done' ;
827
- dropZones [ 0 ] . connectedTo = [ 'done' ] ;
828
- dropZones [ 1 ] . connectedTo = [ 'todo' ] ;
860
+ dropInstances [ 0 ] . id = 'todo' ;
861
+ dropInstances [ 1 ] . id = 'done' ;
862
+ dropInstances [ 0 ] . connectedTo = [ 'done' ] ;
863
+ dropInstances [ 1 ] . connectedTo = [ 'todo' ] ;
829
864
fixture . detectChanges ( ) ;
830
865
831
866
const groups = fixture . componentInstance . groupedDragItems ;
832
867
const element = groups [ 0 ] [ 1 ] . element . nativeElement ;
833
868
const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
834
869
835
- expect ( dropZones [ 0 ] . element . nativeElement . contains ( element ) ) . toBe ( true ) ;
836
-
837
870
dragElementViaMouse ( fixture , element , targetRect . left + 1 , targetRect . top + 1 ) ;
838
871
flush ( ) ;
839
872
fixture . detectChanges ( ) ;
840
873
841
- expect ( dropZones [ 1 ] . element . nativeElement . contains ( element ) ) . toBe ( true ) ;
874
+ const event = fixture . componentInstance . droppedSpy . calls . mostRecent ( ) . args [ 0 ] ;
875
+
876
+ expect ( event ) . toBeTruthy ( ) ;
877
+ expect ( event ) . toEqual ( {
878
+ previousIndex : 1 ,
879
+ currentIndex : 3 ,
880
+ item : groups [ 0 ] [ 1 ] ,
881
+ container : dropInstances [ 1 ] ,
882
+ previousContainer : dropInstances [ 0 ]
883
+ } ) ;
842
884
} ) ) ;
843
885
844
886
} ) ;
0 commit comments