|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +vi.mock('next/navigation', () => ({ |
| 9 | + useParams: () => ({ workspaceId: 'ws-1' }), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/hooks/queries/folders', () => ({ |
| 13 | + useReorderFolders: () => ({ mutateAsync: vi.fn() }), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('@/hooks/queries/workflows', () => ({ |
| 17 | + useReorderWorkflows: () => ({ mutateAsync: vi.fn() }), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/hooks/queries/utils/folder-cache', () => ({ |
| 21 | + getFolderMap: () => ({}), |
| 22 | +})) |
| 23 | + |
| 24 | +vi.mock('@/hooks/queries/utils/workflow-cache', () => ({ |
| 25 | + getWorkflows: () => [], |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/lib/folders/tree', () => ({ |
| 29 | + getFolderPath: () => [], |
| 30 | +})) |
| 31 | + |
| 32 | +const { mockUseFolderStore } = vi.hoisted(() => { |
| 33 | + const folderState = { setExpanded: () => {}, expandedFolders: new Set<string>() } |
| 34 | + const store = Object.assign( |
| 35 | + (selector: (state: typeof folderState) => unknown) => selector(folderState), |
| 36 | + { getState: () => folderState } |
| 37 | + ) |
| 38 | + return { mockUseFolderStore: store } |
| 39 | +}) |
| 40 | +vi.mock('@/stores/folders/store', () => ({ useFolderStore: mockUseFolderStore })) |
| 41 | + |
| 42 | +import { useDragDrop } from '@/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-drag-drop' |
| 43 | + |
| 44 | +type DragDropApi = ReturnType<typeof useDragDrop> |
| 45 | + |
| 46 | +let latest: DragDropApi |
| 47 | + |
| 48 | +function Harness() { |
| 49 | + latest = useDragDrop() |
| 50 | + return null |
| 51 | +} |
| 52 | + |
| 53 | +/** Minimal stand-in for the dragOver event `initDragOver` consumes. */ |
| 54 | +function fakeDragOverEvent(): unknown { |
| 55 | + const node = {} |
| 56 | + return { |
| 57 | + preventDefault: () => {}, |
| 58 | + stopPropagation: () => {}, |
| 59 | + clientY: 0, |
| 60 | + // target !== currentTarget so the root drop zone skips indicator math (getBoundingClientRect) |
| 61 | + target: node, |
| 62 | + currentTarget: {}, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +let container: HTMLDivElement |
| 67 | +let root: Root |
| 68 | + |
| 69 | +describe('useDragDrop stranded-drag reset', () => { |
| 70 | + beforeEach(() => { |
| 71 | + // Prevent the auto-scroll rAF loop from spinning in jsdom. |
| 72 | + vi.stubGlobal( |
| 73 | + 'requestAnimationFrame', |
| 74 | + () => 0 as unknown as ReturnType<typeof requestAnimationFrame> |
| 75 | + ) |
| 76 | + vi.stubGlobal('cancelAnimationFrame', () => {}) |
| 77 | + container = document.createElement('div') |
| 78 | + document.body.appendChild(container) |
| 79 | + root = createRoot(container) |
| 80 | + act(() => { |
| 81 | + root.render(<Harness />) |
| 82 | + }) |
| 83 | + // The reset listeners only attach once a scroll container is registered. |
| 84 | + act(() => { |
| 85 | + latest.setScrollContainer(document.createElement('div')) |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + afterEach(() => { |
| 90 | + act(() => { |
| 91 | + root.unmount() |
| 92 | + }) |
| 93 | + container.remove() |
| 94 | + vi.unstubAllGlobals() |
| 95 | + vi.clearAllMocks() |
| 96 | + }) |
| 97 | + |
| 98 | + it('clears isDragging on a window dragend when no drop fired', () => { |
| 99 | + // A drag entering the list flips isDragging on via initDragOver. |
| 100 | + act(() => { |
| 101 | + latest.createRootDropZone().onDragOver(fakeDragOverEvent() as never) |
| 102 | + }) |
| 103 | + expect(latest.isDragging).toBe(true) |
| 104 | + |
| 105 | + // The drag is cancelled/dropped outside the list: only `dragend` fires, no `drop`. |
| 106 | + act(() => { |
| 107 | + window.dispatchEvent(new Event('dragend')) |
| 108 | + }) |
| 109 | + expect(latest.isDragging).toBe(false) |
| 110 | + }) |
| 111 | + |
| 112 | + it('keeps isDragging active across dragOver updates until the drag ends', () => { |
| 113 | + act(() => { |
| 114 | + latest.createRootDropZone().onDragOver(fakeDragOverEvent() as never) |
| 115 | + }) |
| 116 | + expect(latest.isDragging).toBe(true) |
| 117 | + |
| 118 | + // A subsequent dragOver must not tear down the active drag. |
| 119 | + act(() => { |
| 120 | + latest.createRootDropZone().onDragOver(fakeDragOverEvent() as never) |
| 121 | + }) |
| 122 | + expect(latest.isDragging).toBe(true) |
| 123 | + }) |
| 124 | +}) |
0 commit comments