@@ -4,7 +4,7 @@ import {expect} from 'chai';
4
4
import * as sinon from 'sinon' ;
5
5
import { triggerDomEvent } from './util' ;
6
6
import { DragAndDropModule } from '../src/index' ;
7
- import { Draggable } from '../src/draggable.directive' ;
7
+ import { Draggable , ValidateDrag } from '../src/draggable.directive' ;
8
8
9
9
describe ( 'draggable directive' , ( ) => {
10
10
@@ -15,6 +15,7 @@ describe('draggable directive', () => {
15
15
[dragAxis]="dragAxis"
16
16
[dragSnapGrid]="dragSnapGrid"
17
17
[ghostDragEnabled]="ghostDragEnabled"
18
+ [validateDrag]="validateDrag"
18
19
(dragStart)="dragStart($event)"
19
20
(dragging)="dragging($event)"
20
21
(dragEnd)="dragEnd($event)">
@@ -30,6 +31,7 @@ describe('draggable directive', () => {
30
31
public dragAxis : any = { x : true , y : true } ;
31
32
public dragSnapGrid : any = { } ;
32
33
public ghostDragEnabled : boolean = true ;
34
+ public validateDrag : ValidateDrag ;
33
35
34
36
}
35
37
@@ -227,4 +229,18 @@ describe('draggable directive', () => {
227
229
expect ( fixture . componentInstance . dragging ) . to . have . been . calledWith ( { x : 0 , y : 10 } ) ;
228
230
} ) ;
229
231
232
+ it ( 'should allow drags to be validated' , ( ) => {
233
+ fixture . componentInstance . validateDrag = ( { x, y} ) => x > 0 && y > 0 ;
234
+ fixture . detectChanges ( ) ;
235
+ const draggableElement : HTMLElement = fixture . componentInstance . draggable . element . nativeElement ;
236
+ triggerDomEvent ( 'mousedown' , draggableElement , { clientX : 5 , clientY : 10 } ) ;
237
+ expect ( fixture . componentInstance . dragStart ) . to . have . been . calledWith ( { x : 0 , y : 0 } ) ;
238
+ triggerDomEvent ( 'mousemove' , draggableElement , { clientX : 3 , clientY : 10 } ) ;
239
+ expect ( fixture . componentInstance . dragging ) . not . to . have . been . calledWith ( { x : - 2 , y : 0 } ) ;
240
+ triggerDomEvent ( 'mousemove' , draggableElement , { clientX : 7 , clientY : 8 } ) ;
241
+ expect ( fixture . componentInstance . dragging ) . not . to . have . been . calledWith ( { x : 2 , y : - 2 } ) ;
242
+ triggerDomEvent ( 'mousemove' , draggableElement , { clientX : 7 , clientY : 12 } ) ;
243
+ expect ( fixture . componentInstance . dragging ) . to . have . been . calledWith ( { x : 2 , y : 2 } ) ;
244
+ } ) ;
245
+
230
246
} ) ;
0 commit comments