@@ -15,7 +15,7 @@ import {dispatchMouseEvent, dispatchTouchEvent} from '@angular/cdk/testing';
15
15
import { Directionality } from '@angular/cdk/bidi' ;
16
16
import { CdkDrag } from './drag' ;
17
17
import { CdkDragDrop } from './drag-events' ;
18
- import { moveItemInArray , transferArrayItem } from './drag-utils' ;
18
+ import { moveItemInArray } from './drag-utils' ;
19
19
import { CdkDrop } from './drop' ;
20
20
import { CdkDragHandle } from './drag-handle' ;
21
21
@@ -460,7 +460,9 @@ describe('CdkDrag', () => {
460
460
it ( 'should move the placeholder as an item is being sorted down' , fakeAsync ( ( ) => {
461
461
const fixture = createComponent ( DraggableInDropZone ) ;
462
462
fixture . detectChanges ( ) ;
463
- assertDownwardSorting ( fixture ) ;
463
+ assertDownwardSorting ( fixture , fixture . componentInstance . dragItems . map ( item => {
464
+ return item . element . nativeElement ;
465
+ } ) ) ;
464
466
} ) ) ;
465
467
466
468
it ( 'should move the placeholder as an item is being sorted down on a scrolled page' ,
@@ -470,14 +472,18 @@ describe('CdkDrag', () => {
470
472
const cleanup = makePageScrollable ( ) ;
471
473
472
474
scrollTo ( 0 , 500 ) ;
473
- assertDownwardSorting ( fixture ) ;
475
+ assertDownwardSorting ( fixture , fixture . componentInstance . dragItems . map ( item => {
476
+ return item . element . nativeElement ;
477
+ } ) ) ;
474
478
cleanup ( ) ;
475
479
} ) ) ;
476
480
477
481
it ( 'should move the placeholder as an item is being sorted up' , fakeAsync ( ( ) => {
478
482
const fixture = createComponent ( DraggableInDropZone ) ;
479
483
fixture . detectChanges ( ) ;
480
- assertUpwardSorting ( fixture ) ;
484
+ assertUpwardSorting ( fixture , fixture . componentInstance . dragItems . map ( item => {
485
+ return item . element . nativeElement ;
486
+ } ) ) ;
481
487
} ) ) ;
482
488
483
489
it ( 'should move the placeholder as an item is being sorted up on a scrolled page' ,
@@ -487,7 +493,9 @@ describe('CdkDrag', () => {
487
493
const cleanup = makePageScrollable ( ) ;
488
494
489
495
scrollTo ( 0 , 500 ) ;
490
- assertUpwardSorting ( fixture ) ;
496
+ assertUpwardSorting ( fixture , fixture . componentInstance . dragItems . map ( item => {
497
+ return item . element . nativeElement ;
498
+ } ) ) ;
491
499
cleanup ( ) ;
492
500
} ) ) ;
493
501
@@ -617,9 +625,127 @@ describe('CdkDrag', () => {
617
625
const fixture = createComponent ( ConnectedDropZones ) ;
618
626
fixture . detectChanges ( ) ;
619
627
620
- // TODO
621
- // console.log(fixture.componentInstance.groupedDragItems.map(d => d.length));
628
+ const groups = fixture . componentInstance . groupedDragItems ;
629
+ const item = groups [ 0 ] [ 1 ] ;
630
+ const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
631
+
632
+ dragElementViaMouse ( fixture , item . element . nativeElement ,
633
+ targetRect . left + 1 , targetRect . top + 1 ) ;
634
+ flush ( ) ;
635
+ fixture . detectChanges ( ) ;
636
+
637
+ expect ( fixture . componentInstance . droppedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
638
+
639
+ const event = fixture . componentInstance . droppedSpy . calls . mostRecent ( ) . args [ 0 ] ;
640
+
641
+ expect ( event ) . toEqual ( {
642
+ previousIndex : 1 ,
643
+ currentIndex : 3 ,
644
+ item,
645
+ container : fixture . componentInstance . dropInstances . toArray ( ) [ 1 ] ,
646
+ previousContainer : fixture . componentInstance . dropInstances . first
647
+ } ) ;
622
648
} ) ) ;
649
+
650
+ it ( 'should be able to move the element over a new container and return it' , fakeAsync ( ( ) => {
651
+ const fixture = createComponent ( ConnectedDropZones ) ;
652
+ fixture . detectChanges ( ) ;
653
+
654
+ const groups = fixture . componentInstance . groupedDragItems ;
655
+ const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
656
+ const item = groups [ 0 ] [ 1 ] ;
657
+ const initialRect = item . element . nativeElement . getBoundingClientRect ( ) ;
658
+ const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
659
+
660
+ expect ( dropZones [ 0 ] . contains ( item . element . nativeElement ) ) . toBe ( true ) ;
661
+ dispatchMouseEvent ( item . element . nativeElement , 'mousedown' ) ;
662
+ fixture . detectChanges ( ) ;
663
+
664
+ const placeholder = dropZones [ 0 ] . querySelector ( '.cdk-drag-placeholder' ) ! ;
665
+
666
+ expect ( placeholder ) . toBeTruthy ( ) ;
667
+
668
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . left + 1 , targetRect . top + 1 ) ;
669
+ fixture . detectChanges ( ) ;
670
+
671
+ expect ( dropZones [ 1 ] . contains ( placeholder ) )
672
+ . toBe ( true , 'Expected placeholder to be inside second container.' ) ;
673
+
674
+ dispatchMouseEvent ( document , 'mousemove' , initialRect . left + 1 , initialRect . top + 1 ) ;
675
+ fixture . detectChanges ( ) ;
676
+
677
+ expect ( dropZones [ 0 ] . contains ( placeholder ) )
678
+ . toBe ( true , 'Expected placeholder to be inside first container.' ) ;
679
+
680
+ dispatchMouseEvent ( document , 'mouseup' ) ;
681
+ fixture . detectChanges ( ) ;
682
+
683
+ expect ( fixture . componentInstance . droppedSpy ) . not . toHaveBeenCalled ( ) ;
684
+ } ) ) ;
685
+
686
+ it ( 'should transfer the DOM element from one drop zone to another' , fakeAsync ( ( ) => {
687
+ const fixture = createComponent ( ConnectedDropZones ) ;
688
+ fixture . detectChanges ( ) ;
689
+
690
+ const groups = fixture . componentInstance . groupedDragItems ;
691
+ const element = groups [ 0 ] [ 1 ] . element . nativeElement ;
692
+ const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
693
+ const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
694
+
695
+ expect ( dropZones [ 0 ] . contains ( element ) ) . toBe ( true ) ;
696
+
697
+ dragElementViaMouse ( fixture , element , targetRect . left + 1 , targetRect . top + 1 ) ;
698
+ flush ( ) ;
699
+ fixture . detectChanges ( ) ;
700
+
701
+ expect ( dropZones [ 1 ] . contains ( element ) ) . toBe ( true ) ;
702
+ } ) ) ;
703
+
704
+ it ( 'should not be able to transfer an item into a container that is not in `connectedTo`' ,
705
+ fakeAsync ( ( ) => {
706
+ const fixture = createComponent ( ConnectedDropZones ) ;
707
+
708
+ fixture . detectChanges ( ) ;
709
+ fixture . componentInstance . dropInstances . forEach ( d => d . connectedTo = [ ] ) ;
710
+ fixture . detectChanges ( ) ;
711
+
712
+ const groups = fixture . componentInstance . groupedDragItems ;
713
+ const element = groups [ 0 ] [ 1 ] . element . nativeElement ;
714
+ const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
715
+ const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
716
+
717
+ expect ( dropZones [ 0 ] . contains ( element ) ) . toBe ( true ) ;
718
+
719
+ dragElementViaMouse ( fixture , element , targetRect . left + 1 , targetRect . top + 1 ) ;
720
+ flush ( ) ;
721
+ fixture . detectChanges ( ) ;
722
+
723
+ expect ( dropZones [ 0 ] . contains ( element ) ) . toBe ( true ) ;
724
+ } ) ) ;
725
+
726
+
727
+ it ( 'should be able to start dragging after an item has been transferred' , fakeAsync ( ( ) => {
728
+ const fixture = createComponent ( ConnectedDropZones ) ;
729
+ fixture . detectChanges ( ) ;
730
+
731
+ const groups = fixture . componentInstance . groupedDragItems ;
732
+ const element = groups [ 0 ] [ 1 ] . element . nativeElement ;
733
+ const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
734
+ const targetRect = dropZones [ 1 ] . getBoundingClientRect ( ) ;
735
+
736
+ expect ( dropZones [ 0 ] . contains ( element ) ) . toBe ( true ) ;
737
+
738
+ // Drag the element into the drop zone and move it to the top.
739
+ [ 1 , - 1 ] . forEach ( offset => {
740
+ dragElementViaMouse ( fixture , element , targetRect . left + offset , targetRect . top + offset ) ;
741
+ flush ( ) ;
742
+ fixture . detectChanges ( ) ;
743
+ expect ( dropZones [ 1 ] . contains ( element ) ) . toBe ( true ) ;
744
+ } ) ;
745
+
746
+ assertDownwardSorting ( fixture , Array . from ( dropZones [ 1 ] . querySelectorAll ( '.cdk-drag' ) ) ) ;
747
+ } ) ) ;
748
+
623
749
} ) ;
624
750
625
751
} ) ;
@@ -821,20 +947,9 @@ export class ConnectedDropZones implements AfterViewInit {
821
947
@ViewChildren ( CdkDrop ) dropInstances : QueryList < CdkDrop > ;
822
948
823
949
groupedDragItems : CdkDrag [ ] [ ] = [ ] ;
824
-
825
950
todo = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
826
951
done = [ 'Four' , 'Five' , 'Six' ] ;
827
-
828
- droppedSpy = jasmine . createSpy ( 'dropped spy' ) . and . callFake ( ( event : CdkDragDrop < string [ ] > ) => {
829
- if ( event . previousContainer === event . container ) {
830
- moveItemInArray ( event . container . data , event . previousIndex , event . currentIndex ) ;
831
- } else {
832
- transferArrayItem ( event . previousContainer . data ,
833
- event . container . data ,
834
- event . previousIndex ,
835
- event . currentIndex ) ;
836
- }
837
- } ) ;
952
+ droppedSpy = jasmine . createSpy ( 'dropped spy' ) ;
838
953
839
954
ngAfterViewInit ( ) {
840
955
this . dropInstances . forEach ( ( dropZone , index ) => {
@@ -913,10 +1028,10 @@ function makePageScrollable() {
913
1028
/**
914
1029
* Asserts that sorting an element down works correctly.
915
1030
* @param fixture Fixture against which to run the assertions.
1031
+ * @param items Array of items against which to test sorting.
916
1032
*/
917
- function assertDownwardSorting ( fixture : ComponentFixture < DraggableInDropZone > ) {
918
- const items = fixture . componentInstance . dragItems . toArray ( ) ;
919
- const draggedItem = items [ 0 ] . element . nativeElement ;
1033
+ function assertDownwardSorting ( fixture : ComponentFixture < any > , items : Element [ ] ) {
1034
+ const draggedItem = items [ 0 ] ;
920
1035
const { top, left} = draggedItem . getBoundingClientRect ( ) ;
921
1036
922
1037
dispatchMouseEvent ( draggedItem , 'mousedown' , left , top ) ;
@@ -926,7 +1041,7 @@ function assertDownwardSorting(fixture: ComponentFixture<DraggableInDropZone>) {
926
1041
927
1042
// Drag over each item one-by-one going downwards.
928
1043
for ( let i = 0 ; i < items . length ; i ++ ) {
929
- const elementRect = items [ i ] . element . nativeElement . getBoundingClientRect ( ) ;
1044
+ const elementRect = items [ i ] . getBoundingClientRect ( ) ;
930
1045
931
1046
// Add a few pixels to the top offset so we get some overlap.
932
1047
dispatchMouseEvent ( document , 'mousemove' , elementRect . left , elementRect . top + 5 ) ;
@@ -942,10 +1057,10 @@ function assertDownwardSorting(fixture: ComponentFixture<DraggableInDropZone>) {
942
1057
/**
943
1058
* Asserts that sorting an element up works correctly.
944
1059
* @param fixture Fixture against which to run the assertions.
1060
+ * @param items Array of items against which to test sorting.
945
1061
*/
946
- function assertUpwardSorting ( fixture : ComponentFixture < DraggableInDropZone > ) {
947
- const items = fixture . componentInstance . dragItems . toArray ( ) ;
948
- const draggedItem = items [ items . length - 1 ] . element . nativeElement ;
1062
+ function assertUpwardSorting ( fixture : ComponentFixture < any > , items : Element [ ] ) {
1063
+ const draggedItem = items [ items . length - 1 ] ;
949
1064
const { top, left} = draggedItem . getBoundingClientRect ( ) ;
950
1065
951
1066
dispatchMouseEvent ( draggedItem , 'mousedown' , left , top ) ;
@@ -955,7 +1070,7 @@ function assertUpwardSorting(fixture: ComponentFixture<DraggableInDropZone>) {
955
1070
956
1071
// Drag over each item one-by-one going upwards.
957
1072
for ( let i = items . length - 1 ; i > - 1 ; i -- ) {
958
- const elementRect = items [ i ] . element . nativeElement . getBoundingClientRect ( ) ;
1073
+ const elementRect = items [ i ] . getBoundingClientRect ( ) ;
959
1074
960
1075
// Remove a few pixels from the bottom offset so we get some overlap.
961
1076
dispatchMouseEvent ( document , 'mousemove' , elementRect . left , elementRect . bottom - 5 ) ;
0 commit comments