1
- import { QueryList , ViewChildren , Component } from '@angular/core' ;
1
+ import { Component } from '@angular/core' ;
2
2
import { fakeAsync , TestBed , ComponentFixture , inject } from '@angular/core/testing' ;
3
3
import {
4
4
createMouseEvent ,
@@ -9,25 +9,21 @@ import {
9
9
} from '@angular/cdk/testing/private' ;
10
10
import { DragDropRegistry } from './drag-drop-registry' ;
11
11
import { DragDropModule } from './drag-drop-module' ;
12
- import { CdkDrag } from './directives/drag' ;
13
- import { CdkDropList } from './directives/drop-list' ;
14
12
15
13
describe ( 'DragDropRegistry' , ( ) => {
16
- let fixture : ComponentFixture < SimpleDropZone > ;
17
- let testComponent : SimpleDropZone ;
18
- let registry : DragDropRegistry < CdkDrag , CdkDropList > ;
14
+ let fixture : ComponentFixture < BlankComponent > ;
15
+ let registry : DragDropRegistry < DragItem , DragList > ;
19
16
20
17
beforeEach ( fakeAsync ( ( ) => {
21
18
TestBed . configureTestingModule ( {
22
19
imports : [ DragDropModule ] ,
23
- declarations : [ SimpleDropZone ] ,
20
+ declarations : [ BlankComponent ] ,
24
21
} ) . compileComponents ( ) ;
25
22
26
- fixture = TestBed . createComponent ( SimpleDropZone ) ;
27
- testComponent = fixture . componentInstance ;
23
+ fixture = TestBed . createComponent ( BlankComponent ) ;
28
24
fixture . detectChanges ( ) ;
29
25
30
- inject ( [ DragDropRegistry ] , ( c : DragDropRegistry < CdkDrag , CdkDropList > ) => {
26
+ inject ( [ DragDropRegistry ] , ( c : DragDropRegistry < DragItem , DragList > ) => {
31
27
registry = c ;
32
28
} ) ( ) ;
33
29
} ) ) ;
@@ -37,38 +33,38 @@ describe('DragDropRegistry', () => {
37
33
} ) ;
38
34
39
35
it ( 'should be able to start dragging an item' , ( ) => {
40
- const firstItem = testComponent . dragItems . first ;
36
+ const item = new DragItem ( ) ;
41
37
42
- expect ( registry . isDragging ( firstItem ) ) . toBe ( false ) ;
43
- registry . startDragging ( firstItem , createMouseEvent ( 'mousedown' ) ) ;
44
- expect ( registry . isDragging ( firstItem ) ) . toBe ( true ) ;
38
+ expect ( registry . isDragging ( item ) ) . toBe ( false ) ;
39
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
40
+ expect ( registry . isDragging ( item ) ) . toBe ( true ) ;
45
41
} ) ;
46
42
47
43
it ( 'should be able to stop dragging an item' , ( ) => {
48
- const firstItem = testComponent . dragItems . first ;
44
+ const item = new DragItem ( ) ;
49
45
50
- registry . startDragging ( firstItem , createMouseEvent ( 'mousedown' ) ) ;
51
- expect ( registry . isDragging ( firstItem ) ) . toBe ( true ) ;
46
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
47
+ expect ( registry . isDragging ( item ) ) . toBe ( true ) ;
52
48
53
- registry . stopDragging ( firstItem ) ;
54
- expect ( registry . isDragging ( firstItem ) ) . toBe ( false ) ;
49
+ registry . stopDragging ( item ) ;
50
+ expect ( registry . isDragging ( item ) ) . toBe ( false ) ;
55
51
} ) ;
56
52
57
53
it ( 'should stop dragging an item if it is removed' , ( ) => {
58
- const firstItem = testComponent . dragItems . first ;
54
+ const item = new DragItem ( ) ;
59
55
60
- registry . startDragging ( firstItem , createMouseEvent ( 'mousedown' ) ) ;
61
- expect ( registry . isDragging ( firstItem ) ) . toBe ( true ) ;
56
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
57
+ expect ( registry . isDragging ( item ) ) . toBe ( true ) ;
62
58
63
- registry . removeDragItem ( firstItem ) ;
64
- expect ( registry . isDragging ( firstItem ) ) . toBe ( false ) ;
59
+ registry . removeDragItem ( item ) ;
60
+ expect ( registry . isDragging ( item ) ) . toBe ( false ) ;
65
61
} ) ;
66
62
67
63
it ( 'should dispatch `mousemove` events after starting to drag via the mouse' , ( ) => {
68
64
const spy = jasmine . createSpy ( 'pointerMove spy' ) ;
69
65
const subscription = registry . pointerMove . subscribe ( spy ) ;
70
-
71
- registry . startDragging ( testComponent . dragItems . first , createMouseEvent ( 'mousedown' ) ) ;
66
+ const item = new DragItem ( true ) ;
67
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
72
68
dispatchMouseEvent ( document , 'mousemove' ) ;
73
69
74
70
expect ( spy ) . toHaveBeenCalled ( ) ;
@@ -79,9 +75,8 @@ describe('DragDropRegistry', () => {
79
75
it ( 'should dispatch `touchmove` events after starting to drag via touch' , ( ) => {
80
76
const spy = jasmine . createSpy ( 'pointerMove spy' ) ;
81
77
const subscription = registry . pointerMove . subscribe ( spy ) ;
82
-
83
- registry . startDragging ( testComponent . dragItems . first ,
84
- createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
78
+ const item = new DragItem ( true ) ;
79
+ registry . startDragging ( item , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
85
80
dispatchTouchEvent ( document , 'touchmove' ) ;
86
81
87
82
expect ( spy ) . toHaveBeenCalled ( ) ;
@@ -92,10 +87,10 @@ describe('DragDropRegistry', () => {
92
87
it ( 'should dispatch pointer move events if event propagation is stopped' , ( ) => {
93
88
const spy = jasmine . createSpy ( 'pointerMove spy' ) ;
94
89
const subscription = registry . pointerMove . subscribe ( spy ) ;
95
-
90
+ const item = new DragItem ( true ) ;
96
91
fixture . nativeElement . addEventListener ( 'mousemove' , ( e : MouseEvent ) => e . stopPropagation ( ) ) ;
97
- registry . startDragging ( testComponent . dragItems . first , createMouseEvent ( 'mousedown' ) ) ;
98
- dispatchMouseEvent ( fixture . nativeElement . querySelector ( 'div' ) , 'mousemove' ) ;
92
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
93
+ dispatchMouseEvent ( fixture . nativeElement , 'mousemove' ) ;
99
94
100
95
expect ( spy ) . toHaveBeenCalled ( ) ;
101
96
@@ -105,8 +100,9 @@ describe('DragDropRegistry', () => {
105
100
it ( 'should dispatch `mouseup` events after ending the drag via the mouse' , ( ) => {
106
101
const spy = jasmine . createSpy ( 'pointerUp spy' ) ;
107
102
const subscription = registry . pointerUp . subscribe ( spy ) ;
103
+ const item = new DragItem ( ) ;
108
104
109
- registry . startDragging ( testComponent . dragItems . first , createMouseEvent ( 'mousedown' ) ) ;
105
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
110
106
dispatchMouseEvent ( document , 'mouseup' ) ;
111
107
112
108
expect ( spy ) . toHaveBeenCalled ( ) ;
@@ -117,9 +113,9 @@ describe('DragDropRegistry', () => {
117
113
it ( 'should dispatch `touchend` events after ending the drag via touch' , ( ) => {
118
114
const spy = jasmine . createSpy ( 'pointerUp spy' ) ;
119
115
const subscription = registry . pointerUp . subscribe ( spy ) ;
116
+ const item = new DragItem ( ) ;
120
117
121
- registry . startDragging ( testComponent . dragItems . first ,
122
- createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
118
+ registry . startDragging ( item , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
123
119
dispatchTouchEvent ( document , 'touchend' ) ;
124
120
125
121
expect ( spy ) . toHaveBeenCalled ( ) ;
@@ -130,10 +126,11 @@ describe('DragDropRegistry', () => {
130
126
it ( 'should dispatch pointer up events if event propagation is stopped' , ( ) => {
131
127
const spy = jasmine . createSpy ( 'pointerUp spy' ) ;
132
128
const subscription = registry . pointerUp . subscribe ( spy ) ;
129
+ const item = new DragItem ( ) ;
133
130
134
131
fixture . nativeElement . addEventListener ( 'mouseup' , ( e : MouseEvent ) => e . stopPropagation ( ) ) ;
135
- registry . startDragging ( testComponent . dragItems . first , createMouseEvent ( 'mousedown' ) ) ;
136
- dispatchMouseEvent ( fixture . nativeElement . querySelector ( 'div' ) , 'mouseup' ) ;
132
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
133
+ dispatchMouseEvent ( fixture . nativeElement , 'mouseup' ) ;
137
134
138
135
expect ( spy ) . toHaveBeenCalled ( ) ;
139
136
@@ -156,17 +153,17 @@ describe('DragDropRegistry', () => {
156
153
} ) ;
157
154
158
155
it ( 'should not emit pointer events when dragging is over (multi touch)' , ( ) => {
159
- const firstItem = testComponent . dragItems . first ;
156
+ const item = new DragItem ( ) ;
160
157
161
158
// First finger down
162
- registry . startDragging ( firstItem , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
159
+ registry . startDragging ( item , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
163
160
// Second finger down
164
- registry . startDragging ( firstItem , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
161
+ registry . startDragging ( item , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
165
162
// First finger up
166
- registry . stopDragging ( firstItem ) ;
163
+ registry . stopDragging ( item ) ;
167
164
168
165
// Ensure dragging is over - registry is empty
169
- expect ( registry . isDragging ( firstItem ) ) . toBe ( false ) ;
166
+ expect ( registry . isDragging ( item ) ) . toBe ( false ) ;
170
167
171
168
const pointerUpSpy = jasmine . createSpy ( 'pointerUp spy' ) ;
172
169
const pointerMoveSpy = jasmine . createSpy ( 'pointerMove spy' ) ;
@@ -191,19 +188,27 @@ describe('DragDropRegistry', () => {
191
188
} ) ;
192
189
193
190
it ( 'should prevent the default `touchmove` action when an item is being dragged' , ( ) => {
194
- registry . startDragging ( testComponent . dragItems . first ,
195
- createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
191
+ const item = new DragItem ( true ) ;
192
+ registry . startDragging ( item , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
196
193
expect ( dispatchTouchEvent ( document , 'touchmove' ) . defaultPrevented ) . toBe ( true ) ;
197
194
} ) ;
198
195
199
- it ( 'should prevent the default `touchmove` if event propagation is stopped' , ( ) => {
200
- registry . startDragging ( testComponent . dragItems . first ,
201
- createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
196
+ it ( 'should prevent the default `touchmove` if the item does not consider itself as being ' +
197
+ 'dragged yet' , ( ) => {
198
+ const item = new DragItem ( false ) ;
199
+ registry . startDragging ( item , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
200
+ expect ( dispatchTouchEvent ( document , 'touchmove' ) . defaultPrevented ) . toBe ( false ) ;
202
201
203
- fixture . nativeElement . addEventListener ( 'touchmove' , ( e : TouchEvent ) => e . stopPropagation ( ) ) ;
202
+ item . shouldBeDragging = true ;
203
+ expect ( dispatchTouchEvent ( document , 'touchmove' ) . defaultPrevented ) . toBe ( true ) ;
204
+ } ) ;
204
205
205
- const event = dispatchTouchEvent ( fixture . nativeElement . querySelector ( 'div' ) , 'touchmove' ) ;
206
+ it ( 'should prevent the default `touchmove` if event propagation is stopped' , ( ) => {
207
+ const item = new DragItem ( true ) ;
208
+ registry . startDragging ( item , createTouchEvent ( 'touchstart' ) as TouchEvent ) ;
209
+ fixture . nativeElement . addEventListener ( 'touchmove' , ( e : TouchEvent ) => e . stopPropagation ( ) ) ;
206
210
211
+ const event = dispatchTouchEvent ( fixture . nativeElement , 'touchmove' ) ;
207
212
expect ( event . defaultPrevented ) . toBe ( true ) ;
208
213
} ) ;
209
214
@@ -212,15 +217,17 @@ describe('DragDropRegistry', () => {
212
217
} ) ;
213
218
214
219
it ( 'should prevent the default `selectstart` action when an item is being dragged' , ( ) => {
215
- registry . startDragging ( testComponent . dragItems . first , createMouseEvent ( 'mousedown' ) ) ;
220
+ const item = new DragItem ( true ) ;
221
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
216
222
expect ( dispatchFakeEvent ( document , 'selectstart' ) . defaultPrevented ) . toBe ( true ) ;
217
223
} ) ;
218
224
219
225
it ( 'should dispatch `scroll` events if the viewport is scrolled while dragging' , ( ) => {
220
226
const spy = jasmine . createSpy ( 'scroll spy' ) ;
221
227
const subscription = registry . scroll . subscribe ( spy ) ;
228
+ const item = new DragItem ( ) ;
222
229
223
- registry . startDragging ( testComponent . dragItems . first , createMouseEvent ( 'mousedown' ) ) ;
230
+ registry . startDragging ( item , createMouseEvent ( 'mousedown' ) ) ;
224
231
dispatchFakeEvent ( document , 'scroll' ) ;
225
232
226
233
expect ( spy ) . toHaveBeenCalled ( ) ;
@@ -237,20 +244,19 @@ describe('DragDropRegistry', () => {
237
244
subscription . unsubscribe ( ) ;
238
245
} ) ;
239
246
247
+ class DragItem {
248
+ isDragging ( ) { return this . shouldBeDragging ; }
249
+ constructor ( public shouldBeDragging = false ) {
250
+ registry . registerDragItem ( this ) ;
251
+ }
252
+ }
253
+
254
+ class DragList {
255
+ constructor ( ) {
256
+ registry . registerDropContainer ( this ) ;
257
+ }
258
+ }
259
+
260
+ @Component ( { template : `` } )
261
+ class BlankComponent { }
240
262
} ) ;
241
-
242
- @Component ( {
243
- template : `
244
- <div cdkDropList id="items" [cdkDropListData]="items">
245
- <div *ngFor="let item of items" cdkDrag>{{item}}</div>
246
- </div>
247
-
248
- <div cdkDropList id="items" *ngIf="showDuplicateContainer"></div>
249
- `
250
- } )
251
- class SimpleDropZone {
252
- @ViewChildren ( CdkDrag ) dragItems : QueryList < CdkDrag > ;
253
- @ViewChildren ( CdkDropList ) dropInstances : QueryList < CdkDropList > ;
254
- items = [ 'Zero' , 'One' , 'Two' , 'Three' ] ;
255
- showDuplicateContainer = false ;
256
- }
0 commit comments