@pyreon/dnd 0.24.5 → 0.24.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -6
- package/src/index.ts +0 -20
- package/src/tests/dnd.test.ts +0 -760
- package/src/tests/integration.test.ts +0 -1662
- package/src/tests/use-sortable-leak.test.ts +0 -110
- package/src/types.ts +0 -92
- package/src/use-drag-monitor.ts +0 -69
- package/src/use-draggable.ts +0 -68
- package/src/use-droppable.ts +0 -69
- package/src/use-file-drop.browser.test.tsx +0 -148
- package/src/use-file-drop.ts +0 -133
- package/src/use-sortable.ts +0 -265
package/src/tests/dnd.test.ts
DELETED
|
@@ -1,760 +0,0 @@
|
|
|
1
|
-
import { signal } from '@pyreon/reactivity'
|
|
2
|
-
import { describe, expect, it } from 'vitest'
|
|
3
|
-
|
|
4
|
-
// ─── useDraggable ───────────────────────────────────────────────────────────
|
|
5
|
-
|
|
6
|
-
describe('useDraggable', () => {
|
|
7
|
-
it('exports useDraggable', async () => {
|
|
8
|
-
const { useDraggable } = await import('../index')
|
|
9
|
-
expect(typeof useDraggable).toBe('function')
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
it('returns isDragging signal initialized to false', async () => {
|
|
13
|
-
const { useDraggable } = await import('../use-draggable')
|
|
14
|
-
const el = document.createElement('div')
|
|
15
|
-
const { isDragging } = useDraggable({ element: () => el, data: { id: '1' } })
|
|
16
|
-
expect(isDragging()).toBe(false)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
it('accepts function data for dynamic values', async () => {
|
|
20
|
-
const { useDraggable } = await import('../use-draggable')
|
|
21
|
-
const el = document.createElement('div')
|
|
22
|
-
const counter = signal(0)
|
|
23
|
-
const { isDragging } = useDraggable({
|
|
24
|
-
element: () => el,
|
|
25
|
-
data: () => ({ count: counter() }),
|
|
26
|
-
})
|
|
27
|
-
expect(isDragging()).toBe(false)
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('accepts disabled as boolean', async () => {
|
|
31
|
-
const { useDraggable } = await import('../use-draggable')
|
|
32
|
-
const el = document.createElement('div')
|
|
33
|
-
const { isDragging } = useDraggable({
|
|
34
|
-
element: () => el,
|
|
35
|
-
data: { id: '1' },
|
|
36
|
-
disabled: true,
|
|
37
|
-
})
|
|
38
|
-
expect(isDragging()).toBe(false)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('accepts disabled as reactive getter', async () => {
|
|
42
|
-
const { useDraggable } = await import('../use-draggable')
|
|
43
|
-
const el = document.createElement('div')
|
|
44
|
-
const disabled = signal(false)
|
|
45
|
-
const { isDragging } = useDraggable({
|
|
46
|
-
element: () => el,
|
|
47
|
-
data: { id: '1' },
|
|
48
|
-
disabled,
|
|
49
|
-
})
|
|
50
|
-
expect(isDragging()).toBe(false)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('handles null element gracefully', async () => {
|
|
54
|
-
const { useDraggable } = await import('../use-draggable')
|
|
55
|
-
const { isDragging } = useDraggable({
|
|
56
|
-
element: () => null,
|
|
57
|
-
data: { id: '1' },
|
|
58
|
-
})
|
|
59
|
-
expect(isDragging()).toBe(false)
|
|
60
|
-
})
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
// ─── useDroppable ───────────────────────────────────────────────────────────
|
|
64
|
-
|
|
65
|
-
describe('useDroppable', () => {
|
|
66
|
-
it('exports useDroppable', async () => {
|
|
67
|
-
const { useDroppable } = await import('../index')
|
|
68
|
-
expect(typeof useDroppable).toBe('function')
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
it('returns isOver signal initialized to false', async () => {
|
|
72
|
-
const { useDroppable } = await import('../use-droppable')
|
|
73
|
-
const el = document.createElement('div')
|
|
74
|
-
const { isOver } = useDroppable({ element: () => el, onDrop: () => {} })
|
|
75
|
-
expect(isOver()).toBe(false)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('accepts canDrop filter', async () => {
|
|
79
|
-
const { useDroppable } = await import('../use-droppable')
|
|
80
|
-
const el = document.createElement('div')
|
|
81
|
-
const { isOver } = useDroppable({
|
|
82
|
-
element: () => el,
|
|
83
|
-
canDrop: (data) => data.type === 'card',
|
|
84
|
-
onDrop: () => {},
|
|
85
|
-
})
|
|
86
|
-
expect(isOver()).toBe(false)
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
it('handles null element gracefully', async () => {
|
|
90
|
-
const { useDroppable } = await import('../use-droppable')
|
|
91
|
-
const { isOver } = useDroppable({ element: () => null, onDrop: () => {} })
|
|
92
|
-
expect(isOver()).toBe(false)
|
|
93
|
-
})
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
// ─── useSortable ────────────────────────────────────────────────────────────
|
|
97
|
-
|
|
98
|
-
describe('useSortable', () => {
|
|
99
|
-
it('exports useSortable', async () => {
|
|
100
|
-
const { useSortable } = await import('../index')
|
|
101
|
-
expect(typeof useSortable).toBe('function')
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
it('returns full sortable API with overEdge', async () => {
|
|
105
|
-
const { useSortable } = await import('../use-sortable')
|
|
106
|
-
const items = signal([
|
|
107
|
-
{ id: '1', name: 'A' },
|
|
108
|
-
{ id: '2', name: 'B' },
|
|
109
|
-
{ id: '3', name: 'C' },
|
|
110
|
-
])
|
|
111
|
-
|
|
112
|
-
const result = useSortable({
|
|
113
|
-
items,
|
|
114
|
-
by: (item) => item.id,
|
|
115
|
-
onReorder: (newItems) => items.set(newItems),
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
expect(typeof result.containerRef).toBe('function')
|
|
119
|
-
expect(typeof result.itemRef).toBe('function')
|
|
120
|
-
expect(result.activeId()).toBeNull()
|
|
121
|
-
expect(result.overId()).toBeNull()
|
|
122
|
-
expect(result.overEdge()).toBeNull()
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
it('itemRef returns a ref callback per key', async () => {
|
|
126
|
-
const { useSortable } = await import('../use-sortable')
|
|
127
|
-
const items = signal([{ id: '1' }, { id: '2' }])
|
|
128
|
-
|
|
129
|
-
const { itemRef } = useSortable({
|
|
130
|
-
items,
|
|
131
|
-
by: (item) => item.id,
|
|
132
|
-
onReorder: () => {},
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
const ref1 = itemRef('1')
|
|
136
|
-
const ref2 = itemRef('2')
|
|
137
|
-
expect(typeof ref1).toBe('function')
|
|
138
|
-
expect(typeof ref2).toBe('function')
|
|
139
|
-
expect(ref1).not.toBe(ref2)
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
it('itemRef sets accessibility attributes on elements', async () => {
|
|
143
|
-
const { useSortable } = await import('../use-sortable')
|
|
144
|
-
const items = signal([{ id: '1' }])
|
|
145
|
-
|
|
146
|
-
const { itemRef } = useSortable({
|
|
147
|
-
items,
|
|
148
|
-
by: (item) => item.id,
|
|
149
|
-
onReorder: () => {},
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
const el = document.createElement('div')
|
|
153
|
-
itemRef('1')(el)
|
|
154
|
-
|
|
155
|
-
expect(el.getAttribute('role')).toBe('listitem')
|
|
156
|
-
expect(el.getAttribute('aria-roledescription')).toBe('sortable item')
|
|
157
|
-
expect(el.getAttribute('tabindex')).toBe('0')
|
|
158
|
-
expect(el.dataset.pyreonSortKey).toBe('1')
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
it('containerRef and itemRef accept null without throwing (Pyreon RefProp contract)', async () => {
|
|
162
|
-
// Regression: Pyreon's runtime invokes ref callbacks with `el | null`
|
|
163
|
-
// — `null` on unmount. Previously the exported types were
|
|
164
|
-
// `(el: HTMLElement) => void` (non-null), forcing every consumer to
|
|
165
|
-
// wrap with adapter callbacks. The hook accepts `null`; on `null`
|
|
166
|
-
// the per-key pdnd registration is disposed eagerly (F3 fix) rather
|
|
167
|
-
// than deferred to component `onCleanup`.
|
|
168
|
-
const { useSortable } = await import('../use-sortable')
|
|
169
|
-
const items = signal([{ id: '1' }, { id: '2' }])
|
|
170
|
-
|
|
171
|
-
const { containerRef, itemRef } = useSortable({
|
|
172
|
-
items,
|
|
173
|
-
by: (item) => item.id,
|
|
174
|
-
onReorder: () => {},
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
expect(() => containerRef(null)).not.toThrow()
|
|
178
|
-
expect(() => itemRef('1')(null)).not.toThrow()
|
|
179
|
-
|
|
180
|
-
// Sanity: a real element still wires up the ARIA attributes after a
|
|
181
|
-
// prior null call (idempotent — the null path didn't poison state).
|
|
182
|
-
const el = document.createElement('div')
|
|
183
|
-
itemRef('1')(el)
|
|
184
|
-
expect(el.getAttribute('role')).toBe('listitem')
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
it('does not override existing tabindex', async () => {
|
|
188
|
-
const { useSortable } = await import('../use-sortable')
|
|
189
|
-
const items = signal([{ id: '1' }])
|
|
190
|
-
|
|
191
|
-
const { itemRef } = useSortable({
|
|
192
|
-
items,
|
|
193
|
-
by: (item) => item.id,
|
|
194
|
-
onReorder: () => {},
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
const el = document.createElement('div')
|
|
198
|
-
el.setAttribute('tabindex', '-1')
|
|
199
|
-
itemRef('1')(el)
|
|
200
|
-
|
|
201
|
-
expect(el.getAttribute('tabindex')).toBe('-1')
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
// F3 (per-item registration leak) is regression-tested in the isolated
|
|
205
|
-
// `use-sortable-leak.test.ts` (pdnd mocked there — the real lib uses
|
|
206
|
-
// document-level listeners with no robust per-element DOM observable).
|
|
207
|
-
|
|
208
|
-
it('supports horizontal axis', async () => {
|
|
209
|
-
const { useSortable } = await import('../use-sortable')
|
|
210
|
-
const items = signal([{ id: '1' }, { id: '2' }])
|
|
211
|
-
|
|
212
|
-
const result = useSortable({
|
|
213
|
-
items,
|
|
214
|
-
by: (item) => item.id,
|
|
215
|
-
onReorder: () => {},
|
|
216
|
-
axis: 'horizontal',
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
expect(result.activeId()).toBeNull()
|
|
220
|
-
})
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
// ─── useFileDrop ────────────────────────────────────────────────────────────
|
|
224
|
-
|
|
225
|
-
describe('useFileDrop', () => {
|
|
226
|
-
it('exports useFileDrop', async () => {
|
|
227
|
-
const { useFileDrop } = await import('../index')
|
|
228
|
-
expect(typeof useFileDrop).toBe('function')
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
it('returns isOver and isDraggingFiles signals', async () => {
|
|
232
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
233
|
-
const el = document.createElement('div')
|
|
234
|
-
const { isOver, isDraggingFiles } = useFileDrop({
|
|
235
|
-
element: () => el,
|
|
236
|
-
onDrop: () => {},
|
|
237
|
-
})
|
|
238
|
-
expect(isOver()).toBe(false)
|
|
239
|
-
expect(isDraggingFiles()).toBe(false)
|
|
240
|
-
})
|
|
241
|
-
|
|
242
|
-
it('accepts accept filter and maxFiles', async () => {
|
|
243
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
244
|
-
const el = document.createElement('div')
|
|
245
|
-
const { isOver } = useFileDrop({
|
|
246
|
-
element: () => el,
|
|
247
|
-
accept: ['image/*', '.pdf'],
|
|
248
|
-
maxFiles: 5,
|
|
249
|
-
onDrop: () => {},
|
|
250
|
-
})
|
|
251
|
-
expect(isOver()).toBe(false)
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
it('accepts disabled option', async () => {
|
|
255
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
256
|
-
const el = document.createElement('div')
|
|
257
|
-
const disabled = signal(true)
|
|
258
|
-
const { isOver } = useFileDrop({
|
|
259
|
-
element: () => el,
|
|
260
|
-
disabled,
|
|
261
|
-
onDrop: () => {},
|
|
262
|
-
})
|
|
263
|
-
expect(isOver()).toBe(false)
|
|
264
|
-
})
|
|
265
|
-
})
|
|
266
|
-
|
|
267
|
-
// ─── useDragMonitor ─────────────────────────────────────────────────────────
|
|
268
|
-
|
|
269
|
-
describe('useDragMonitor', () => {
|
|
270
|
-
it('exports useDragMonitor', async () => {
|
|
271
|
-
const { useDragMonitor } = await import('../index')
|
|
272
|
-
expect(typeof useDragMonitor).toBe('function')
|
|
273
|
-
})
|
|
274
|
-
|
|
275
|
-
it('returns isDragging and dragData signals', async () => {
|
|
276
|
-
const { useDragMonitor } = await import('../use-drag-monitor')
|
|
277
|
-
const { isDragging, dragData } = useDragMonitor()
|
|
278
|
-
expect(isDragging()).toBe(false)
|
|
279
|
-
expect(dragData()).toBeNull()
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
it('accepts canMonitor filter', async () => {
|
|
283
|
-
const { useDragMonitor } = await import('../use-drag-monitor')
|
|
284
|
-
const { isDragging } = useDragMonitor({
|
|
285
|
-
canMonitor: (data) => data.type === 'card',
|
|
286
|
-
})
|
|
287
|
-
expect(isDragging()).toBe(false)
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
it('accepts onDragStart and onDrop callbacks', async () => {
|
|
291
|
-
const { useDragMonitor } = await import('../use-drag-monitor')
|
|
292
|
-
const { isDragging } = useDragMonitor({
|
|
293
|
-
onDragStart: () => {},
|
|
294
|
-
onDrop: () => {},
|
|
295
|
-
})
|
|
296
|
-
expect(isDragging()).toBe(false)
|
|
297
|
-
})
|
|
298
|
-
})
|
|
299
|
-
|
|
300
|
-
// ─── useSortable: containerRef and auto-scroll ────────────────────────────
|
|
301
|
-
|
|
302
|
-
describe('useSortable containerRef', () => {
|
|
303
|
-
it('containerRef registers auto-scroll and drop target on element', async () => {
|
|
304
|
-
const { useSortable } = await import('../use-sortable')
|
|
305
|
-
const items = signal([{ id: '1' }, { id: '2' }, { id: '3' }])
|
|
306
|
-
|
|
307
|
-
const { containerRef } = useSortable({
|
|
308
|
-
items,
|
|
309
|
-
by: (item) => item.id,
|
|
310
|
-
onReorder: () => {},
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
const container = document.createElement('ul')
|
|
314
|
-
// Should not throw — sets up autoScrollForElements + dropTargetForElements
|
|
315
|
-
expect(() => containerRef(container)).not.toThrow()
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
it('containerRef adds keydown event listener to element', async () => {
|
|
319
|
-
const { useSortable } = await import('../use-sortable')
|
|
320
|
-
const items = signal([{ id: '1' }, { id: '2' }])
|
|
321
|
-
|
|
322
|
-
const { containerRef } = useSortable({
|
|
323
|
-
items,
|
|
324
|
-
by: (item) => item.id,
|
|
325
|
-
onReorder: () => {},
|
|
326
|
-
})
|
|
327
|
-
|
|
328
|
-
const container = document.createElement('ul')
|
|
329
|
-
const spy = vi.spyOn(container, 'addEventListener')
|
|
330
|
-
containerRef(container)
|
|
331
|
-
|
|
332
|
-
expect(spy).toHaveBeenCalledWith('keydown', expect.any(Function))
|
|
333
|
-
spy.mockRestore()
|
|
334
|
-
})
|
|
335
|
-
})
|
|
336
|
-
|
|
337
|
-
// ─── useSortable: overEdge signal ─────────────────────────────────────────
|
|
338
|
-
|
|
339
|
-
describe('useSortable overEdge', () => {
|
|
340
|
-
it('overEdge signal is initially null', async () => {
|
|
341
|
-
const { useSortable } = await import('../use-sortable')
|
|
342
|
-
const items = signal([{ id: '1' }, { id: '2' }])
|
|
343
|
-
|
|
344
|
-
const { overEdge } = useSortable({
|
|
345
|
-
items,
|
|
346
|
-
by: (item) => item.id,
|
|
347
|
-
onReorder: () => {},
|
|
348
|
-
})
|
|
349
|
-
|
|
350
|
-
expect(overEdge()).toBeNull()
|
|
351
|
-
})
|
|
352
|
-
|
|
353
|
-
it('overEdge is part of the returned API alongside activeId and overId', async () => {
|
|
354
|
-
const { useSortable } = await import('../use-sortable')
|
|
355
|
-
const items = signal([{ id: 'a' }, { id: 'b' }])
|
|
356
|
-
|
|
357
|
-
const result = useSortable({
|
|
358
|
-
items,
|
|
359
|
-
by: (item) => item.id,
|
|
360
|
-
onReorder: () => {},
|
|
361
|
-
})
|
|
362
|
-
|
|
363
|
-
expect(result).toHaveProperty('overEdge')
|
|
364
|
-
expect(result).toHaveProperty('activeId')
|
|
365
|
-
expect(result).toHaveProperty('overId')
|
|
366
|
-
expect(result).toHaveProperty('containerRef')
|
|
367
|
-
expect(result).toHaveProperty('itemRef')
|
|
368
|
-
})
|
|
369
|
-
})
|
|
370
|
-
|
|
371
|
-
// ─── useSortable: keyboard handler ─────────────────────────────────────────
|
|
372
|
-
|
|
373
|
-
describe('useSortable keyboard reordering', () => {
|
|
374
|
-
it('Alt+ArrowDown swaps focused item with next (vertical axis)', async () => {
|
|
375
|
-
const { useSortable } = await import('../use-sortable')
|
|
376
|
-
const reordered: { id: string }[][] = []
|
|
377
|
-
const items = signal([{ id: '1' }, { id: '2' }, { id: '3' }])
|
|
378
|
-
|
|
379
|
-
const { containerRef, itemRef } = useSortable({
|
|
380
|
-
items,
|
|
381
|
-
by: (item) => item.id,
|
|
382
|
-
onReorder: (newItems) => reordered.push(newItems),
|
|
383
|
-
})
|
|
384
|
-
|
|
385
|
-
// Build a container with items
|
|
386
|
-
const container = document.createElement('ul')
|
|
387
|
-
document.body.appendChild(container)
|
|
388
|
-
containerRef(container)
|
|
389
|
-
|
|
390
|
-
const li1 = document.createElement('li')
|
|
391
|
-
const li2 = document.createElement('li')
|
|
392
|
-
const li3 = document.createElement('li')
|
|
393
|
-
itemRef('1')(li1)
|
|
394
|
-
itemRef('2')(li2)
|
|
395
|
-
itemRef('3')(li3)
|
|
396
|
-
container.appendChild(li1)
|
|
397
|
-
container.appendChild(li2)
|
|
398
|
-
container.appendChild(li3)
|
|
399
|
-
|
|
400
|
-
// Focus the first item
|
|
401
|
-
li1.focus()
|
|
402
|
-
|
|
403
|
-
// Dispatch Alt+ArrowDown
|
|
404
|
-
const event = new KeyboardEvent('keydown', {
|
|
405
|
-
key: 'ArrowDown',
|
|
406
|
-
altKey: true,
|
|
407
|
-
bubbles: true,
|
|
408
|
-
})
|
|
409
|
-
container.dispatchEvent(event)
|
|
410
|
-
|
|
411
|
-
expect(reordered.length).toBe(1)
|
|
412
|
-
expect(reordered[0]!.map((i) => i.id)).toEqual(['2', '1', '3'])
|
|
413
|
-
|
|
414
|
-
document.body.removeChild(container)
|
|
415
|
-
})
|
|
416
|
-
|
|
417
|
-
it('Alt+ArrowUp swaps focused item with previous (vertical axis)', async () => {
|
|
418
|
-
const { useSortable } = await import('../use-sortable')
|
|
419
|
-
const reordered: { id: string }[][] = []
|
|
420
|
-
const items = signal([{ id: '1' }, { id: '2' }, { id: '3' }])
|
|
421
|
-
|
|
422
|
-
const { containerRef, itemRef } = useSortable({
|
|
423
|
-
items,
|
|
424
|
-
by: (item) => item.id,
|
|
425
|
-
onReorder: (newItems) => reordered.push(newItems),
|
|
426
|
-
})
|
|
427
|
-
|
|
428
|
-
const container = document.createElement('ul')
|
|
429
|
-
document.body.appendChild(container)
|
|
430
|
-
containerRef(container)
|
|
431
|
-
|
|
432
|
-
const li1 = document.createElement('li')
|
|
433
|
-
const li2 = document.createElement('li')
|
|
434
|
-
const li3 = document.createElement('li')
|
|
435
|
-
itemRef('1')(li1)
|
|
436
|
-
itemRef('2')(li2)
|
|
437
|
-
itemRef('3')(li3)
|
|
438
|
-
container.appendChild(li1)
|
|
439
|
-
container.appendChild(li2)
|
|
440
|
-
container.appendChild(li3)
|
|
441
|
-
|
|
442
|
-
// Focus the second item
|
|
443
|
-
li2.focus()
|
|
444
|
-
|
|
445
|
-
const event = new KeyboardEvent('keydown', {
|
|
446
|
-
key: 'ArrowUp',
|
|
447
|
-
altKey: true,
|
|
448
|
-
bubbles: true,
|
|
449
|
-
})
|
|
450
|
-
container.dispatchEvent(event)
|
|
451
|
-
|
|
452
|
-
expect(reordered.length).toBe(1)
|
|
453
|
-
expect(reordered[0]!.map((i) => i.id)).toEqual(['2', '1', '3'])
|
|
454
|
-
|
|
455
|
-
document.body.removeChild(container)
|
|
456
|
-
})
|
|
457
|
-
|
|
458
|
-
it('ignores keyboard events without Alt key', async () => {
|
|
459
|
-
const { useSortable } = await import('../use-sortable')
|
|
460
|
-
const reordered: { id: string }[][] = []
|
|
461
|
-
const items = signal([{ id: '1' }, { id: '2' }])
|
|
462
|
-
|
|
463
|
-
const { containerRef, itemRef } = useSortable({
|
|
464
|
-
items,
|
|
465
|
-
by: (item) => item.id,
|
|
466
|
-
onReorder: (newItems) => reordered.push(newItems),
|
|
467
|
-
})
|
|
468
|
-
|
|
469
|
-
const container = document.createElement('ul')
|
|
470
|
-
document.body.appendChild(container)
|
|
471
|
-
containerRef(container)
|
|
472
|
-
|
|
473
|
-
const li1 = document.createElement('li')
|
|
474
|
-
const li2 = document.createElement('li')
|
|
475
|
-
itemRef('1')(li1)
|
|
476
|
-
itemRef('2')(li2)
|
|
477
|
-
container.appendChild(li1)
|
|
478
|
-
container.appendChild(li2)
|
|
479
|
-
|
|
480
|
-
li1.focus()
|
|
481
|
-
|
|
482
|
-
const event = new KeyboardEvent('keydown', {
|
|
483
|
-
key: 'ArrowDown',
|
|
484
|
-
altKey: false,
|
|
485
|
-
bubbles: true,
|
|
486
|
-
})
|
|
487
|
-
container.dispatchEvent(event)
|
|
488
|
-
|
|
489
|
-
expect(reordered.length).toBe(0)
|
|
490
|
-
|
|
491
|
-
document.body.removeChild(container)
|
|
492
|
-
})
|
|
493
|
-
|
|
494
|
-
it('ignores Alt+ArrowDown at the last item (boundary)', async () => {
|
|
495
|
-
const { useSortable } = await import('../use-sortable')
|
|
496
|
-
const reordered: { id: string }[][] = []
|
|
497
|
-
const items = signal([{ id: '1' }, { id: '2' }])
|
|
498
|
-
|
|
499
|
-
const { containerRef, itemRef } = useSortable({
|
|
500
|
-
items,
|
|
501
|
-
by: (item) => item.id,
|
|
502
|
-
onReorder: (newItems) => reordered.push(newItems),
|
|
503
|
-
})
|
|
504
|
-
|
|
505
|
-
const container = document.createElement('ul')
|
|
506
|
-
document.body.appendChild(container)
|
|
507
|
-
containerRef(container)
|
|
508
|
-
|
|
509
|
-
const li1 = document.createElement('li')
|
|
510
|
-
const li2 = document.createElement('li')
|
|
511
|
-
itemRef('1')(li1)
|
|
512
|
-
itemRef('2')(li2)
|
|
513
|
-
container.appendChild(li1)
|
|
514
|
-
container.appendChild(li2)
|
|
515
|
-
|
|
516
|
-
// Focus last item
|
|
517
|
-
li2.focus()
|
|
518
|
-
|
|
519
|
-
const event = new KeyboardEvent('keydown', {
|
|
520
|
-
key: 'ArrowDown',
|
|
521
|
-
altKey: true,
|
|
522
|
-
bubbles: true,
|
|
523
|
-
})
|
|
524
|
-
container.dispatchEvent(event)
|
|
525
|
-
|
|
526
|
-
expect(reordered.length).toBe(0) // no reorder at boundary
|
|
527
|
-
|
|
528
|
-
document.body.removeChild(container)
|
|
529
|
-
})
|
|
530
|
-
|
|
531
|
-
it('horizontal axis uses ArrowLeft/ArrowRight', async () => {
|
|
532
|
-
const { useSortable } = await import('../use-sortable')
|
|
533
|
-
const reordered: { id: string }[][] = []
|
|
534
|
-
const items = signal([{ id: '1' }, { id: '2' }, { id: '3' }])
|
|
535
|
-
|
|
536
|
-
const { containerRef, itemRef } = useSortable({
|
|
537
|
-
items,
|
|
538
|
-
by: (item) => item.id,
|
|
539
|
-
onReorder: (newItems) => reordered.push(newItems),
|
|
540
|
-
axis: 'horizontal',
|
|
541
|
-
})
|
|
542
|
-
|
|
543
|
-
const container = document.createElement('div')
|
|
544
|
-
document.body.appendChild(container)
|
|
545
|
-
containerRef(container)
|
|
546
|
-
|
|
547
|
-
const el1 = document.createElement('div')
|
|
548
|
-
const el2 = document.createElement('div')
|
|
549
|
-
const el3 = document.createElement('div')
|
|
550
|
-
itemRef('1')(el1)
|
|
551
|
-
itemRef('2')(el2)
|
|
552
|
-
itemRef('3')(el3)
|
|
553
|
-
container.appendChild(el1)
|
|
554
|
-
container.appendChild(el2)
|
|
555
|
-
container.appendChild(el3)
|
|
556
|
-
|
|
557
|
-
el1.focus()
|
|
558
|
-
|
|
559
|
-
// ArrowDown should be ignored in horizontal mode
|
|
560
|
-
container.dispatchEvent(
|
|
561
|
-
new KeyboardEvent('keydown', { key: 'ArrowDown', altKey: true, bubbles: true }),
|
|
562
|
-
)
|
|
563
|
-
expect(reordered.length).toBe(0)
|
|
564
|
-
|
|
565
|
-
// ArrowRight should work
|
|
566
|
-
container.dispatchEvent(
|
|
567
|
-
new KeyboardEvent('keydown', { key: 'ArrowRight', altKey: true, bubbles: true }),
|
|
568
|
-
)
|
|
569
|
-
expect(reordered.length).toBe(1)
|
|
570
|
-
expect(reordered[0]!.map((i) => i.id)).toEqual(['2', '1', '3'])
|
|
571
|
-
|
|
572
|
-
document.body.removeChild(container)
|
|
573
|
-
})
|
|
574
|
-
})
|
|
575
|
-
|
|
576
|
-
// ─── useFileDrop: MIME type filtering ──────────────────────────────────────
|
|
577
|
-
|
|
578
|
-
describe('useFileDrop MIME filtering logic', () => {
|
|
579
|
-
it('matchesAccept handles extension patterns (.pdf)', async () => {
|
|
580
|
-
// We can't test matchesAccept directly since it's private,
|
|
581
|
-
// but we can verify the useFileDrop options are accepted
|
|
582
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
583
|
-
const el = document.createElement('div')
|
|
584
|
-
const { isOver } = useFileDrop({
|
|
585
|
-
element: () => el,
|
|
586
|
-
accept: ['.pdf', '.doc', '.docx'],
|
|
587
|
-
onDrop: () => {},
|
|
588
|
-
})
|
|
589
|
-
expect(isOver()).toBe(false)
|
|
590
|
-
})
|
|
591
|
-
|
|
592
|
-
it('matchesAccept handles wildcard MIME types (image/*)', async () => {
|
|
593
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
594
|
-
const el = document.createElement('div')
|
|
595
|
-
const { isOver } = useFileDrop({
|
|
596
|
-
element: () => el,
|
|
597
|
-
accept: ['image/*'],
|
|
598
|
-
onDrop: () => {},
|
|
599
|
-
})
|
|
600
|
-
expect(isOver()).toBe(false)
|
|
601
|
-
})
|
|
602
|
-
|
|
603
|
-
it('matchesAccept handles exact MIME types (application/json)', async () => {
|
|
604
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
605
|
-
const el = document.createElement('div')
|
|
606
|
-
const { isOver } = useFileDrop({
|
|
607
|
-
element: () => el,
|
|
608
|
-
accept: ['application/json', 'text/plain'],
|
|
609
|
-
onDrop: () => {},
|
|
610
|
-
})
|
|
611
|
-
expect(isOver()).toBe(false)
|
|
612
|
-
})
|
|
613
|
-
|
|
614
|
-
it('handles maxFiles option', async () => {
|
|
615
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
616
|
-
const el = document.createElement('div')
|
|
617
|
-
const { isOver } = useFileDrop({
|
|
618
|
-
element: () => el,
|
|
619
|
-
accept: ['image/*'],
|
|
620
|
-
maxFiles: 1,
|
|
621
|
-
onDrop: () => {},
|
|
622
|
-
})
|
|
623
|
-
expect(isOver()).toBe(false)
|
|
624
|
-
})
|
|
625
|
-
|
|
626
|
-
it('handles null element gracefully', async () => {
|
|
627
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
628
|
-
const { isOver, isDraggingFiles } = useFileDrop({
|
|
629
|
-
element: () => null,
|
|
630
|
-
onDrop: () => {},
|
|
631
|
-
})
|
|
632
|
-
expect(isOver()).toBe(false)
|
|
633
|
-
expect(isDraggingFiles()).toBe(false)
|
|
634
|
-
})
|
|
635
|
-
|
|
636
|
-
it('accepts disabled as reactive getter', async () => {
|
|
637
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
638
|
-
const el = document.createElement('div')
|
|
639
|
-
const isDisabled = signal(false)
|
|
640
|
-
const { isOver } = useFileDrop({
|
|
641
|
-
element: () => el,
|
|
642
|
-
disabled: isDisabled,
|
|
643
|
-
onDrop: () => {},
|
|
644
|
-
})
|
|
645
|
-
expect(isOver()).toBe(false)
|
|
646
|
-
})
|
|
647
|
-
})
|
|
648
|
-
|
|
649
|
-
// ─── useDragMonitor: canMonitor filter ─────────────────────────────────────
|
|
650
|
-
|
|
651
|
-
describe('useDragMonitor canMonitor filter', () => {
|
|
652
|
-
it('canMonitor option is a function that receives drag data', async () => {
|
|
653
|
-
const { useDragMonitor } = await import('../use-drag-monitor')
|
|
654
|
-
const canMonitorCalls: unknown[] = []
|
|
655
|
-
|
|
656
|
-
const { isDragging } = useDragMonitor({
|
|
657
|
-
canMonitor: (data) => {
|
|
658
|
-
canMonitorCalls.push(data)
|
|
659
|
-
return data.type === 'card'
|
|
660
|
-
},
|
|
661
|
-
})
|
|
662
|
-
|
|
663
|
-
expect(isDragging()).toBe(false)
|
|
664
|
-
// canMonitor is passed to the underlying pragmatic-dnd monitor
|
|
665
|
-
// We can't trigger real drag events, but verify setup doesn't throw
|
|
666
|
-
})
|
|
667
|
-
|
|
668
|
-
it('creates monitor without canMonitor (monitors all drags)', async () => {
|
|
669
|
-
const { useDragMonitor } = await import('../use-drag-monitor')
|
|
670
|
-
const { isDragging, dragData } = useDragMonitor()
|
|
671
|
-
|
|
672
|
-
expect(isDragging()).toBe(false)
|
|
673
|
-
expect(dragData()).toBeNull()
|
|
674
|
-
})
|
|
675
|
-
|
|
676
|
-
it('creates monitor with all options', async () => {
|
|
677
|
-
const { useDragMonitor } = await import('../use-drag-monitor')
|
|
678
|
-
const starts: unknown[] = []
|
|
679
|
-
const drops: unknown[] = []
|
|
680
|
-
|
|
681
|
-
const { isDragging } = useDragMonitor({
|
|
682
|
-
canMonitor: (data) => data.type === 'task',
|
|
683
|
-
onDragStart: (data) => starts.push(data),
|
|
684
|
-
onDrop: (source, target) => drops.push({ source, target }),
|
|
685
|
-
})
|
|
686
|
-
|
|
687
|
-
expect(isDragging()).toBe(false)
|
|
688
|
-
// Callbacks are registered — verify no errors on setup
|
|
689
|
-
})
|
|
690
|
-
})
|
|
691
|
-
|
|
692
|
-
// ─── SSR safety ────────────────────────────────────────────────────────────
|
|
693
|
-
|
|
694
|
-
describe('SSR safety — all hooks return inert results without document', () => {
|
|
695
|
-
let originalDocument: typeof globalThis.document
|
|
696
|
-
|
|
697
|
-
beforeEach(() => {
|
|
698
|
-
originalDocument = globalThis.document
|
|
699
|
-
// @ts-expect-error — simulate SSR by removing document
|
|
700
|
-
delete globalThis.document
|
|
701
|
-
})
|
|
702
|
-
|
|
703
|
-
afterEach(() => {
|
|
704
|
-
globalThis.document = originalDocument
|
|
705
|
-
})
|
|
706
|
-
|
|
707
|
-
it('useDraggable returns inert isDragging', async () => {
|
|
708
|
-
const { useDraggable } = await import('../use-draggable')
|
|
709
|
-
const result = useDraggable({ element: () => null, data: { id: '1' } })
|
|
710
|
-
expect(result.isDragging()).toBe(false)
|
|
711
|
-
})
|
|
712
|
-
|
|
713
|
-
it('useDroppable returns inert isOver', async () => {
|
|
714
|
-
const { useDroppable } = await import('../use-droppable')
|
|
715
|
-
const result = useDroppable({ element: () => null, onDrop: () => {} })
|
|
716
|
-
expect(result.isOver()).toBe(false)
|
|
717
|
-
})
|
|
718
|
-
|
|
719
|
-
it('useSortable returns full inert API', async () => {
|
|
720
|
-
const { useSortable } = await import('../use-sortable')
|
|
721
|
-
const result = useSortable({
|
|
722
|
-
items: () => [{ id: '1' }],
|
|
723
|
-
by: (item: { id: string }) => item.id,
|
|
724
|
-
onReorder: () => {},
|
|
725
|
-
})
|
|
726
|
-
expect(result.activeId()).toBeNull()
|
|
727
|
-
expect(result.overId()).toBeNull()
|
|
728
|
-
expect(result.overEdge()).toBeNull()
|
|
729
|
-
expect(typeof result.containerRef).toBe('function')
|
|
730
|
-
expect(typeof result.itemRef).toBe('function')
|
|
731
|
-
expect(typeof result.itemRef('1')).toBe('function')
|
|
732
|
-
})
|
|
733
|
-
|
|
734
|
-
it('useFileDrop returns inert signals', async () => {
|
|
735
|
-
const { useFileDrop } = await import('../use-file-drop')
|
|
736
|
-
const result = useFileDrop({ element: () => null, onDrop: () => {} })
|
|
737
|
-
expect(result.isOver()).toBe(false)
|
|
738
|
-
expect(result.isDraggingFiles()).toBe(false)
|
|
739
|
-
})
|
|
740
|
-
|
|
741
|
-
it('useDragMonitor returns inert signals', async () => {
|
|
742
|
-
const { useDragMonitor } = await import('../use-drag-monitor')
|
|
743
|
-
const result = useDragMonitor()
|
|
744
|
-
expect(result.isDragging()).toBe(false)
|
|
745
|
-
expect(result.dragData()).toBeNull()
|
|
746
|
-
})
|
|
747
|
-
})
|
|
748
|
-
|
|
749
|
-
// ─── Module exports ─────────────────────────────────────────────────────────
|
|
750
|
-
|
|
751
|
-
describe('module exports', () => {
|
|
752
|
-
it('exports all 5 hooks', async () => {
|
|
753
|
-
const mod = await import('../index')
|
|
754
|
-
expect(mod.useDraggable).toBeDefined()
|
|
755
|
-
expect(mod.useDroppable).toBeDefined()
|
|
756
|
-
expect(mod.useSortable).toBeDefined()
|
|
757
|
-
expect(mod.useFileDrop).toBeDefined()
|
|
758
|
-
expect(mod.useDragMonitor).toBeDefined()
|
|
759
|
-
})
|
|
760
|
-
})
|