@@ -246,6 +246,38 @@ describe('CdkDrag', () => {
246246 subscription . unsubscribe ( ) ;
247247 } ) ;
248248
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+
249281 } ) ;
250282
251283 describe ( 'draggable with a handle' , ( ) => {
@@ -690,6 +722,65 @@ describe('CdkDrag', () => {
690722 expect ( preview . style . transform ) . toBe ( 'translate3d(50px, 50px, 0px)' ) ;
691723 } ) ) ;
692724
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+
693784 it ( 'should be able to customize the placeholder' , fakeAsync ( ( ) => {
694785 const fixture = createComponent ( DraggableInDropZoneWithCustomPlaceholder ) ;
695786 fixture . detectChanges ( ) ;
@@ -1103,6 +1194,7 @@ export class DraggableInHorizontalDropZone {
11031194 `
11041195} )
11051196export class DraggableInDropZoneWithCustomPreview {
1197+ @ViewChild ( CdkDrop ) dropInstance : CdkDrop ;
11061198 @ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
11071199 items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
11081200}
0 commit comments