@pyreon/dnd 0.12.8 → 0.12.10

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.
@@ -0,0 +1,1662 @@
1
+ import { signal } from '@pyreon/reactivity'
2
+
3
+ // ─── Mock pragmatic-drag-and-drop ──────────────────────────────────────────
4
+
5
+ // Captured callback registrations for testing
6
+ let lastDraggableOpts: any = null
7
+ let lastDropTargetOpts: any = null
8
+ let lastMonitorOpts: any = null
9
+ let lastExternalDropOpts: any = null
10
+ let lastExternalMonitorOpts: any = null
11
+ const allDraggableOpts: any[] = []
12
+ const allDropTargetOpts: any[] = []
13
+
14
+ vi.mock('@atlaskit/pragmatic-drag-and-drop/element/adapter', () => ({
15
+ draggable: (opts: any) => {
16
+ lastDraggableOpts = opts
17
+ allDraggableOpts.push(opts)
18
+ return () => {} // cleanup
19
+ },
20
+ dropTargetForElements: (opts: any) => {
21
+ lastDropTargetOpts = opts
22
+ allDropTargetOpts.push(opts)
23
+ return () => {} // cleanup
24
+ },
25
+ monitorForElements: (opts: any) => {
26
+ lastMonitorOpts = opts
27
+ return () => {} // cleanup
28
+ },
29
+ }))
30
+
31
+ vi.mock('@atlaskit/pragmatic-drag-and-drop/external/adapter', () => ({
32
+ dropTargetForExternal: (opts: any) => {
33
+ lastExternalDropOpts = opts
34
+ return () => {} // cleanup
35
+ },
36
+ monitorForExternal: (opts: any) => {
37
+ lastExternalMonitorOpts = opts
38
+ return () => {} // cleanup
39
+ },
40
+ }))
41
+
42
+ vi.mock('@atlaskit/pragmatic-drag-and-drop/external/file', () => ({
43
+ containsFiles: () => true,
44
+ getFiles: ({ source }: any) => source?._files ?? [],
45
+ }))
46
+
47
+ vi.mock('@atlaskit/pragmatic-drag-and-drop/combine', () => ({
48
+ combine: (...fns: (() => void)[]) => () => fns.forEach((fn) => fn()),
49
+ }))
50
+
51
+ let lastAutoScrollOpts: any = null
52
+ vi.mock('@atlaskit/pragmatic-drag-and-drop-auto-scroll/element', () => ({
53
+ autoScrollForElements: (opts: any) => {
54
+ lastAutoScrollOpts = opts
55
+ return () => {}
56
+ },
57
+ }))
58
+
59
+ vi.mock('@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge', () => ({
60
+ attachClosestEdge: (data: any) => data,
61
+ extractClosestEdge: (data: any) => data?.__edge ?? null,
62
+ }))
63
+
64
+ beforeEach(() => {
65
+ lastDraggableOpts = null
66
+ lastDropTargetOpts = null
67
+ lastMonitorOpts = null
68
+ lastExternalDropOpts = null
69
+ lastExternalMonitorOpts = null
70
+ lastAutoScrollOpts = null
71
+ allDraggableOpts.length = 0
72
+ allDropTargetOpts.length = 0
73
+ })
74
+
75
+ // Helper to flush microtasks
76
+ const flushMicrotasks = () => new Promise<void>((resolve) => queueMicrotask(resolve))
77
+
78
+ // ─── useDraggable integration ───────────────────────────────────────────────
79
+
80
+ describe('useDraggable integration', () => {
81
+ it('initializes with isDragging false for a real element', async () => {
82
+ const { useDraggable } = await import('../use-draggable')
83
+ const el = document.createElement('div')
84
+ document.body.appendChild(el)
85
+
86
+ const { isDragging } = useDraggable({
87
+ element: () => el,
88
+ data: { id: '1', type: 'card' },
89
+ })
90
+
91
+ expect(isDragging()).toBe(false)
92
+ el.remove()
93
+ })
94
+
95
+ it('supports drag handle option', async () => {
96
+ const { useDraggable } = await import('../use-draggable')
97
+ const el = document.createElement('div')
98
+ const handle = document.createElement('span')
99
+ el.appendChild(handle)
100
+ document.body.appendChild(el)
101
+
102
+ const { isDragging } = useDraggable({
103
+ element: () => el,
104
+ data: { id: '1' },
105
+ handle: () => handle,
106
+ })
107
+
108
+ expect(isDragging()).toBe(false)
109
+ el.remove()
110
+ })
111
+
112
+ it('supports onDragStart and onDragEnd callbacks', async () => {
113
+ const { useDraggable } = await import('../use-draggable')
114
+ const el = document.createElement('div')
115
+ document.body.appendChild(el)
116
+
117
+ const startFn = vi.fn()
118
+ const endFn = vi.fn()
119
+
120
+ const { isDragging } = useDraggable({
121
+ element: () => el,
122
+ data: { id: '1' },
123
+ onDragStart: startFn,
124
+ onDragEnd: endFn,
125
+ })
126
+
127
+ expect(isDragging()).toBe(false)
128
+ // Callbacks are registered — verify no errors during setup
129
+ el.remove()
130
+ })
131
+
132
+ it('disabled as reactive signal prevents drag', async () => {
133
+ const { useDraggable } = await import('../use-draggable')
134
+ const el = document.createElement('div')
135
+ document.body.appendChild(el)
136
+ const disabled = signal(true)
137
+
138
+ const { isDragging } = useDraggable({
139
+ element: () => el,
140
+ data: { id: '1' },
141
+ disabled,
142
+ })
143
+
144
+ expect(isDragging()).toBe(false)
145
+ disabled.set(false)
146
+ // Switching disabled does not change isDragging by itself
147
+ expect(isDragging()).toBe(false)
148
+ el.remove()
149
+ })
150
+
151
+ it('dynamic data function is accepted', async () => {
152
+ const { useDraggable } = await import('../use-draggable')
153
+ const el = document.createElement('div')
154
+ document.body.appendChild(el)
155
+ const count = signal(0)
156
+
157
+ const { isDragging } = useDraggable({
158
+ element: () => el,
159
+ data: () => ({ count: count(), type: 'counter' }),
160
+ })
161
+
162
+ expect(isDragging()).toBe(false)
163
+ count.set(5)
164
+ expect(isDragging()).toBe(false)
165
+ el.remove()
166
+ })
167
+ })
168
+
169
+ // ─── useDroppable integration ───────────────────────────────────────────────
170
+
171
+ describe('useDroppable integration', () => {
172
+ it('initializes with isOver false for a real element', async () => {
173
+ const { useDroppable } = await import('../use-droppable')
174
+ const el = document.createElement('div')
175
+ document.body.appendChild(el)
176
+
177
+ const { isOver } = useDroppable({
178
+ element: () => el,
179
+ onDrop: () => {},
180
+ })
181
+
182
+ expect(isOver()).toBe(false)
183
+ el.remove()
184
+ })
185
+
186
+ it('supports data option as static object', async () => {
187
+ const { useDroppable } = await import('../use-droppable')
188
+ const el = document.createElement('div')
189
+ document.body.appendChild(el)
190
+
191
+ const { isOver } = useDroppable({
192
+ element: () => el,
193
+ data: { zone: 'target', accepts: 'card' },
194
+ onDrop: () => {},
195
+ })
196
+
197
+ expect(isOver()).toBe(false)
198
+ el.remove()
199
+ })
200
+
201
+ it('supports data option as function', async () => {
202
+ const { useDroppable } = await import('../use-droppable')
203
+ const el = document.createElement('div')
204
+ document.body.appendChild(el)
205
+ const zoneType = signal('primary')
206
+
207
+ const { isOver } = useDroppable({
208
+ element: () => el,
209
+ data: () => ({ zone: zoneType() }),
210
+ onDrop: () => {},
211
+ })
212
+
213
+ expect(isOver()).toBe(false)
214
+ el.remove()
215
+ })
216
+
217
+ it('canDrop filter does not affect initial state', async () => {
218
+ const { useDroppable } = await import('../use-droppable')
219
+ const el = document.createElement('div')
220
+ document.body.appendChild(el)
221
+
222
+ const { isOver } = useDroppable({
223
+ element: () => el,
224
+ canDrop: (data) => data.type === 'task',
225
+ onDragEnter: () => {},
226
+ onDragLeave: () => {},
227
+ onDrop: () => {},
228
+ })
229
+
230
+ expect(isOver()).toBe(false)
231
+ el.remove()
232
+ })
233
+ })
234
+
235
+ // ─── useSortable integration ────────────────────────────────────────────────
236
+
237
+ describe('useSortable integration', () => {
238
+ it('creates a complete sortable list with items attached to DOM', async () => {
239
+ const { useSortable } = await import('../use-sortable')
240
+ const items = signal([
241
+ { id: '1', name: 'Alice' },
242
+ { id: '2', name: 'Bob' },
243
+ { id: '3', name: 'Charlie' },
244
+ ])
245
+
246
+ const reorderedResults: typeof items extends ReturnType<typeof signal<infer T>> ? T : never[] =
247
+ []
248
+
249
+ const { containerRef, itemRef, activeId, overId, overEdge } = useSortable({
250
+ items,
251
+ by: (item) => item.id,
252
+ onReorder: (newItems) => {
253
+ reorderedResults.push(...newItems)
254
+ items.set(newItems)
255
+ },
256
+ })
257
+
258
+ // Build DOM
259
+ const container = document.createElement('ul')
260
+ document.body.appendChild(container)
261
+ containerRef(container)
262
+
263
+ const elements = items().map((item) => {
264
+ const li = document.createElement('li')
265
+ li.textContent = item.name
266
+ itemRef(item.id)(li)
267
+ container.appendChild(li)
268
+ return li
269
+ })
270
+
271
+ // Verify initial state
272
+ expect(activeId()).toBeNull()
273
+ expect(overId()).toBeNull()
274
+ expect(overEdge()).toBeNull()
275
+
276
+ // Verify accessibility attributes on all items
277
+ for (let i = 0; i < elements.length; i++) {
278
+ const el = elements[i]!
279
+ expect(el.getAttribute('role')).toBe('listitem')
280
+ expect(el.getAttribute('aria-roledescription')).toBe('sortable item')
281
+ expect(el.getAttribute('tabindex')).toBe('0')
282
+ expect(el.dataset.pyreonSortKey).toBe(items()[i]!.id)
283
+ }
284
+
285
+ container.remove()
286
+ })
287
+
288
+ it('keyboard reorder: Alt+ArrowDown moves item down in vertical mode', async () => {
289
+ const { useSortable } = await import('../use-sortable')
290
+ const reordered: { id: string }[][] = []
291
+ const items = signal([{ id: 'a' }, { id: 'b' }, { id: 'c' }])
292
+
293
+ const { containerRef, itemRef } = useSortable({
294
+ items,
295
+ by: (i) => i.id,
296
+ onReorder: (newItems) => reordered.push(newItems),
297
+ })
298
+
299
+ const container = document.createElement('ul')
300
+ document.body.appendChild(container)
301
+ containerRef(container)
302
+
303
+ const els = items().map((item) => {
304
+ const li = document.createElement('li')
305
+ itemRef(item.id)(li)
306
+ container.appendChild(li)
307
+ return li
308
+ })
309
+
310
+ els[0]!.focus()
311
+
312
+ container.dispatchEvent(
313
+ new KeyboardEvent('keydown', { key: 'ArrowDown', altKey: true, bubbles: true }),
314
+ )
315
+
316
+ expect(reordered.length).toBe(1)
317
+ expect(reordered[0]!.map((i) => i.id)).toEqual(['b', 'a', 'c'])
318
+
319
+ container.remove()
320
+ })
321
+
322
+ it('keyboard reorder: Alt+ArrowRight moves item in horizontal mode', async () => {
323
+ const { useSortable } = await import('../use-sortable')
324
+ const reordered: { id: string }[][] = []
325
+ const items = signal([{ id: 'x' }, { id: 'y' }, { id: 'z' }])
326
+
327
+ const { containerRef, itemRef } = useSortable({
328
+ items,
329
+ by: (i) => i.id,
330
+ onReorder: (newItems) => reordered.push(newItems),
331
+ axis: 'horizontal',
332
+ })
333
+
334
+ const container = document.createElement('div')
335
+ document.body.appendChild(container)
336
+ containerRef(container)
337
+
338
+ const els = items().map((item) => {
339
+ const el = document.createElement('div')
340
+ itemRef(item.id)(el)
341
+ container.appendChild(el)
342
+ return el
343
+ })
344
+
345
+ els[0]!.focus()
346
+
347
+ // ArrowDown should NOT work in horizontal mode
348
+ container.dispatchEvent(
349
+ new KeyboardEvent('keydown', { key: 'ArrowDown', altKey: true, bubbles: true }),
350
+ )
351
+ expect(reordered.length).toBe(0)
352
+
353
+ // ArrowRight should work
354
+ container.dispatchEvent(
355
+ new KeyboardEvent('keydown', { key: 'ArrowRight', altKey: true, bubbles: true }),
356
+ )
357
+ expect(reordered.length).toBe(1)
358
+ expect(reordered[0]!.map((i) => i.id)).toEqual(['y', 'x', 'z'])
359
+
360
+ container.remove()
361
+ })
362
+
363
+ it('does not reorder past boundaries', async () => {
364
+ const { useSortable } = await import('../use-sortable')
365
+ const reordered: { id: string }[][] = []
366
+ const items = signal([{ id: '1' }, { id: '2' }])
367
+
368
+ const { containerRef, itemRef } = useSortable({
369
+ items,
370
+ by: (i) => i.id,
371
+ onReorder: (newItems) => reordered.push(newItems),
372
+ })
373
+
374
+ const container = document.createElement('ul')
375
+ document.body.appendChild(container)
376
+ containerRef(container)
377
+
378
+ const li1 = document.createElement('li')
379
+ const li2 = document.createElement('li')
380
+ itemRef('1')(li1)
381
+ itemRef('2')(li2)
382
+ container.appendChild(li1)
383
+ container.appendChild(li2)
384
+
385
+ // Try to move first item up (already at top)
386
+ li1.focus()
387
+ container.dispatchEvent(
388
+ new KeyboardEvent('keydown', { key: 'ArrowUp', altKey: true, bubbles: true }),
389
+ )
390
+ expect(reordered.length).toBe(0)
391
+
392
+ // Try to move last item down (already at bottom)
393
+ li2.focus()
394
+ container.dispatchEvent(
395
+ new KeyboardEvent('keydown', { key: 'ArrowDown', altKey: true, bubbles: true }),
396
+ )
397
+ expect(reordered.length).toBe(0)
398
+
399
+ container.remove()
400
+ })
401
+
402
+ it('preserves existing tabindex on items', async () => {
403
+ const { useSortable } = await import('../use-sortable')
404
+ const items = signal([{ id: '1' }])
405
+
406
+ const { itemRef } = useSortable({
407
+ items,
408
+ by: (i) => i.id,
409
+ onReorder: () => {},
410
+ })
411
+
412
+ const el = document.createElement('div')
413
+ el.setAttribute('tabindex', '-1')
414
+ itemRef('1')(el)
415
+
416
+ expect(el.getAttribute('tabindex')).toBe('-1')
417
+ })
418
+ })
419
+
420
+ // ─── useFileDrop integration ────────────────────────────────────────────────
421
+
422
+ describe('useFileDrop integration', () => {
423
+ it('initializes with both signals false for a real element', async () => {
424
+ const { useFileDrop } = await import('../use-file-drop')
425
+ const el = document.createElement('div')
426
+ document.body.appendChild(el)
427
+
428
+ const { isOver, isDraggingFiles } = useFileDrop({
429
+ element: () => el,
430
+ onDrop: () => {},
431
+ })
432
+
433
+ expect(isOver()).toBe(false)
434
+ expect(isDraggingFiles()).toBe(false)
435
+ el.remove()
436
+ })
437
+
438
+ it('accepts accept, maxFiles, and disabled options together', async () => {
439
+ const { useFileDrop } = await import('../use-file-drop')
440
+ const el = document.createElement('div')
441
+ document.body.appendChild(el)
442
+ const disabled = signal(false)
443
+
444
+ const { isOver, isDraggingFiles } = useFileDrop({
445
+ element: () => el,
446
+ accept: ['image/*', '.pdf', 'application/json'],
447
+ maxFiles: 3,
448
+ disabled,
449
+ onDrop: () => {},
450
+ })
451
+
452
+ expect(isOver()).toBe(false)
453
+ expect(isDraggingFiles()).toBe(false)
454
+ el.remove()
455
+ })
456
+
457
+ it('handles disabled as boolean', async () => {
458
+ const { useFileDrop } = await import('../use-file-drop')
459
+ const el = document.createElement('div')
460
+ document.body.appendChild(el)
461
+
462
+ const { isOver } = useFileDrop({
463
+ element: () => el,
464
+ disabled: true,
465
+ onDrop: () => {},
466
+ })
467
+
468
+ expect(isOver()).toBe(false)
469
+ el.remove()
470
+ })
471
+ })
472
+
473
+ // ─── useDragMonitor integration ─────────────────────────────────────────────
474
+
475
+ describe('useDragMonitor integration', () => {
476
+ it('creates monitor without options', async () => {
477
+ const { useDragMonitor } = await import('../use-drag-monitor')
478
+ const { isDragging, dragData } = useDragMonitor()
479
+
480
+ expect(isDragging()).toBe(false)
481
+ expect(dragData()).toBeNull()
482
+ })
483
+
484
+ it('creates monitor with full options', async () => {
485
+ const { useDragMonitor } = await import('../use-drag-monitor')
486
+ const starts: unknown[] = []
487
+ const drops: unknown[] = []
488
+
489
+ const { isDragging, dragData } = useDragMonitor({
490
+ canMonitor: (data) => data.type === 'card',
491
+ onDragStart: (data) => starts.push(data),
492
+ onDrop: (source, target) => drops.push({ source, target }),
493
+ })
494
+
495
+ expect(isDragging()).toBe(false)
496
+ expect(dragData()).toBeNull()
497
+ })
498
+ })
499
+
500
+ // ─── Combined draggable + droppable setup ───────────────────────────────────
501
+
502
+ describe('combined draggable + droppable', () => {
503
+ it('creates both on the same element without conflicts', async () => {
504
+ const { useDraggable } = await import('../use-draggable')
505
+ const { useDroppable } = await import('../use-droppable')
506
+
507
+ const el = document.createElement('div')
508
+ document.body.appendChild(el)
509
+
510
+ const { isDragging } = useDraggable({
511
+ element: () => el,
512
+ data: { id: '1', type: 'card' },
513
+ })
514
+
515
+ const { isOver } = useDroppable({
516
+ element: () => el,
517
+ canDrop: (data) => data.type === 'card',
518
+ onDrop: () => {},
519
+ })
520
+
521
+ expect(isDragging()).toBe(false)
522
+ expect(isOver()).toBe(false)
523
+ el.remove()
524
+ })
525
+
526
+ it('multiple droppable zones can coexist', async () => {
527
+ const { useDroppable } = await import('../use-droppable')
528
+ const zone1 = document.createElement('div')
529
+ const zone2 = document.createElement('div')
530
+ document.body.appendChild(zone1)
531
+ document.body.appendChild(zone2)
532
+
533
+ const drop1 = useDroppable({
534
+ element: () => zone1,
535
+ data: { zone: 'left' },
536
+ onDrop: () => {},
537
+ })
538
+
539
+ const drop2 = useDroppable({
540
+ element: () => zone2,
541
+ data: { zone: 'right' },
542
+ onDrop: () => {},
543
+ })
544
+
545
+ expect(drop1.isOver()).toBe(false)
546
+ expect(drop2.isOver()).toBe(false)
547
+ zone1.remove()
548
+ zone2.remove()
549
+ })
550
+ })
551
+
552
+ // ─── Module exports ─────────────────────────────────────────────────────────
553
+
554
+ describe('module exports completeness', () => {
555
+ it('exports all hooks and types from index', async () => {
556
+ const mod = await import('../index')
557
+ expect(typeof mod.useDraggable).toBe('function')
558
+ expect(typeof mod.useDroppable).toBe('function')
559
+ expect(typeof mod.useSortable).toBe('function')
560
+ expect(typeof mod.useFileDrop).toBe('function')
561
+ expect(typeof mod.useDragMonitor).toBe('function')
562
+ })
563
+ })
564
+
565
+ // ─── useDraggable — callback coverage ──────────────────────────────────────
566
+
567
+ describe('useDraggable — callbacks via mock', () => {
568
+ it('isDragging becomes true on onDragStart and false on onDrop', async () => {
569
+ const { useDraggable } = await import('../use-draggable')
570
+ const el = document.createElement('div')
571
+ document.body.appendChild(el)
572
+
573
+ const startFn = vi.fn()
574
+ const endFn = vi.fn()
575
+
576
+ const { isDragging } = useDraggable({
577
+ element: () => el,
578
+ data: { id: '1', type: 'card' },
579
+ onDragStart: startFn,
580
+ onDragEnd: endFn,
581
+ })
582
+
583
+ await flushMicrotasks()
584
+
585
+ expect(lastDraggableOpts).not.toBeNull()
586
+
587
+ // Trigger drag start
588
+ lastDraggableOpts.onDragStart()
589
+ expect(isDragging()).toBe(true)
590
+ expect(startFn).toHaveBeenCalledOnce()
591
+
592
+ // Trigger drop
593
+ lastDraggableOpts.onDrop()
594
+ expect(isDragging()).toBe(false)
595
+ expect(endFn).toHaveBeenCalledOnce()
596
+
597
+ el.remove()
598
+ })
599
+
600
+ it('getInitialData resolves static data', async () => {
601
+ const { useDraggable } = await import('../use-draggable')
602
+ const el = document.createElement('div')
603
+ document.body.appendChild(el)
604
+
605
+ useDraggable({
606
+ element: () => el,
607
+ data: { id: '42', type: 'item' },
608
+ })
609
+
610
+ await flushMicrotasks()
611
+
612
+ const data = lastDraggableOpts.getInitialData()
613
+ expect(data).toEqual({ id: '42', type: 'item' })
614
+
615
+ el.remove()
616
+ })
617
+
618
+ it('getInitialData resolves dynamic data function', async () => {
619
+ const { useDraggable } = await import('../use-draggable')
620
+ const el = document.createElement('div')
621
+ document.body.appendChild(el)
622
+ const count = signal(5)
623
+
624
+ useDraggable({
625
+ element: () => el,
626
+ data: () => ({ count: count(), type: 'counter' }),
627
+ })
628
+
629
+ await flushMicrotasks()
630
+
631
+ const data = lastDraggableOpts.getInitialData()
632
+ expect(data.count).toBe(5)
633
+
634
+ count.set(10)
635
+ const data2 = lastDraggableOpts.getInitialData()
636
+ expect(data2.count).toBe(10)
637
+
638
+ el.remove()
639
+ })
640
+
641
+ it('canDrag returns true when not disabled', async () => {
642
+ const { useDraggable } = await import('../use-draggable')
643
+ const el = document.createElement('div')
644
+ document.body.appendChild(el)
645
+
646
+ useDraggable({
647
+ element: () => el,
648
+ data: { id: '1' },
649
+ })
650
+
651
+ await flushMicrotasks()
652
+ expect(lastDraggableOpts.canDrag()).toBe(true)
653
+ el.remove()
654
+ })
655
+
656
+ it('canDrag returns false when disabled is true', async () => {
657
+ const { useDraggable } = await import('../use-draggable')
658
+ const el = document.createElement('div')
659
+ document.body.appendChild(el)
660
+
661
+ useDraggable({
662
+ element: () => el,
663
+ data: { id: '1' },
664
+ disabled: true,
665
+ })
666
+
667
+ await flushMicrotasks()
668
+ expect(lastDraggableOpts.canDrag()).toBe(false)
669
+ el.remove()
670
+ })
671
+
672
+ it('canDrag uses reactive disabled function', async () => {
673
+ const { useDraggable } = await import('../use-draggable')
674
+ const el = document.createElement('div')
675
+ document.body.appendChild(el)
676
+ const disabled = signal(false)
677
+
678
+ useDraggable({
679
+ element: () => el,
680
+ data: { id: '1' },
681
+ disabled,
682
+ })
683
+
684
+ await flushMicrotasks()
685
+ expect(lastDraggableOpts.canDrag()).toBe(true)
686
+
687
+ disabled.set(true)
688
+ expect(lastDraggableOpts.canDrag()).toBe(false)
689
+
690
+ el.remove()
691
+ })
692
+
693
+ it('setup does nothing when element is null', async () => {
694
+ const { useDraggable } = await import('../use-draggable')
695
+
696
+ lastDraggableOpts = null
697
+ useDraggable({
698
+ element: () => null,
699
+ data: { id: '1' },
700
+ })
701
+
702
+ await flushMicrotasks()
703
+ // Should not have registered with pragmatic-dnd
704
+ expect(lastDraggableOpts).toBeNull()
705
+ })
706
+ })
707
+
708
+ // ─── useDroppable — callback coverage ──────────────────────────────────────
709
+
710
+ describe('useDroppable — callbacks via mock', () => {
711
+ it('isOver changes on drag enter/leave/drop', async () => {
712
+ const { useDroppable } = await import('../use-droppable')
713
+ const el = document.createElement('div')
714
+ document.body.appendChild(el)
715
+
716
+ const onDrop = vi.fn()
717
+ const onDragEnter = vi.fn()
718
+ const onDragLeave = vi.fn()
719
+
720
+ const { isOver } = useDroppable({
721
+ element: () => el,
722
+ onDrop,
723
+ onDragEnter,
724
+ onDragLeave,
725
+ })
726
+
727
+ await flushMicrotasks()
728
+ expect(lastDropTargetOpts).not.toBeNull()
729
+
730
+ // Drag enter
731
+ lastDropTargetOpts.onDragEnter({ source: { data: { type: 'card' } } })
732
+ expect(isOver()).toBe(true)
733
+ expect(onDragEnter).toHaveBeenCalledOnce()
734
+
735
+ // Drag leave
736
+ lastDropTargetOpts.onDragLeave()
737
+ expect(isOver()).toBe(false)
738
+ expect(onDragLeave).toHaveBeenCalledOnce()
739
+
740
+ // Drop
741
+ lastDropTargetOpts.onDragEnter({ source: { data: { type: 'card' } } })
742
+ lastDropTargetOpts.onDrop({ source: { data: { type: 'card' } } })
743
+ expect(isOver()).toBe(false)
744
+ expect(onDrop).toHaveBeenCalledOnce()
745
+
746
+ el.remove()
747
+ })
748
+
749
+ it('getData returns static data', async () => {
750
+ const { useDroppable } = await import('../use-droppable')
751
+ const el = document.createElement('div')
752
+ document.body.appendChild(el)
753
+
754
+ useDroppable({
755
+ element: () => el,
756
+ data: { zone: 'target' },
757
+ onDrop: () => {},
758
+ })
759
+
760
+ await flushMicrotasks()
761
+ expect(lastDropTargetOpts.getData()).toEqual({ zone: 'target' })
762
+ el.remove()
763
+ })
764
+
765
+ it('getData returns dynamic data', async () => {
766
+ const { useDroppable } = await import('../use-droppable')
767
+ const el = document.createElement('div')
768
+ document.body.appendChild(el)
769
+ const zone = signal('a')
770
+
771
+ useDroppable({
772
+ element: () => el,
773
+ data: () => ({ zone: zone() }),
774
+ onDrop: () => {},
775
+ })
776
+
777
+ await flushMicrotasks()
778
+ expect(lastDropTargetOpts.getData()).toEqual({ zone: 'a' })
779
+ zone.set('b')
780
+ expect(lastDropTargetOpts.getData()).toEqual({ zone: 'b' })
781
+ el.remove()
782
+ })
783
+
784
+ it('getData returns empty when no data option', async () => {
785
+ const { useDroppable } = await import('../use-droppable')
786
+ const el = document.createElement('div')
787
+ document.body.appendChild(el)
788
+
789
+ useDroppable({
790
+ element: () => el,
791
+ onDrop: () => {},
792
+ })
793
+
794
+ await flushMicrotasks()
795
+ expect(lastDropTargetOpts.getData()).toEqual({})
796
+ el.remove()
797
+ })
798
+
799
+ it('canDrop returns true when no filter', async () => {
800
+ const { useDroppable } = await import('../use-droppable')
801
+ const el = document.createElement('div')
802
+ document.body.appendChild(el)
803
+
804
+ useDroppable({
805
+ element: () => el,
806
+ onDrop: () => {},
807
+ })
808
+
809
+ await flushMicrotasks()
810
+ expect(lastDropTargetOpts.canDrop({ source: { data: {} } })).toBe(true)
811
+ el.remove()
812
+ })
813
+
814
+ it('canDrop uses filter function', async () => {
815
+ const { useDroppable } = await import('../use-droppable')
816
+ const el = document.createElement('div')
817
+ document.body.appendChild(el)
818
+
819
+ useDroppable({
820
+ element: () => el,
821
+ canDrop: (data) => data.type === 'allowed',
822
+ onDrop: () => {},
823
+ })
824
+
825
+ await flushMicrotasks()
826
+ expect(lastDropTargetOpts.canDrop({ source: { data: { type: 'allowed' } } })).toBe(true)
827
+ expect(lastDropTargetOpts.canDrop({ source: { data: { type: 'denied' } } })).toBe(false)
828
+ el.remove()
829
+ })
830
+
831
+ it('setup does nothing when element is null', async () => {
832
+ const { useDroppable } = await import('../use-droppable')
833
+
834
+ lastDropTargetOpts = null
835
+ useDroppable({
836
+ element: () => null,
837
+ onDrop: () => {},
838
+ })
839
+
840
+ await flushMicrotasks()
841
+ expect(lastDropTargetOpts).toBeNull()
842
+ })
843
+ })
844
+
845
+ // ─── useDragMonitor — callback coverage ────────────────────────────────────
846
+
847
+ describe('useDragMonitor — callbacks via mock', () => {
848
+ it('fires onDragStart and onDrop callbacks', async () => {
849
+ const { useDragMonitor } = await import('../use-drag-monitor')
850
+ const starts: any[] = []
851
+ const drops: any[] = []
852
+
853
+ const { isDragging, dragData } = useDragMonitor({
854
+ onDragStart: (data) => starts.push(data),
855
+ onDrop: (source, target) => drops.push({ source, target }),
856
+ })
857
+
858
+ expect(lastMonitorOpts).not.toBeNull()
859
+
860
+ // Trigger drag start
861
+ lastMonitorOpts.onDragStart({ source: { data: { id: '1' } } })
862
+ expect(isDragging()).toBe(true)
863
+ expect(dragData()).toEqual({ id: '1' })
864
+ expect(starts).toHaveLength(1)
865
+
866
+ // Trigger drop
867
+ lastMonitorOpts.onDrop({
868
+ source: { data: { id: '1' } },
869
+ location: { current: { dropTargets: [{ data: { zone: 'target' } }] } },
870
+ })
871
+ expect(isDragging()).toBe(false)
872
+ expect(dragData()).toBeNull()
873
+ expect(drops).toHaveLength(1)
874
+ expect(drops[0]!.target).toEqual({ zone: 'target' })
875
+ })
876
+
877
+ it('onDrop handles missing drop targets', async () => {
878
+ const { useDragMonitor } = await import('../use-drag-monitor')
879
+ useDragMonitor()
880
+
881
+ lastMonitorOpts.onDrop({
882
+ source: { data: { id: '1' } },
883
+ location: { current: { dropTargets: [] } },
884
+ })
885
+
886
+ // Should not throw
887
+ })
888
+
889
+ it('canMonitor filter is applied when provided', async () => {
890
+ const { useDragMonitor } = await import('../use-drag-monitor')
891
+ useDragMonitor({
892
+ canMonitor: (data) => data.type === 'card',
893
+ })
894
+
895
+ expect(lastMonitorOpts.canMonitor).toBeDefined()
896
+ expect(lastMonitorOpts.canMonitor({ source: { data: { type: 'card' } } })).toBe(true)
897
+ expect(lastMonitorOpts.canMonitor({ source: { data: { type: 'other' } } })).toBe(false)
898
+ })
899
+ })
900
+
901
+ // ─── useFileDrop — callback coverage ──────────────────────────────────────
902
+
903
+ describe('useFileDrop — callbacks via mock', () => {
904
+ it('fires onDrop with filtered files', async () => {
905
+ const { useFileDrop } = await import('../use-file-drop')
906
+ const el = document.createElement('div')
907
+ document.body.appendChild(el)
908
+
909
+ const droppedFiles: File[][] = []
910
+
911
+ const { isOver, isDraggingFiles } = useFileDrop({
912
+ element: () => el,
913
+ accept: ['image/*'],
914
+ maxFiles: 2,
915
+ onDrop: (files) => droppedFiles.push(files),
916
+ })
917
+
918
+ await flushMicrotasks()
919
+ expect(lastExternalDropOpts).not.toBeNull()
920
+
921
+ // Monitor fires drag start
922
+ lastExternalMonitorOpts.onDragStart()
923
+ expect(isDraggingFiles()).toBe(true)
924
+
925
+ // Drop target fires enter
926
+ lastExternalDropOpts.onDragEnter()
927
+ expect(isOver()).toBe(true)
928
+
929
+ // Drop target fires leave
930
+ lastExternalDropOpts.onDragLeave()
931
+ expect(isOver()).toBe(false)
932
+
933
+ // Drop with files
934
+ const imgFile = new File([''], 'photo.png', { type: 'image/png' })
935
+ const pdfFile = new File([''], 'doc.pdf', { type: 'application/pdf' })
936
+ const img2File = new File([''], 'other.jpg', { type: 'image/jpeg' })
937
+ const img3File = new File([''], 'extra.gif', { type: 'image/gif' })
938
+
939
+ lastExternalDropOpts.onDrop({
940
+ source: { _files: [imgFile, pdfFile, img2File, img3File] },
941
+ })
942
+
943
+ expect(isOver()).toBe(false)
944
+ expect(isDraggingFiles()).toBe(false)
945
+ expect(droppedFiles).toHaveLength(1)
946
+ // Should filter out pdf, keep images, max 2
947
+ expect(droppedFiles[0]!.length).toBe(2)
948
+
949
+ // Monitor fires drop
950
+ lastExternalMonitorOpts.onDrop()
951
+
952
+ el.remove()
953
+ })
954
+
955
+ it('canDrop returns false when disabled is true', async () => {
956
+ const { useFileDrop } = await import('../use-file-drop')
957
+ const el = document.createElement('div')
958
+ document.body.appendChild(el)
959
+
960
+ useFileDrop({
961
+ element: () => el,
962
+ disabled: true,
963
+ onDrop: () => {},
964
+ })
965
+
966
+ await flushMicrotasks()
967
+ expect(lastExternalDropOpts.canDrop({ source: {} })).toBe(false)
968
+ el.remove()
969
+ })
970
+
971
+ it('canDrop returns false when disabled function returns true', async () => {
972
+ const { useFileDrop } = await import('../use-file-drop')
973
+ const el = document.createElement('div')
974
+ document.body.appendChild(el)
975
+ const disabled = signal(true)
976
+
977
+ useFileDrop({
978
+ element: () => el,
979
+ disabled,
980
+ onDrop: () => {},
981
+ })
982
+
983
+ await flushMicrotasks()
984
+ expect(lastExternalDropOpts.canDrop({ source: {} })).toBe(false)
985
+ el.remove()
986
+ })
987
+
988
+ it('drops with no accept filter passes all files', async () => {
989
+ const { useFileDrop } = await import('../use-file-drop')
990
+ const el = document.createElement('div')
991
+ document.body.appendChild(el)
992
+
993
+ const droppedFiles: File[][] = []
994
+ useFileDrop({
995
+ element: () => el,
996
+ onDrop: (files) => droppedFiles.push(files),
997
+ })
998
+
999
+ await flushMicrotasks()
1000
+
1001
+ const file1 = new File([''], 'a.txt', { type: 'text/plain' })
1002
+ const file2 = new File([''], 'b.jpg', { type: 'image/jpeg' })
1003
+ lastExternalDropOpts.onDrop({ source: { _files: [file1, file2] } })
1004
+ expect(droppedFiles[0]!.length).toBe(2)
1005
+
1006
+ el.remove()
1007
+ })
1008
+
1009
+ it('drops with empty files does not call onDrop', async () => {
1010
+ const { useFileDrop } = await import('../use-file-drop')
1011
+ const el = document.createElement('div')
1012
+ document.body.appendChild(el)
1013
+
1014
+ const droppedFiles: File[][] = []
1015
+ useFileDrop({
1016
+ element: () => el,
1017
+ accept: ['image/*'],
1018
+ onDrop: (files) => droppedFiles.push(files),
1019
+ })
1020
+
1021
+ await flushMicrotasks()
1022
+
1023
+ // Drop non-matching files
1024
+ const txtFile = new File([''], 'readme.txt', { type: 'text/plain' })
1025
+ lastExternalDropOpts.onDrop({ source: { _files: [txtFile] } })
1026
+ expect(droppedFiles).toHaveLength(0)
1027
+
1028
+ el.remove()
1029
+ })
1030
+
1031
+ it('accept filter with file extension pattern', async () => {
1032
+ const { useFileDrop } = await import('../use-file-drop')
1033
+ const el = document.createElement('div')
1034
+ document.body.appendChild(el)
1035
+
1036
+ const droppedFiles: File[][] = []
1037
+ useFileDrop({
1038
+ element: () => el,
1039
+ accept: ['.pdf'],
1040
+ onDrop: (files) => droppedFiles.push(files),
1041
+ })
1042
+
1043
+ await flushMicrotasks()
1044
+
1045
+ const pdf = new File([''], 'doc.PDF', { type: 'application/pdf' }) // uppercase ext
1046
+ const txt = new File([''], 'readme.txt', { type: 'text/plain' })
1047
+ lastExternalDropOpts.onDrop({ source: { _files: [pdf, txt] } })
1048
+ expect(droppedFiles[0]!.length).toBe(1)
1049
+
1050
+ el.remove()
1051
+ })
1052
+
1053
+ it('accept filter with exact mime type', async () => {
1054
+ const { useFileDrop } = await import('../use-file-drop')
1055
+ const el = document.createElement('div')
1056
+ document.body.appendChild(el)
1057
+
1058
+ const droppedFiles: File[][] = []
1059
+ useFileDrop({
1060
+ element: () => el,
1061
+ accept: ['application/json'],
1062
+ onDrop: (files) => droppedFiles.push(files),
1063
+ })
1064
+
1065
+ await flushMicrotasks()
1066
+
1067
+ const json = new File(['{}'], 'data.json', { type: 'application/json' })
1068
+ lastExternalDropOpts.onDrop({ source: { _files: [json] } })
1069
+ expect(droppedFiles[0]!.length).toBe(1)
1070
+
1071
+ el.remove()
1072
+ })
1073
+
1074
+ it('setup does nothing when element is null', async () => {
1075
+ const { useFileDrop } = await import('../use-file-drop')
1076
+
1077
+ lastExternalDropOpts = null
1078
+ useFileDrop({
1079
+ element: () => null,
1080
+ onDrop: () => {},
1081
+ })
1082
+
1083
+ await flushMicrotasks()
1084
+ expect(lastExternalDropOpts).toBeNull()
1085
+ })
1086
+
1087
+ it('external monitor canMonitor is called', async () => {
1088
+ const { useFileDrop } = await import('../use-file-drop')
1089
+ const el = document.createElement('div')
1090
+ document.body.appendChild(el)
1091
+
1092
+ useFileDrop({
1093
+ element: () => el,
1094
+ onDrop: () => {},
1095
+ })
1096
+
1097
+ await flushMicrotasks()
1098
+
1099
+ if (lastExternalMonitorOpts?.canMonitor) {
1100
+ // Our containsFiles mock always returns true
1101
+ expect(lastExternalMonitorOpts.canMonitor({ source: {} })).toBe(true)
1102
+ }
1103
+
1104
+ // canDrop on drop target also calls containsFiles
1105
+ if (lastExternalDropOpts?.canDrop) {
1106
+ expect(lastExternalDropOpts.canDrop({ source: {} })).toBe(true)
1107
+ }
1108
+
1109
+ el.remove()
1110
+ })
1111
+ })
1112
+
1113
+ // ─── useSortable — extended coverage ──────────────────────────────────────
1114
+
1115
+ describe('useSortable — callback coverage via mock', () => {
1116
+ it('keyboard reorder: Alt+ArrowLeft in horizontal mode', async () => {
1117
+ const { useSortable } = await import('../use-sortable')
1118
+ const reordered: { id: string }[][] = []
1119
+ const items = signal([{ id: 'a' }, { id: 'b' }, { id: 'c' }])
1120
+
1121
+ const { containerRef, itemRef } = useSortable({
1122
+ items,
1123
+ by: (i) => i.id,
1124
+ onReorder: (newItems) => reordered.push(newItems),
1125
+ axis: 'horizontal',
1126
+ })
1127
+
1128
+ const container = document.createElement('div')
1129
+ document.body.appendChild(container)
1130
+ containerRef(container)
1131
+
1132
+ const els = items().map((item) => {
1133
+ const el = document.createElement('div')
1134
+ itemRef(item.id)(el)
1135
+ container.appendChild(el)
1136
+ return el
1137
+ })
1138
+
1139
+ // Focus on second item and move left
1140
+ els[1]!.focus()
1141
+ container.dispatchEvent(
1142
+ new KeyboardEvent('keydown', { key: 'ArrowLeft', altKey: true, bubbles: true }),
1143
+ )
1144
+
1145
+ expect(reordered.length).toBe(1)
1146
+ expect(reordered[0]!.map((i) => i.id)).toEqual(['b', 'a', 'c'])
1147
+
1148
+ container.remove()
1149
+ })
1150
+
1151
+ it('ignores keyboard events without altKey', async () => {
1152
+ const { useSortable } = await import('../use-sortable')
1153
+ const reordered: { id: string }[][] = []
1154
+ const items = signal([{ id: 'a' }, { id: 'b' }])
1155
+
1156
+ const { containerRef, itemRef } = useSortable({
1157
+ items,
1158
+ by: (i) => i.id,
1159
+ onReorder: (newItems) => reordered.push(newItems),
1160
+ })
1161
+
1162
+ const container = document.createElement('ul')
1163
+ document.body.appendChild(container)
1164
+ containerRef(container)
1165
+
1166
+ items().forEach((item) => {
1167
+ const li = document.createElement('li')
1168
+ itemRef(item.id)(li)
1169
+ container.appendChild(li)
1170
+ })
1171
+
1172
+ container.dispatchEvent(
1173
+ new KeyboardEvent('keydown', { key: 'ArrowDown', altKey: false, bubbles: true }),
1174
+ )
1175
+ expect(reordered.length).toBe(0)
1176
+
1177
+ container.remove()
1178
+ })
1179
+
1180
+ it('ignores unrelated key with alt', async () => {
1181
+ const { useSortable } = await import('../use-sortable')
1182
+ const reordered: { id: string }[][] = []
1183
+ const items = signal([{ id: 'a' }, { id: 'b' }])
1184
+
1185
+ const { containerRef, itemRef } = useSortable({
1186
+ items,
1187
+ by: (i) => i.id,
1188
+ onReorder: (newItems) => reordered.push(newItems),
1189
+ })
1190
+
1191
+ const container = document.createElement('ul')
1192
+ document.body.appendChild(container)
1193
+ containerRef(container)
1194
+
1195
+ const els = items().map((item) => {
1196
+ const li = document.createElement('li')
1197
+ itemRef(item.id)(li)
1198
+ container.appendChild(li)
1199
+ return li
1200
+ })
1201
+
1202
+ els[0]!.focus()
1203
+
1204
+ // Alt + Escape — not an arrow key
1205
+ container.dispatchEvent(
1206
+ new KeyboardEvent('keydown', { key: 'Escape', altKey: true, bubbles: true }),
1207
+ )
1208
+ expect(reordered.length).toBe(0)
1209
+
1210
+ container.remove()
1211
+ })
1212
+
1213
+ it('ignores keyboard when focused element is outside container', async () => {
1214
+ const { useSortable } = await import('../use-sortable')
1215
+ const reordered: { id: string }[][] = []
1216
+ const items = signal([{ id: 'a' }])
1217
+
1218
+ const { containerRef, itemRef } = useSortable({
1219
+ items,
1220
+ by: (i) => i.id,
1221
+ onReorder: (newItems) => reordered.push(newItems),
1222
+ })
1223
+
1224
+ const container = document.createElement('ul')
1225
+ document.body.appendChild(container)
1226
+ containerRef(container)
1227
+
1228
+ const li = document.createElement('li')
1229
+ itemRef('a')(li)
1230
+ container.appendChild(li)
1231
+
1232
+ // Focus an element OUTSIDE the container
1233
+ const outside = document.createElement('button')
1234
+ document.body.appendChild(outside)
1235
+ outside.focus()
1236
+
1237
+ container.dispatchEvent(
1238
+ new KeyboardEvent('keydown', { key: 'ArrowDown', altKey: true, bubbles: true }),
1239
+ )
1240
+ expect(reordered.length).toBe(0)
1241
+
1242
+ outside.remove()
1243
+ container.remove()
1244
+ })
1245
+
1246
+ it('sortable item drag events update signals', async () => {
1247
+ const { useSortable } = await import('../use-sortable')
1248
+ const items = signal([{ id: 'a' }, { id: 'b' }])
1249
+
1250
+ const { containerRef, itemRef, activeId, overId, overEdge } = useSortable({
1251
+ items,
1252
+ by: (i) => i.id,
1253
+ onReorder: () => {},
1254
+ })
1255
+
1256
+ const container = document.createElement('ul')
1257
+ document.body.appendChild(container)
1258
+ containerRef(container)
1259
+
1260
+ const li1 = document.createElement('li')
1261
+ itemRef('a')(li1)
1262
+ container.appendChild(li1)
1263
+
1264
+ const li2 = document.createElement('li')
1265
+ itemRef('b')(li2)
1266
+ container.appendChild(li2)
1267
+
1268
+ // Find the draggable registered for item 'a'
1269
+ const itemADraggable = allDraggableOpts.find(
1270
+ (o: any) => o.getInitialData().__pyreon_sortable_key === 'a',
1271
+ )
1272
+ expect(itemADraggable).toBeDefined()
1273
+
1274
+ // Trigger drag start on item 'a'
1275
+ itemADraggable.onDragStart()
1276
+ expect(activeId()).toBe('a')
1277
+
1278
+ // Find the drop target registered for item 'b'
1279
+ const itemBDropTarget = allDropTargetOpts.find((o: any) => {
1280
+ try {
1281
+ const d = o.getData({ input: {}, element: li2 })
1282
+ return d.__pyreon_sortable_key === 'b'
1283
+ } catch {
1284
+ return false
1285
+ }
1286
+ })
1287
+
1288
+ if (itemBDropTarget) {
1289
+ // Drag enter on item B
1290
+ itemBDropTarget.onDragEnter({ self: { data: { __edge: 'bottom' } } })
1291
+ expect(overId()).toBe('b')
1292
+ expect(overEdge()).toBe('bottom')
1293
+
1294
+ // onDrag updates edge
1295
+ itemBDropTarget.onDrag({ self: { data: { __edge: 'top' } } })
1296
+ expect(overEdge()).toBe('top')
1297
+
1298
+ // Drag leave
1299
+ itemBDropTarget.onDragLeave()
1300
+ expect(overId()).toBeNull()
1301
+ }
1302
+
1303
+ // Trigger drop on item 'a' draggable
1304
+ itemADraggable.onDrop()
1305
+ // queueMicrotask deferred reset
1306
+ await flushMicrotasks()
1307
+ expect(activeId()).toBeNull()
1308
+
1309
+ container.remove()
1310
+ })
1311
+
1312
+ it('container drop target fires performReorder', async () => {
1313
+ const { useSortable } = await import('../use-sortable')
1314
+ const reordered: { id: string }[][] = []
1315
+ const items = signal([{ id: 'a' }, { id: 'b' }, { id: 'c' }])
1316
+
1317
+ const { containerRef, itemRef, activeId, overId, overEdge } = useSortable({
1318
+ items,
1319
+ by: (i) => i.id,
1320
+ onReorder: (newItems) => reordered.push(newItems),
1321
+ })
1322
+
1323
+ const container = document.createElement('ul')
1324
+ document.body.appendChild(container)
1325
+ containerRef(container)
1326
+
1327
+ items().forEach((item) => {
1328
+ const li = document.createElement('li')
1329
+ itemRef(item.id)(li)
1330
+ container.appendChild(li)
1331
+ })
1332
+
1333
+ // Find the container drop target (has our sortable ID in getData)
1334
+ const containerDropTarget = allDropTargetOpts.find((o: any) => {
1335
+ try {
1336
+ const d = o.getData()
1337
+ return d.__pyreon_sortable_id !== undefined
1338
+ } catch {
1339
+ return false
1340
+ }
1341
+ })
1342
+ expect(containerDropTarget).toBeDefined()
1343
+
1344
+ // Simulate: drag 'a', hover over 'c' at bottom edge, then drop on container
1345
+ const itemADraggable = allDraggableOpts.find(
1346
+ (o: any) => o.getInitialData().__pyreon_sortable_key === 'a',
1347
+ )
1348
+ itemADraggable!.onDragStart()
1349
+ // Manually set overId and overEdge (simulating hover)
1350
+ // Find item C's drop target
1351
+ const itemCDropTarget = allDropTargetOpts.find((o: any) => {
1352
+ try {
1353
+ return o.getData({ input: {}, element: {} }).__pyreon_sortable_key === 'c'
1354
+ } catch {
1355
+ return false
1356
+ }
1357
+ })
1358
+ if (itemCDropTarget) {
1359
+ itemCDropTarget.onDragEnter({ self: { data: { __edge: 'bottom' } } })
1360
+ }
1361
+
1362
+ // Drop on container
1363
+ containerDropTarget!.onDrop()
1364
+ expect(reordered.length).toBe(1)
1365
+ // 'a' moved after 'c'
1366
+ expect(reordered[0]!.map((i) => i.id)).toEqual(['b', 'c', 'a'])
1367
+ expect(activeId()).toBeNull()
1368
+ expect(overId()).toBeNull()
1369
+ expect(overEdge()).toBeNull()
1370
+
1371
+ container.remove()
1372
+ })
1373
+
1374
+ it('performReorder with top/left edge moves before target', async () => {
1375
+ const { useSortable } = await import('../use-sortable')
1376
+ const reordered: { id: string }[][] = []
1377
+ const items = signal([{ id: 'a' }, { id: 'b' }, { id: 'c' }])
1378
+
1379
+ const { containerRef, itemRef } = useSortable({
1380
+ items,
1381
+ by: (i) => i.id,
1382
+ onReorder: (newItems) => reordered.push(newItems),
1383
+ })
1384
+
1385
+ const container = document.createElement('ul')
1386
+ document.body.appendChild(container)
1387
+ containerRef(container)
1388
+
1389
+ items().forEach((item) => {
1390
+ const li = document.createElement('li')
1391
+ itemRef(item.id)(li)
1392
+ container.appendChild(li)
1393
+ })
1394
+
1395
+ // Drag 'c', hover over 'a' at top edge
1396
+ const itemCDraggable = allDraggableOpts.find(
1397
+ (o: any) => o.getInitialData().__pyreon_sortable_key === 'c',
1398
+ )
1399
+ itemCDraggable!.onDragStart()
1400
+
1401
+ const itemADropTarget = allDropTargetOpts.find((o: any) => {
1402
+ try {
1403
+ return o.getData({ input: {}, element: {} }).__pyreon_sortable_key === 'a'
1404
+ } catch {
1405
+ return false
1406
+ }
1407
+ })
1408
+ if (itemADropTarget) {
1409
+ itemADropTarget.onDragEnter({ self: { data: { __edge: 'top' } } })
1410
+ }
1411
+
1412
+ // Drop on container
1413
+ const containerDropTarget = allDropTargetOpts.find((o: any) => {
1414
+ try {
1415
+ const d = o.getData()
1416
+ return d.__pyreon_sortable_id !== undefined
1417
+ } catch {
1418
+ return false
1419
+ }
1420
+ })
1421
+ containerDropTarget!.onDrop()
1422
+ expect(reordered.length).toBe(1)
1423
+ // 'c' moved before 'a'
1424
+ expect(reordered[0]!.map((i) => i.id)).toEqual(['c', 'a', 'b'])
1425
+
1426
+ container.remove()
1427
+ })
1428
+
1429
+ it('item drag leave only clears when overId matches current key', async () => {
1430
+ const { useSortable } = await import('../use-sortable')
1431
+ const items = signal([{ id: 'a' }, { id: 'b' }])
1432
+
1433
+ const { containerRef, itemRef, overId, overEdge } = useSortable({
1434
+ items,
1435
+ by: (i) => i.id,
1436
+ onReorder: () => {},
1437
+ })
1438
+
1439
+ const container = document.createElement('ul')
1440
+ document.body.appendChild(container)
1441
+ containerRef(container)
1442
+
1443
+ const li1 = document.createElement('li')
1444
+ itemRef('a')(li1)
1445
+ container.appendChild(li1)
1446
+
1447
+ const li2 = document.createElement('li')
1448
+ itemRef('b')(li2)
1449
+ container.appendChild(li2)
1450
+
1451
+ // Find item B's drop target
1452
+ const itemBDropTarget = allDropTargetOpts.find((o: any) => {
1453
+ try {
1454
+ return o.getData({ input: {}, element: li2 }).__pyreon_sortable_key === 'b'
1455
+ } catch {
1456
+ return false
1457
+ }
1458
+ })
1459
+
1460
+ if (itemBDropTarget) {
1461
+ // Enter B — overId is now 'b'
1462
+ itemBDropTarget.onDragEnter({ self: { data: { __edge: 'bottom' } } })
1463
+ expect(overId()).toBe('b')
1464
+
1465
+ // Leave B — since overId matches 'b', it should clear
1466
+ itemBDropTarget.onDragLeave()
1467
+ expect(overId()).toBeNull()
1468
+ expect(overEdge()).toBeNull()
1469
+ }
1470
+
1471
+ container.remove()
1472
+ })
1473
+
1474
+ it('canDrop on container checks sortable ID', async () => {
1475
+ const { useSortable } = await import('../use-sortable')
1476
+ const items = signal([{ id: 'a' }])
1477
+
1478
+ const { containerRef, itemRef } = useSortable({
1479
+ items,
1480
+ by: (i) => i.id,
1481
+ onReorder: () => {},
1482
+ })
1483
+
1484
+ const container = document.createElement('ul')
1485
+ document.body.appendChild(container)
1486
+ containerRef(container)
1487
+
1488
+ const li = document.createElement('li')
1489
+ itemRef('a')(li)
1490
+ container.appendChild(li)
1491
+
1492
+ // Find container drop target
1493
+ const containerDropTarget = allDropTargetOpts.find((o: any) => {
1494
+ try {
1495
+ const d = o.getData()
1496
+ return d.__pyreon_sortable_id !== undefined
1497
+ } catch {
1498
+ return false
1499
+ }
1500
+ })
1501
+
1502
+ // canDrop should accept same sortable ID
1503
+ const sortableId = containerDropTarget!.getData().__pyreon_sortable_id
1504
+ expect(
1505
+ containerDropTarget!.canDrop({ source: { data: { __pyreon_sortable_id: sortableId } } }),
1506
+ ).toBe(true)
1507
+ expect(
1508
+ containerDropTarget!.canDrop({ source: { data: { __pyreon_sortable_id: 'other' } } }),
1509
+ ).toBe(false)
1510
+
1511
+ container.remove()
1512
+ })
1513
+
1514
+ it('autoScroll canScroll checks sortable ID', async () => {
1515
+ const { useSortable } = await import('../use-sortable')
1516
+ const items = signal([{ id: 'a' }])
1517
+
1518
+ const { containerRef, itemRef } = useSortable({
1519
+ items,
1520
+ by: (i) => i.id,
1521
+ onReorder: () => {},
1522
+ })
1523
+
1524
+ const container = document.createElement('ul')
1525
+ document.body.appendChild(container)
1526
+ containerRef(container)
1527
+
1528
+ const li = document.createElement('li')
1529
+ itemRef('a')(li)
1530
+ container.appendChild(li)
1531
+
1532
+ // Get the sortable ID from the item's data
1533
+ const itemData = allDraggableOpts[allDraggableOpts.length - 1]?.getInitialData()
1534
+ const sortableId = itemData?.__pyreon_sortable_id
1535
+
1536
+ if (lastAutoScrollOpts?.canScroll) {
1537
+ // canScroll returns true for matching sortable ID
1538
+ expect(lastAutoScrollOpts.canScroll({ source: { data: { __pyreon_sortable_id: sortableId } } })).toBe(true)
1539
+ // canScroll returns false for different sortable ID
1540
+ expect(lastAutoScrollOpts.canScroll({ source: { data: { __pyreon_sortable_id: 'other' } } })).toBe(false)
1541
+ }
1542
+
1543
+ container.remove()
1544
+ })
1545
+
1546
+ it('item canDrop checks sortable ID', async () => {
1547
+ const { useSortable } = await import('../use-sortable')
1548
+ const items = signal([{ id: 'a' }])
1549
+
1550
+ const { containerRef, itemRef } = useSortable({
1551
+ items,
1552
+ by: (i) => i.id,
1553
+ onReorder: () => {},
1554
+ })
1555
+
1556
+ const container = document.createElement('ul')
1557
+ document.body.appendChild(container)
1558
+ containerRef(container)
1559
+
1560
+ const li = document.createElement('li')
1561
+ itemRef('a')(li)
1562
+ container.appendChild(li)
1563
+
1564
+ // Find the item drop target for 'a'
1565
+ const itemDropTarget = allDropTargetOpts.find((o: any) => {
1566
+ try {
1567
+ return o.getData({ input: {}, element: li }).__pyreon_sortable_key === 'a'
1568
+ } catch {
1569
+ return false
1570
+ }
1571
+ })
1572
+
1573
+ if (itemDropTarget?.canDrop) {
1574
+ const sortableId = allDraggableOpts[allDraggableOpts.length - 1]?.getInitialData().__pyreon_sortable_id
1575
+ expect(itemDropTarget.canDrop({ source: { data: { __pyreon_sortable_id: sortableId } } })).toBe(true)
1576
+ expect(itemDropTarget.canDrop({ source: { data: { __pyreon_sortable_id: 'other' } } })).toBe(false)
1577
+ }
1578
+
1579
+ container.remove()
1580
+ })
1581
+
1582
+ it('item drag leave does not clear overId if it does not match key', async () => {
1583
+ const { useSortable } = await import('../use-sortable')
1584
+ const items = signal([{ id: 'a' }, { id: 'b' }])
1585
+
1586
+ const { containerRef, itemRef, overId } = useSortable({
1587
+ items,
1588
+ by: (i) => i.id,
1589
+ onReorder: () => {},
1590
+ })
1591
+
1592
+ const container = document.createElement('ul')
1593
+ document.body.appendChild(container)
1594
+ containerRef(container)
1595
+
1596
+ const li1 = document.createElement('li')
1597
+ itemRef('a')(li1)
1598
+ container.appendChild(li1)
1599
+
1600
+ const li2 = document.createElement('li')
1601
+ itemRef('b')(li2)
1602
+ container.appendChild(li2)
1603
+
1604
+ // Find item A's drop target
1605
+ const itemADropTarget = allDropTargetOpts.find((o: any) => {
1606
+ try {
1607
+ return o.getData({ input: {}, element: li1 }).__pyreon_sortable_key === 'a'
1608
+ } catch {
1609
+ return false
1610
+ }
1611
+ })
1612
+
1613
+ // Find item B's drop target
1614
+ const itemBDropTarget = allDropTargetOpts.find((o: any) => {
1615
+ try {
1616
+ return o.getData({ input: {}, element: li2 }).__pyreon_sortable_key === 'b'
1617
+ } catch {
1618
+ return false
1619
+ }
1620
+ })
1621
+
1622
+ if (itemADropTarget && itemBDropTarget) {
1623
+ // Enter B — overId is now 'b'
1624
+ itemBDropTarget.onDragEnter({ self: { data: { __edge: 'bottom' } } })
1625
+ expect(overId()).toBe('b')
1626
+
1627
+ // Leave A — overId is 'b', not 'a', so should NOT clear
1628
+ itemADropTarget.onDragLeave()
1629
+ expect(overId()).toBe('b') // Still 'b'
1630
+ }
1631
+
1632
+ container.remove()
1633
+ })
1634
+
1635
+ it('ignores keyboard when focused element has no data-pyreon-sort-key', async () => {
1636
+ const { useSortable } = await import('../use-sortable')
1637
+ const reordered: { id: string }[][] = []
1638
+ const items = signal([{ id: 'a' }])
1639
+
1640
+ const { containerRef } = useSortable({
1641
+ items,
1642
+ by: (i) => i.id,
1643
+ onReorder: (newItems) => reordered.push(newItems),
1644
+ })
1645
+
1646
+ const container = document.createElement('ul')
1647
+ document.body.appendChild(container)
1648
+ containerRef(container)
1649
+
1650
+ // Add a child without data-pyreon-sort-key
1651
+ const child = document.createElement('li')
1652
+ container.appendChild(child)
1653
+ child.focus()
1654
+
1655
+ container.dispatchEvent(
1656
+ new KeyboardEvent('keydown', { key: 'ArrowDown', altKey: true, bubbles: true }),
1657
+ )
1658
+ expect(reordered.length).toBe(0)
1659
+
1660
+ container.remove()
1661
+ })
1662
+ })