@@ -2183,6 +2183,59 @@ describe('CdkDrag', () => {
2183
2183
. toEqual ( [ 'Zero' , 'One' , 'Two' , 'Three' ] ) ;
2184
2184
} ) ) ;
2185
2185
2186
+ it ( 'should not sort an item if sorting the list is disabled' , fakeAsync ( ( ) => {
2187
+ const fixture = createComponent ( DraggableInDropZone ) ;
2188
+ fixture . detectChanges ( ) ;
2189
+
2190
+ const dropInstance = fixture . componentInstance . dropInstance ;
2191
+ const dragItems = fixture . componentInstance . dragItems ;
2192
+
2193
+ dropInstance . sortingDisabled = true ;
2194
+
2195
+ expect ( dragItems . map ( drag => drag . element . nativeElement . textContent ! . trim ( ) ) )
2196
+ . toEqual ( [ 'Zero' , 'One' , 'Two' , 'Three' ] ) ;
2197
+
2198
+ const firstItem = dragItems . first ;
2199
+ const thirdItemRect = dragItems . toArray ( ) [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
2200
+ const targetX = thirdItemRect . left + 1 ;
2201
+ const targetY = thirdItemRect . top + 1 ;
2202
+
2203
+ startDraggingViaMouse ( fixture , firstItem . element . nativeElement ) ;
2204
+
2205
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) as HTMLElement ;
2206
+
2207
+ dispatchMouseEvent ( document , 'mousemove' , targetX , targetY ) ;
2208
+ fixture . detectChanges ( ) ;
2209
+
2210
+ expect ( getElementIndexByPosition ( placeholder , 'top' ) )
2211
+ . toBe ( 0 , 'Expected placeholder to stay in place.' ) ;
2212
+
2213
+ dispatchMouseEvent ( document , 'mouseup' , targetX , targetY ) ;
2214
+ fixture . detectChanges ( ) ;
2215
+
2216
+ flush ( ) ;
2217
+ fixture . detectChanges ( ) ;
2218
+
2219
+ expect ( fixture . componentInstance . droppedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2220
+
2221
+ const event = fixture . componentInstance . droppedSpy . calls . mostRecent ( ) . args [ 0 ] ;
2222
+
2223
+ // Assert the event like this, rather than `toHaveBeenCalledWith`, because Jasmine will
2224
+ // go into an infinite loop trying to stringify the event, if the test fails.
2225
+ expect ( event ) . toEqual ( {
2226
+ previousIndex : 0 ,
2227
+ currentIndex : 0 ,
2228
+ item : firstItem ,
2229
+ container : dropInstance ,
2230
+ previousContainer : dropInstance ,
2231
+ isPointerOverContainer : true
2232
+ } ) ;
2233
+
2234
+ expect ( dragItems . map ( drag => drag . element . nativeElement . textContent ! . trim ( ) ) )
2235
+ . toEqual ( [ 'Zero' , 'One' , 'Two' , 'Three' ] ) ;
2236
+ } ) ) ;
2237
+
2238
+
2186
2239
} ) ;
2187
2240
2188
2241
describe ( 'in a connected drop container' , ( ) => {
@@ -2794,6 +2847,54 @@ describe('CdkDrag', () => {
2794
2847
2795
2848
} ) ) ;
2796
2849
2850
+ it ( 'should return the item to its initial position, if sorting in the source container ' +
2851
+ 'was disabled' , fakeAsync ( ( ) => {
2852
+ const fixture = createComponent ( ConnectedDropZones ) ;
2853
+ fixture . detectChanges ( ) ;
2854
+
2855
+ const groups = fixture . componentInstance . groupedDragItems ;
2856
+ const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
2857
+ const item = groups [ 0 ] [ 1 ] ;
2858
+ const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
2859
+
2860
+ fixture . componentInstance . dropInstances . first . sortingDisabled = true ;
2861
+ startDraggingViaMouse ( fixture , item . element . nativeElement ) ;
2862
+
2863
+ const placeholder = dropZones [ 0 ] . querySelector ( '.cdk-drag-placeholder' ) ! ;
2864
+
2865
+ expect ( placeholder ) . toBeTruthy ( ) ;
2866
+ expect ( dropZones [ 0 ] . contains ( placeholder ) )
2867
+ . toBe ( true , 'Expected placeholder to be inside the first container.' ) ;
2868
+ expect ( getElementIndexByPosition ( placeholder , 'top' ) )
2869
+ . toBe ( 1 , 'Expected placeholder to be at item index.' ) ;
2870
+
2871
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . left + 1 , targetRect . top + 1 ) ;
2872
+ fixture . detectChanges ( ) ;
2873
+
2874
+ expect ( dropZones [ 1 ] . contains ( placeholder ) )
2875
+ . toBe ( true , 'Expected placeholder to be inside second container.' ) ;
2876
+ expect ( getElementIndexByPosition ( placeholder , 'top' ) )
2877
+ . toBe ( 3 , 'Expected placeholder to be at the target index.' ) ;
2878
+
2879
+ const firstInitialSiblingRect = groups [ 0 ] [ 0 ] . element
2880
+ . nativeElement . getBoundingClientRect ( ) ;
2881
+
2882
+ // Return the item to an index that is different from the initial one.
2883
+ dispatchMouseEvent ( document , 'mousemove' , firstInitialSiblingRect . left + 1 ,
2884
+ firstInitialSiblingRect . top + 1 ) ;
2885
+ fixture . detectChanges ( ) ;
2886
+
2887
+ expect ( dropZones [ 0 ] . contains ( placeholder ) )
2888
+ . toBe ( true , 'Expected placeholder to be back inside first container.' ) ;
2889
+ expect ( getElementIndexByPosition ( placeholder , 'top' ) )
2890
+ . toBe ( 1 , 'Expected placeholder to be back at the initial index.' ) ;
2891
+
2892
+ dispatchMouseEvent ( document , 'mouseup' ) ;
2893
+ fixture . detectChanges ( ) ;
2894
+
2895
+ expect ( fixture . componentInstance . droppedSpy ) . not . toHaveBeenCalled ( ) ;
2896
+ } ) ) ;
2897
+
2797
2898
} ) ;
2798
2899
2799
2900
} ) ;
0 commit comments