@@ -246,6 +246,38 @@ describe('CdkDrag', () => {
246
246
subscription . unsubscribe ( ) ;
247
247
} ) ;
248
248
249
+ it ( 'should be able to lock dragging along the x axis' , fakeAsync ( ( ) => {
250
+ const fixture = createComponent ( StandaloneDraggable ) ;
251
+ fixture . detectChanges ( ) ;
252
+ fixture . componentInstance . dragInstance . lockAxis = 'x' ;
253
+
254
+ const dragElement = fixture . componentInstance . dragElement . nativeElement ;
255
+
256
+ expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
257
+
258
+ dragElementViaMouse ( fixture , dragElement , 50 , 100 ) ;
259
+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(50px, 0px, 0px)' ) ;
260
+
261
+ dragElementViaMouse ( fixture , dragElement , 100 , 200 ) ;
262
+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(150px, 0px, 0px)' ) ;
263
+ } ) ) ;
264
+
265
+ it ( 'should be able to lock dragging along the y axis' , fakeAsync ( ( ) => {
266
+ const fixture = createComponent ( StandaloneDraggable ) ;
267
+ fixture . detectChanges ( ) ;
268
+ fixture . componentInstance . dragInstance . lockAxis = 'y' ;
269
+
270
+ const dragElement = fixture . componentInstance . dragElement . nativeElement ;
271
+
272
+ expect ( dragElement . style . transform ) . toBeFalsy ( ) ;
273
+
274
+ dragElementViaMouse ( fixture , dragElement , 50 , 100 ) ;
275
+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(0px, 100px, 0px)' ) ;
276
+
277
+ dragElementViaMouse ( fixture , dragElement , 100 , 200 ) ;
278
+ expect ( dragElement . style . transform ) . toBe ( 'translate3d(0px, 300px, 0px)' ) ;
279
+ } ) ) ;
280
+
249
281
} ) ;
250
282
251
283
describe ( 'draggable with a handle' , ( ) => {
@@ -690,6 +722,65 @@ describe('CdkDrag', () => {
690
722
expect ( preview . style . transform ) . toBe ( 'translate3d(50px, 50px, 0px)' ) ;
691
723
} ) ) ;
692
724
725
+ it ( 'should lock position inside a drop container along the x axis' , fakeAsync ( ( ) => {
726
+ const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
727
+ fixture . detectChanges ( ) ;
728
+
729
+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] ;
730
+ const element = item . element . nativeElement ;
731
+
732
+ item . lockAxis = 'x' ;
733
+
734
+ dispatchMouseEvent ( element , 'mousedown' , 50 , 50 ) ;
735
+ fixture . detectChanges ( ) ;
736
+
737
+ dispatchMouseEvent ( element , 'mousemove' , 100 , 100 ) ;
738
+ fixture . detectChanges ( ) ;
739
+
740
+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
741
+
742
+ expect ( preview . style . transform ) . toBe ( 'translate3d(100px, 50px, 0px)' ) ;
743
+ } ) ) ;
744
+
745
+ it ( 'should lock position inside a drop container along the y axis' , fakeAsync ( ( ) => {
746
+ const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
747
+ fixture . detectChanges ( ) ;
748
+
749
+ const item = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] ;
750
+ const element = item . element . nativeElement ;
751
+
752
+ item . lockAxis = 'y' ;
753
+
754
+ dispatchMouseEvent ( element , 'mousedown' , 50 , 50 ) ;
755
+ fixture . detectChanges ( ) ;
756
+
757
+ dispatchMouseEvent ( element , 'mousemove' , 100 , 100 ) ;
758
+ fixture . detectChanges ( ) ;
759
+
760
+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
761
+
762
+ expect ( preview . style . transform ) . toBe ( 'translate3d(50px, 100px, 0px)' ) ;
763
+ } ) ) ;
764
+
765
+ it ( 'should inherit the position locking from the drop container' , fakeAsync ( ( ) => {
766
+ const fixture = createComponent ( DraggableInDropZoneWithCustomPreview ) ;
767
+ fixture . detectChanges ( ) ;
768
+
769
+ const element = fixture . componentInstance . dragItems . toArray ( ) [ 1 ] . element . nativeElement ;
770
+
771
+ fixture . componentInstance . dropInstance . lockAxis = 'x' ;
772
+
773
+ dispatchMouseEvent ( element , 'mousedown' , 50 , 50 ) ;
774
+ fixture . detectChanges ( ) ;
775
+
776
+ dispatchMouseEvent ( element , 'mousemove' , 100 , 100 ) ;
777
+ fixture . detectChanges ( ) ;
778
+
779
+ const preview = document . querySelector ( '.cdk-drag-preview' ) ! as HTMLElement ;
780
+
781
+ expect ( preview . style . transform ) . toBe ( 'translate3d(100px, 50px, 0px)' ) ;
782
+ } ) ) ;
783
+
693
784
it ( 'should be able to customize the placeholder' , fakeAsync ( ( ) => {
694
785
const fixture = createComponent ( DraggableInDropZoneWithCustomPlaceholder ) ;
695
786
fixture . detectChanges ( ) ;
@@ -1103,6 +1194,7 @@ export class DraggableInHorizontalDropZone {
1103
1194
`
1104
1195
} )
1105
1196
export class DraggableInDropZoneWithCustomPreview {
1197
+ @ViewChild ( CdkDrop ) dropInstance : CdkDrop ;
1106
1198
@ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
1107
1199
items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
1108
1200
}
0 commit comments