@@ -2241,6 +2241,59 @@ describe('CdkDrag', () => {
2241
2241
. toEqual ( [ 'Zero' , 'One' , 'Two' , 'Three' ] ) ;
2242
2242
} ) ) ;
2243
2243
2244
+ it ( 'should not sort an item if sorting the list is disabled' , fakeAsync ( ( ) => {
2245
+ const fixture = createComponent ( DraggableInDropZone ) ;
2246
+ fixture . detectChanges ( ) ;
2247
+
2248
+ const dropInstance = fixture . componentInstance . dropInstance ;
2249
+ const dragItems = fixture . componentInstance . dragItems ;
2250
+
2251
+ dropInstance . sortingDisabled = true ;
2252
+
2253
+ expect ( dragItems . map ( drag => drag . element . nativeElement . textContent ! . trim ( ) ) )
2254
+ . toEqual ( [ 'Zero' , 'One' , 'Two' , 'Three' ] ) ;
2255
+
2256
+ const firstItem = dragItems . first ;
2257
+ const thirdItemRect = dragItems . toArray ( ) [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
2258
+ const targetX = thirdItemRect . left + 1 ;
2259
+ const targetY = thirdItemRect . top + 1 ;
2260
+
2261
+ startDraggingViaMouse ( fixture , firstItem . element . nativeElement ) ;
2262
+
2263
+ const placeholder = document . querySelector ( '.cdk-drag-placeholder' ) as HTMLElement ;
2264
+
2265
+ dispatchMouseEvent ( document , 'mousemove' , targetX , targetY ) ;
2266
+ fixture . detectChanges ( ) ;
2267
+
2268
+ expect ( getElementIndexByPosition ( placeholder , 'top' ) )
2269
+ . toBe ( 0 , 'Expected placeholder to stay in place.' ) ;
2270
+
2271
+ dispatchMouseEvent ( document , 'mouseup' , targetX , targetY ) ;
2272
+ fixture . detectChanges ( ) ;
2273
+
2274
+ flush ( ) ;
2275
+ fixture . detectChanges ( ) ;
2276
+
2277
+ expect ( fixture . componentInstance . droppedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2278
+
2279
+ const event = fixture . componentInstance . droppedSpy . calls . mostRecent ( ) . args [ 0 ] ;
2280
+
2281
+ // Assert the event like this, rather than `toHaveBeenCalledWith`, because Jasmine will
2282
+ // go into an infinite loop trying to stringify the event, if the test fails.
2283
+ expect ( event ) . toEqual ( {
2284
+ previousIndex : 0 ,
2285
+ currentIndex : 0 ,
2286
+ item : firstItem ,
2287
+ container : dropInstance ,
2288
+ previousContainer : dropInstance ,
2289
+ isPointerOverContainer : true
2290
+ } ) ;
2291
+
2292
+ expect ( dragItems . map ( drag => drag . element . nativeElement . textContent ! . trim ( ) ) )
2293
+ . toEqual ( [ 'Zero' , 'One' , 'Two' , 'Three' ] ) ;
2294
+ } ) ) ;
2295
+
2296
+
2244
2297
} ) ;
2245
2298
2246
2299
describe ( 'in a connected drop container' , ( ) => {
@@ -2852,6 +2905,54 @@ describe('CdkDrag', () => {
2852
2905
2853
2906
} ) ) ;
2854
2907
2908
+ it ( 'should return the item to its initial position, if sorting in the source container ' +
2909
+ 'was disabled' , fakeAsync ( ( ) => {
2910
+ const fixture = createComponent ( ConnectedDropZones ) ;
2911
+ fixture . detectChanges ( ) ;
2912
+
2913
+ const groups = fixture . componentInstance . groupedDragItems ;
2914
+ const dropZones = fixture . componentInstance . dropInstances . map ( d => d . element . nativeElement ) ;
2915
+ const item = groups [ 0 ] [ 1 ] ;
2916
+ const targetRect = groups [ 1 ] [ 2 ] . element . nativeElement . getBoundingClientRect ( ) ;
2917
+
2918
+ fixture . componentInstance . dropInstances . first . sortingDisabled = true ;
2919
+ startDraggingViaMouse ( fixture , item . element . nativeElement ) ;
2920
+
2921
+ const placeholder = dropZones [ 0 ] . querySelector ( '.cdk-drag-placeholder' ) ! ;
2922
+
2923
+ expect ( placeholder ) . toBeTruthy ( ) ;
2924
+ expect ( dropZones [ 0 ] . contains ( placeholder ) )
2925
+ . toBe ( true , 'Expected placeholder to be inside the first container.' ) ;
2926
+ expect ( getElementIndexByPosition ( placeholder , 'top' ) )
2927
+ . toBe ( 1 , 'Expected placeholder to be at item index.' ) ;
2928
+
2929
+ dispatchMouseEvent ( document , 'mousemove' , targetRect . left + 1 , targetRect . top + 1 ) ;
2930
+ fixture . detectChanges ( ) ;
2931
+
2932
+ expect ( dropZones [ 1 ] . contains ( placeholder ) )
2933
+ . toBe ( true , 'Expected placeholder to be inside second container.' ) ;
2934
+ expect ( getElementIndexByPosition ( placeholder , 'top' ) )
2935
+ . toBe ( 3 , 'Expected placeholder to be at the target index.' ) ;
2936
+
2937
+ const firstInitialSiblingRect = groups [ 0 ] [ 0 ] . element
2938
+ . nativeElement . getBoundingClientRect ( ) ;
2939
+
2940
+ // Return the item to an index that is different from the initial one.
2941
+ dispatchMouseEvent ( document , 'mousemove' , firstInitialSiblingRect . left + 1 ,
2942
+ firstInitialSiblingRect . top + 1 ) ;
2943
+ fixture . detectChanges ( ) ;
2944
+
2945
+ expect ( dropZones [ 0 ] . contains ( placeholder ) )
2946
+ . toBe ( true , 'Expected placeholder to be back inside first container.' ) ;
2947
+ expect ( getElementIndexByPosition ( placeholder , 'top' ) )
2948
+ . toBe ( 1 , 'Expected placeholder to be back at the initial index.' ) ;
2949
+
2950
+ dispatchMouseEvent ( document , 'mouseup' ) ;
2951
+ fixture . detectChanges ( ) ;
2952
+
2953
+ expect ( fixture . componentInstance . droppedSpy ) . not . toHaveBeenCalled ( ) ;
2954
+ } ) ) ;
2955
+
2855
2956
} ) ;
2856
2957
2857
2958
} ) ;
0 commit comments