This repository has been archived by the owner on Jun 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
DragDropManager-test.js
414 lines (356 loc) · 18.2 KB
/
DragDropManager-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import expect from 'expect.js';
import Types from './types';
import { NormalSource, NonDraggableSource, BadItemSource } from './sources';
import { NormalTarget, NonDroppableTarget, TargetWithNoDropResult, BadResultTarget, TransformResultTarget } from './targets';
import { DragDropManager, TestBackend } from '..';
describe('DragDropManager', () => {
let manager;
let backend;
let registry;
beforeEach(() => {
manager = new DragDropManager(TestBackend);
backend = manager.getBackend();
registry = manager.getRegistry();
});
describe('handler registration', () => {
it('registers and unregisters drag sources', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
expect(registry.getSource(sourceHandle)).to.equal(source);
registry.removeSource(sourceHandle);
expect(registry.getSource(sourceHandle)).to.equal(null);
expect(() => registry.removeSource(sourceHandle)).to.throwError();
});
it('registers and unregisters drop targets', () => {
const target = new NormalTarget();
const targetHandle = registry.addTarget(Types.FOO, target);
expect(registry.getTarget(targetHandle)).to.equal(target);
registry.removeTarget(targetHandle);
expect(registry.getTarget(targetHandle)).to.equal(null);
expect(() => registry.removeTarget(targetHandle)).to.throwError();
});
it('knows the difference between sources and targets', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const target = new NormalTarget();
const targetHandle = registry.addTarget(Types.FOO, target);
expect(() => registry.getSource(targetHandle)).to.throwError();
expect(() => registry.getTarget(sourceHandle)).to.throwError();
expect(() => registry.removeSource(targetHandle)).to.throwError();
expect(() => registry.removeTarget(sourceHandle)).to.throwError();
});
it('throws on invalid type', () => {
const source = new NormalSource();
const target = new NormalTarget();
expect(() => registry.addSource(null, source)).to.throwError();
expect(() => registry.addSource(undefined, source)).to.throwError();
expect(() => registry.addSource(23, source)).to.throwError();
expect(() => registry.addTarget(null, target)).to.throwError();
expect(() => registry.addTarget(undefined, target)).to.throwError();
expect(() => registry.addTarget(23, target)).to.throwError();
});
});
describe('drag source and target contract', () => {
describe('beginDrag() and canDrag()', () => {
it('ignores beginDrag() if canDrag() returns false', () => {
const source = new NonDraggableSource();
const sourceHandle = registry.addSource(Types.FOO, source);
backend.simulateBeginDrag(sourceHandle);
expect(source.didCallBeginDrag).to.equal(false);
});
it('throws if beginDrag() returns non-object', () => {
const source = new BadItemSource();
const sourceHandle = registry.addSource(Types.FOO, source);
expect(() => backend.simulateBeginDrag(sourceHandle)).to.throwError();
});
it('begins drag if canDrag() returns true', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
backend.simulateBeginDrag(sourceHandle);
expect(source.didCallBeginDrag).to.equal(true);
});
it('throws in beginDrag() if it is called twice during one operation', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
backend.simulateBeginDrag(sourceHandle);
expect(() => backend.simulateBeginDrag(sourceHandle)).to.throwError();
});
it('lets beginDrag() be called again in a next operation', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEndDrag(sourceHandle);
source.didCallBeginDrag = false;
expect(() => backend.simulateBeginDrag(sourceHandle)).to.not.throwError();
expect(source.didCallBeginDrag).to.equal(true);
});
});
describe('drop(), canDrop() and endDrag()', () => {
it('endDrag() sees drop() return value as drop result if dropped on a target', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const target = new NormalTarget();
const targetHandle = registry.addTarget(Types.FOO, target);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(target.didCallDrop).to.equal(true);
expect(source.recordedDropResult.foo).to.equal('bar');
});
it('endDrag() sees true as drop result by default if dropped on a target', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const target = new TargetWithNoDropResult();
const targetHandle = registry.addTarget(Types.FOO, target);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(source.recordedDropResult).to.equal(true);
});
it('endDrag() sees false as drop result if dropped outside a target', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEndDrag();
expect(source.recordedDropResult).to.equal(false);
});
it('calls endDrag even if source was unregistered', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
backend.simulateBeginDrag(sourceHandle);
registry.removeSource(sourceHandle);
expect(registry.getSource(sourceHandle)).to.equal(null);
backend.simulateEndDrag();
expect(source.recordedDropResult).to.equal(false);
});
it('throws in endDrag() if it is called outside a drag operation', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
expect(() => backend.simulateEndDrag(sourceHandle)).to.throwError();
});
it('ignores drop() if no drop targets entered', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
backend.simulateBeginDrag(sourceHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(source.recordedDropResult).to.equal(false);
});
it('ignores drop() if drop targets entered and left', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const targetA = new NormalTarget();
const targetAHandle = registry.addTarget(Types.FOO, targetA);
const targetB = new NormalTarget();
const targetBHandle = registry.addTarget(Types.FOO, targetB);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetAHandle);
backend.simulateEnter(targetBHandle);
backend.simulateLeave(targetBHandle);
backend.simulateLeave(targetAHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(targetA.didCallDrop).to.equal(false);
expect(targetB.didCallDrop).to.equal(false);
expect(source.recordedDropResult).to.equal(false);
});
it('ignores drop() if canDrop() returns false', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const target = new NonDroppableTarget();
const targetHandle = registry.addTarget(Types.FOO, target);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetHandle);
backend.simulateDrop();
expect(target.didCallDrop).to.equal(false);
});
it('ignores drop() if target has a different type', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const target = new NormalTarget();
const targetHandle = registry.addTarget(Types.BAR, target);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetHandle);
backend.simulateDrop();
expect(target.didCallDrop).to.equal(false);
});
it('throws in drop() if it is called outside a drag operation', () => {
expect(() => backend.simulateDrop()).to.throwError();
});
it('throws in drop() if it returns something that is neither undefined nor an object', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const target = new BadResultTarget();
const targetHandle = registry.addTarget(Types.FOO, target);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetHandle);
expect(() => backend.simulateDrop()).to.throwError();
});
describe('nested drop targets', () => {
it('uses child result if parents have no drop result', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const targetA = new TargetWithNoDropResult();
const targetAHandle = registry.addTarget(Types.FOO, targetA);
const targetB = new NormalTarget({ number: 16 });
const targetBHandle = registry.addTarget(Types.FOO, targetB);
const targetC = new NormalTarget({ number: 42 });
const targetCHandle = registry.addTarget(Types.FOO, targetC);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetAHandle);
backend.simulateEnter(targetBHandle);
backend.simulateEnter(targetCHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(targetA.didCallDrop).to.equal(true);
expect(targetB.didCallDrop).to.equal(true);
expect(targetC.didCallDrop).to.equal(true);
expect(source.recordedDropResult.number).to.equal(16);
});
it('excludes targets of different type when dispatching drop', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const targetA = new TargetWithNoDropResult();
const targetAHandle = registry.addTarget(Types.FOO, targetA);
const targetB = new NormalTarget({ number: 16 });
const targetBHandle = registry.addTarget(Types.BAR, targetB);
const targetC = new NormalTarget({ number: 42 });
const targetCHandle = registry.addTarget(Types.FOO, targetC);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetAHandle);
backend.simulateEnter(targetBHandle);
backend.simulateEnter(targetCHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(targetA.didCallDrop).to.equal(true);
expect(targetB.didCallDrop).to.equal(false);
expect(targetC.didCallDrop).to.equal(true);
expect(source.recordedDropResult.number).to.equal(42);
});
it('excludes non-droppable targets when dispatching drop', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const targetA = new TargetWithNoDropResult();
const targetAHandle = registry.addTarget(Types.FOO, targetA);
const targetB = new TargetWithNoDropResult();
const targetBHandle = registry.addTarget(Types.FOO, targetB);
const targetC = new NonDroppableTarget({ number: 16 });
const targetCHandle = registry.addTarget(Types.BAR, targetC);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetAHandle);
backend.simulateEnter(targetBHandle);
backend.simulateEnter(targetCHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(targetA.didCallDrop).to.equal(true);
expect(targetB.didCallDrop).to.equal(true);
expect(targetC.didCallDrop).to.equal(false);
expect(source.recordedDropResult).to.equal(true);
});
it('lets parent drop targets transform child results', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const targetA = new TargetWithNoDropResult();
const targetAHandle = registry.addTarget(Types.FOO, targetA);
const targetB = new TransformResultTarget(dropResult => ({ number: dropResult.number * 2 }));
const targetBHandle = registry.addTarget(Types.FOO, targetB);
const targetC = new NonDroppableTarget();
const targetCHandle = registry.addTarget(Types.FOO, targetC);
const targetD = new TransformResultTarget(dropResult => ({ number: dropResult.number + 1 }));
const targetDHandle = registry.addTarget(Types.FOO, targetD);
const targetE = new NormalTarget({ number: 42 });
const targetEHandle = registry.addTarget(Types.FOO, targetE);
const targetF = new TransformResultTarget(dropResult => ({ number: dropResult.number / 2 }));
const targetFHandle = registry.addTarget(Types.BAR, targetF);
const targetG = new NormalTarget({ number: 100 });
const targetGHandle = registry.addTarget(Types.BAR, targetG);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetAHandle);
backend.simulateEnter(targetBHandle);
backend.simulateEnter(targetCHandle);
backend.simulateEnter(targetDHandle);
backend.simulateEnter(targetEHandle);
backend.simulateEnter(targetFHandle);
backend.simulateEnter(targetGHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(targetA.didCallDrop).to.equal(true);
expect(targetB.didCallDrop).to.equal(true);
expect(targetC.didCallDrop).to.equal(false);
expect(targetD.didCallDrop).to.equal(true);
expect(targetE.didCallDrop).to.equal(true);
expect(targetF.didCallDrop).to.equal(false);
expect(targetG.didCallDrop).to.equal(false);
expect(source.recordedDropResult.number).to.equal((42 + 1) * 2);
});
it('always chooses parent drop result', () => {
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const targetA = new NormalTarget({ number: 12345 });
const targetAHandle = registry.addTarget(Types.FOO, targetA);
const targetB = new TransformResultTarget(dropResult => ({ number: dropResult.number * 2 }));
const targetBHandle = registry.addTarget(Types.FOO, targetB);
const targetC = new NonDroppableTarget();
const targetCHandle = registry.addTarget(Types.FOO, targetC);
const targetD = new TransformResultTarget(dropResult => ({ number: dropResult.number + 1 }));
const targetDHandle = registry.addTarget(Types.FOO, targetD);
const targetE = new NormalTarget({ number: 42 });
const targetEHandle = registry.addTarget(Types.FOO, targetE);
const targetF = new TransformResultTarget(dropResult => ({ number: dropResult.number / 2 }));
const targetFHandle = registry.addTarget(Types.BAR, targetF);
const targetG = new NormalTarget({ number: 100 });
const targetGHandle = registry.addTarget(Types.BAR, targetG);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetAHandle);
backend.simulateEnter(targetBHandle);
backend.simulateEnter(targetCHandle);
backend.simulateEnter(targetDHandle);
backend.simulateEnter(targetEHandle);
backend.simulateEnter(targetFHandle);
backend.simulateEnter(targetGHandle);
backend.simulateDrop();
backend.simulateEndDrag();
expect(targetA.didCallDrop).to.equal(true);
expect(targetB.didCallDrop).to.equal(true);
expect(targetC.didCallDrop).to.equal(false);
expect(targetD.didCallDrop).to.equal(true);
expect(targetE.didCallDrop).to.equal(true);
expect(targetF.didCallDrop).to.equal(false);
expect(targetG.didCallDrop).to.equal(false);
expect(source.recordedDropResult.number).to.equal(12345);
});
});
});
describe('enter() and leave()', () => {
it('throws in enter() if it is called outside a drag operation', () => {
const target = new NormalTarget();
const targetHandle = registry.addTarget(Types.BAR, target);
expect(() => backend.simulateEnter(targetHandle)).to.throwError();
});
it('throws in enter() if it is already in an entered target', () => {
const target = new NormalTarget();
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const targetHandle = registry.addTarget(Types.BAR, target);
backend.simulateBeginDrag(sourceHandle);
backend.simulateEnter(targetHandle);
expect(() => backend.simulateEnter(targetHandle)).to.throwError();
});
it('throws in leave() if it is called outside a drag operation', () => {
const target = new NormalTarget();
const targetHandle = registry.addTarget(Types.BAR, target);
expect(() => backend.simulateLeave(targetHandle)).to.throwError();
});
it('throws in leave() if it is not entered', () => {
const target = new NormalTarget();
const source = new NormalSource();
const sourceHandle = registry.addSource(Types.FOO, source);
const targetHandle = registry.addTarget(Types.BAR, target);
backend.simulateBeginDrag(sourceHandle);
expect(() => backend.simulateLeave(targetHandle)).to.throwError();
});
});
});
});