-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathSelection.test.js
More file actions
550 lines (475 loc) · 20.8 KB
/
Copy pathSelection.test.js
File metadata and controls
550 lines (475 loc) · 20.8 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import Selection, {
getEventNodeFromPoint,
getShowMoreNodeFromPoint,
isEvent,
isShowMore,
objectsCollide,
getBoundsForNode,
} from '../src/Selection'
// Helpers shared across suites
let container
let selection
function makeSelection(opts = {}) {
return new Selection(container, opts)
}
beforeEach(() => {
container = document.createElement('div')
document.body.appendChild(container)
})
afterEach(() => {
if (selection && !selection.isDetached) {
selection.teardown()
selection = null
}
if (container && document.body.contains(container)) {
document.body.removeChild(container)
}
})
// ── Constructor ───────────────────────────────────────────────────────────────
describe('Selection — constructor', () => {
test('initializes with default options', () => {
selection = makeSelection()
expect(selection.selecting).toBe(false)
expect(selection.isDetached).toBe(false)
expect(selection.globalMouse).toBe(false)
expect(selection.longPressThreshold).toBe(250)
expect(selection.validContainers).toEqual([])
})
test('accepts custom longPressThreshold', () => {
selection = makeSelection({ longPressThreshold: 500 })
expect(selection.longPressThreshold).toBe(500)
})
test('sets globalMouse=true when global=true', () => {
selection = makeSelection({ global: true })
expect(selection.globalMouse).toBe(true)
})
test('sets globalMouse=true when container is null', () => {
selection = new Selection(null, {})
expect(selection.globalMouse).toBe(true)
selection.teardown()
selection = null
})
test('accepts validContainers', () => {
selection = makeSelection({ validContainers: ['.rbc-calendar'] })
expect(selection.validContainers).toEqual(['.rbc-calendar'])
})
test('binds all handler methods', () => {
selection = makeSelection()
expect(typeof selection._handleInitialEvent).toBe('function')
expect(typeof selection._handleMoveEvent).toBe('function')
expect(typeof selection._handleTerminatingEvent).toBe('function')
expect(typeof selection._keyListener).toBe('function')
})
})
// ── on / emit ─────────────────────────────────────────────────────────────────
describe('Selection — on / emit', () => {
beforeEach(() => { selection = makeSelection() })
test('on registers handler; emit calls it', () => {
const fn = jest.fn()
selection.on('click', fn)
selection.emit('click', { x: 1 })
expect(fn).toHaveBeenCalledWith({ x: 1 })
})
test('on().remove() stops handler from firing', () => {
const fn = jest.fn()
const sub = selection.on('click', fn)
sub.remove()
selection.emit('click', {})
expect(fn).not.toHaveBeenCalled()
})
test('multiple handlers for same event all fire', () => {
const a = jest.fn()
const b = jest.fn()
selection.on('select', a)
selection.on('select', b)
selection.emit('select', {})
expect(a).toHaveBeenCalled()
expect(b).toHaveBeenCalled()
})
test('emit returns first handler result', () => {
selection.on('test', () => 'first')
selection.on('test', () => 'second')
expect(selection.emit('test')).toBe('first')
})
test('emit returns undefined when no handlers', () => {
expect(selection.emit('nothing')).toBeUndefined()
})
})
// ── isClick ───────────────────────────────────────────────────────────────────
describe('Selection — isClick', () => {
beforeEach(() => {
selection = makeSelection()
selection._initialEventData = { x: 100, y: 100, isTouch: false }
})
test('returns true when within clickTolerance (5px)', () => {
expect(selection.isClick(104, 104)).toBe(true)
})
test('returns false when x exceeds tolerance', () => {
expect(selection.isClick(106, 100)).toBe(false)
})
test('returns false when y exceeds tolerance', () => {
expect(selection.isClick(100, 106)).toBe(false)
})
test('returns false for touch events regardless of position', () => {
selection._initialEventData.isTouch = true
expect(selection.isClick(100, 100)).toBe(false)
})
test('returns true at exact tolerance boundary', () => {
expect(selection.isClick(105, 105)).toBe(true)
})
})
// ── _handleClickEvent ─────────────────────────────────────────────────────────
describe('Selection — _handleClickEvent', () => {
beforeEach(() => { selection = makeSelection() })
test('emits click on first click', () => {
const fn = jest.fn()
selection.on('click', fn)
selection._handleClickEvent({ type: 'mouseup', pageX: 10, pageY: 10, clientX: 10, clientY: 10 })
expect(fn).toHaveBeenCalled()
})
test('emits doubleClick on second click within 250ms', () => {
const dblFn = jest.fn()
selection.on('doubleClick', dblFn)
selection._lastClickData = { timestamp: Date.now() - 100 }
selection._handleClickEvent({ type: 'mouseup', pageX: 10, pageY: 10, clientX: 10, clientY: 10 })
expect(dblFn).toHaveBeenCalled()
})
test('emits click (not doubleClick) when last click was > 250ms ago', () => {
const clickFn = jest.fn()
const dblFn = jest.fn()
selection.on('click', clickFn)
selection.on('doubleClick', dblFn)
selection._lastClickData = { timestamp: Date.now() - 300 }
selection._handleClickEvent({ type: 'mouseup', pageX: 10, pageY: 10, clientX: 10, clientY: 10 })
expect(clickFn).toHaveBeenCalled()
expect(dblFn).not.toHaveBeenCalled()
})
})
// ── _handleMoveEvent ──────────────────────────────────────────────────────────
describe('Selection — _handleMoveEvent', () => {
beforeEach(() => { selection = makeSelection() })
test('returns early when _initialEventData is null', () => {
selection._initialEventData = null
const fn = jest.fn()
selection.on('selectStart', fn)
selection._handleMoveEvent({ pageX: 100, pageY: 100, preventDefault: jest.fn() })
expect(fn).not.toHaveBeenCalled()
})
test('returns early when isDetached', () => {
selection._initialEventData = { x: 0, y: 0, isTouch: false }
selection.isDetached = true
const fn = jest.fn()
selection.on('selectStart', fn)
selection._handleMoveEvent({ pageX: 100, pageY: 100, preventDefault: jest.fn() })
expect(fn).not.toHaveBeenCalled()
})
test('emits selectStart on first move beyond tolerance', () => {
selection._initialEventData = { x: 10, y: 10, isTouch: false }
const fn = jest.fn()
selection.on('selectStart', fn)
selection._handleMoveEvent({ pageX: 100, pageY: 100, preventDefault: jest.fn() })
expect(fn).toHaveBeenCalled()
})
test('emits selecting and updates _selectRect after selectStart', () => {
selection._initialEventData = { x: 10, y: 10, isTouch: false }
const fn = jest.fn()
selection.on('selecting', fn)
selection._handleMoveEvent({ pageX: 100, pageY: 100, preventDefault: jest.fn() })
expect(fn).toHaveBeenCalled()
expect(selection._selectRect).toBeTruthy()
expect(selection.selecting).toBe(true)
})
test('does not emit selectStart on tiny move (click territory)', () => {
selection._initialEventData = { x: 10, y: 10, isTouch: false }
const fn = jest.fn()
selection.on('selectStart', fn)
// Move only 2px — within clickTolerance of 5
selection._handleMoveEvent({ pageX: 12, pageY: 12, preventDefault: jest.fn() })
// tiny move with w=2, h=2 — click=true, old=false, !(w || h) is false so it returns early
expect(fn).not.toHaveBeenCalled()
})
})
// ── _handleTerminatingEvent ───────────────────────────────────────────────────
describe('Selection — _handleTerminatingEvent', () => {
beforeEach(() => {
selection = makeSelection()
selection._initialEventData = { x: 10, y: 10, isTouch: false }
})
test('emits select when selecting=true', () => {
selection.selecting = true
selection._selectRect = { top: 0, left: 0, right: 100, bottom: 100 }
const fn = jest.fn()
selection.on('select', fn)
// container() is called internally — make it a callable function
selection.container = () => container
selection._handleTerminatingEvent({
type: 'mouseup',
target: container,
key: undefined,
})
expect(fn).toHaveBeenCalled()
expect(selection.selecting).toBe(false)
})
test('emits reset on Escape key', () => {
// When not selecting and key event: e is replaced by _initialEvent
// Then key='Escape' → emit reset
selection.selecting = false
const escapeEvent = { type: 'mouseup', key: 'Escape', target: container }
selection._initialEvent = escapeEvent
selection.container = () => container
const fn = jest.fn()
selection.on('reset', fn)
selection._handleTerminatingEvent({ type: 'keydown', key: undefined, target: container })
expect(fn).toHaveBeenCalled()
})
test('emits reset when not in valid container', () => {
selection.validContainers = ['.rbc-does-not-exist']
selection.selecting = false
const fn = jest.fn()
selection.on('reset', fn)
selection._initialEvent = { type: 'mouseup', target: container }
selection.container = () => container
selection._handleTerminatingEvent({ type: 'mouseup', key: undefined, target: container })
expect(fn).toHaveBeenCalled()
})
test('does nothing when e is null after key event with no initialEvent', () => {
selection._initialEvent = null
const fn = jest.fn()
selection.on('reset', fn)
selection.on('click', fn)
selection.on('select', fn)
// keydown with no _initialEvent means e becomes null inside handler
selection._handleTerminatingEvent({ type: 'keydown', key: undefined })
expect(fn).not.toHaveBeenCalled()
})
})
// ── _dropFromOutsideListener ──────────────────────────────────────────────────
describe('Selection — _dropFromOutsideListener', () => {
beforeEach(() => { selection = makeSelection() })
test('emits dropFromOutside with coordinates', () => {
const fn = jest.fn()
selection.on('dropFromOutside', fn)
const e = {
pageX: 50, pageY: 60, clientX: 50, clientY: 60,
preventDefault: jest.fn(),
}
selection._dropFromOutsideListener(e)
expect(fn).toHaveBeenCalledWith({ x: 50, y: 60, clientX: 50, clientY: 60 })
expect(e.preventDefault).toHaveBeenCalled()
})
test('extracts coords from touches when present', () => {
const fn = jest.fn()
selection.on('dropFromOutside', fn)
const e = {
touches: [{ pageX: 70, pageY: 80, clientX: 70, clientY: 80 }],
preventDefault: jest.fn(),
}
selection._dropFromOutsideListener(e)
expect(fn).toHaveBeenCalledWith({ x: 70, y: 80, clientX: 70, clientY: 80 })
})
})
// ── _dragOverFromOutsideListener ──────────────────────────────────────────────
describe('Selection — _dragOverFromOutsideListener', () => {
beforeEach(() => { selection = makeSelection() })
test('emits dragOverFromOutside with coordinates', () => {
const fn = jest.fn()
selection.on('dragOverFromOutside', fn)
const e = {
pageX: 10, pageY: 20, clientX: 10, clientY: 20,
preventDefault: jest.fn(),
}
selection._dragOverFromOutsideListener(e)
expect(fn).toHaveBeenCalledWith({ x: 10, y: 20, clientX: 10, clientY: 20 })
expect(e.preventDefault).toHaveBeenCalled()
})
})
// ── _keyListener ──────────────────────────────────────────────────────────────
describe('Selection — _keyListener', () => {
beforeEach(() => { selection = makeSelection() })
test('sets ctrl=true when metaKey pressed', () => {
selection._keyListener({ metaKey: true, ctrlKey: false })
expect(selection.ctrl).toBe(true)
})
test('sets ctrl=true when ctrlKey pressed', () => {
selection._keyListener({ metaKey: false, ctrlKey: true })
expect(selection.ctrl).toBe(true)
})
test('sets ctrl=false when neither key pressed', () => {
selection._keyListener({ metaKey: false, ctrlKey: false })
expect(selection.ctrl).toBe(false)
})
})
// ── _isWithinValidContainer ───────────────────────────────────────────────────
describe('Selection — _isWithinValidContainer', () => {
test('returns true when validContainers is empty', () => {
selection = makeSelection()
expect(selection._isWithinValidContainer({ target: container })).toBe(true)
})
test('returns true when target matches a valid container selector', () => {
const div = document.createElement('div')
div.className = 'rbc-calendar'
selection = makeSelection({ validContainers: ['.rbc-calendar'] })
expect(selection._isWithinValidContainer({ target: div })).toBe(true)
})
test('returns false when target does not match any valid container', () => {
const div = document.createElement('div')
selection = makeSelection({ validContainers: ['.rbc-calendar'] })
expect(selection._isWithinValidContainer({ target: div })).toBe(false)
})
test('returns true when target is null/undefined', () => {
selection = makeSelection({ validContainers: ['.rbc-calendar'] })
expect(selection._isWithinValidContainer({ target: null })).toBe(true)
})
})
// ── teardown ──────────────────────────────────────────────────────────────────
describe('Selection — teardown', () => {
test('sets isDetached=true and clears state', () => {
selection = makeSelection()
selection._initialEvent = {}
selection.selecting = true
selection._selectRect = {}
selection.teardown()
expect(selection.isDetached).toBe(true)
expect(selection.selecting).toBe(false)
expect(selection._initialEvent).toBeNull()
expect(selection._selectRect).toBeNull()
})
test('clears all listener handlers', () => {
selection = makeSelection()
const fn = jest.fn()
selection.on('click', fn)
selection.teardown()
selection.emit('click')
expect(fn).not.toHaveBeenCalled()
})
test('is safe to call when optional remove fns are absent', () => {
selection = makeSelection()
// Remove some cleanup functions to test guard clauses
selection._removeEndListener = null
selection._removeMoveListener = null
selection._onEscListener = null
expect(() => selection.teardown()).not.toThrow()
})
})
// ── isSelected / filter ───────────────────────────────────────────────────────
describe('Selection — isSelected / filter', () => {
beforeEach(() => { selection = makeSelection() })
test('isSelected returns false when not selecting', () => {
expect(selection.isSelected(document.createElement('div'))).toBe(false)
})
test('isSelected returns false when _selectRect is null', () => {
selection.selecting = true
selection._selectRect = null
expect(selection.isSelected(document.createElement('div'))).toBe(false)
})
test('filter returns empty array when not selecting', () => {
expect(selection.filter([{ id: 1 }, { id: 2 }])).toEqual([])
})
test('filter returns empty array when _selectRect is null', () => {
selection.selecting = true
selection._selectRect = null
expect(selection.filter([{ id: 1 }])).toEqual([])
})
})
// ── objectsCollide ────────────────────────────────────────────────────────────
describe('Selection — objectsCollide', () => {
test('returns true for overlapping boxes', () => {
const a = { top: 0, left: 0, bottom: 100, right: 100 }
const b = { top: 50, left: 50, bottom: 150, right: 150 }
expect(objectsCollide(a, b)).toBe(true)
})
test('returns false for completely separate boxes', () => {
const a = { top: 0, left: 0, bottom: 50, right: 50 }
const b = { top: 100, left: 100, bottom: 200, right: 200 }
expect(objectsCollide(a, b)).toBe(false)
})
test('returns true at exact boundary (touching)', () => {
const a = { top: 0, left: 0, bottom: 100, right: 100 }
const b = { top: 100, left: 100, bottom: 200, right: 200 }
expect(objectsCollide(a, b)).toBe(true)
})
test('handles plain point (no right/bottom): point-vs-box', () => {
const box = { top: 0, left: 0, bottom: 100, right: 100 }
const point = { top: 50, left: 50 }
// right defaults to left, bottom defaults to top → treated as a point
expect(objectsCollide(box, point)).toBe(true)
})
test('returns false when a is above b with gap', () => {
const a = { top: 0, left: 0, bottom: 40, right: 100 }
const b = { top: 60, left: 0, bottom: 100, right: 100 }
expect(objectsCollide(a, b)).toBe(false)
})
test('returns false when a is left of b with gap', () => {
const a = { top: 0, left: 0, bottom: 100, right: 40 }
const b = { top: 0, left: 60, bottom: 100, right: 100 }
expect(objectsCollide(a, b)).toBe(false)
})
test('tolerance makes overlapping boxes NOT collide (tightens the check)', () => {
// a and b overlap slightly (bTop=40 < aBottom=50)
const a = { top: 0, left: 0, bottom: 50, right: 100 }
const b = { top: 40, left: 40, bottom: 100, right: 100 }
// Without tolerance: overlap exists → collision
expect(objectsCollide(a, b, 0)).toBe(true)
// tolerance=20: aBottom(50)-20=30 < bTop(40) → separated → no collision
expect(objectsCollide(a, b, 20)).toBe(false)
})
})
// ── getBoundsForNode ──────────────────────────────────────────────────────────
describe('Selection — getBoundsForNode', () => {
test('returns the object itself when no getBoundingClientRect', () => {
const node = { top: 10, left: 20, right: 100, bottom: 50 }
expect(getBoundsForNode(node)).toBe(node)
})
test('calculates bounds for a real DOM node', () => {
const node = document.createElement('div')
document.body.appendChild(node)
const bounds = getBoundsForNode(node)
expect(bounds).toHaveProperty('top')
expect(bounds).toHaveProperty('left')
expect(bounds).toHaveProperty('right')
expect(bounds).toHaveProperty('bottom')
document.body.removeChild(node)
})
})
// ── ownerDocument (rendered into another document) ──────────────────────────────
describe('Selection — ownerDocument', () => {
test('resolves ownerDocument/ownerWindow from the container node', () => {
const frameDoc = document.implementation.createHTMLDocument('frame')
const node = frameDoc.createElement('div')
frameDoc.body.appendChild(node)
selection = new Selection(node)
expect(selection.ownerDocument).toBe(frameDoc)
// createHTMLDocument() has no defaultView, so the window falls back to global
expect(selection.ownerWindow).toBe(window)
})
test('binds its listeners to the container document, not the global document', () => {
const frameDoc = document.implementation.createHTMLDocument('frame')
const node = frameDoc.createElement('div')
frameDoc.body.appendChild(node)
const frameSpy = jest.spyOn(frameDoc, 'addEventListener')
const globalSpy = jest.spyOn(document, 'addEventListener')
selection = new Selection(node)
const frameTypes = frameSpy.mock.calls.map((c) => c[0])
expect(frameTypes).toEqual(
expect.arrayContaining([
'keydown',
'keyup',
'drop',
'dragover',
'mousedown',
'touchstart',
])
)
// The fix: these no longer land on the global document.
const globalTypes = globalSpy.mock.calls.map((c) => c[0])
expect(globalTypes).not.toContain('drop')
expect(globalTypes).not.toContain('dragover')
frameSpy.mockRestore()
globalSpy.mockRestore()
})
test('falls back to the global document when there is no container', () => {
selection = new Selection(null, { global: true })
expect(selection.ownerDocument).toBe(document)
expect(selection.ownerWindow).toBe(window)
})
})