|
| 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 | +import { useSidebarResize } from '@/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-sidebar-resize' |
| 8 | +import { useSidebarStore } from '@/stores/sidebar/store' |
| 9 | + |
| 10 | +let container: HTMLDivElement |
| 11 | +let root: Root |
| 12 | + |
| 13 | +function ResizeHandle() { |
| 14 | + const { handlePointerDown } = useSidebarResize() |
| 15 | + return <div role='separator' onPointerDown={handlePointerDown} /> |
| 16 | +} |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + vi.useFakeTimers() |
| 20 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 21 | + useSidebarStore.setState({ isCollapsed: false, sidebarWidth: 256 }) |
| 22 | + container = document.createElement('div') |
| 23 | + container.className = 'sidebar-shell-outer' |
| 24 | + document.body.appendChild(container) |
| 25 | + root = createRoot(container) |
| 26 | + act(() => root.render(<ResizeHandle />)) |
| 27 | +}) |
| 28 | + |
| 29 | +afterEach(() => { |
| 30 | + act(() => root.unmount()) |
| 31 | + container.remove() |
| 32 | + vi.useRealTimers() |
| 33 | +}) |
| 34 | + |
| 35 | +function startResize() { |
| 36 | + const handle = container.querySelector('[role="separator"]')! |
| 37 | + act(() => handle.dispatchEvent(new MouseEvent('pointerdown', { bubbles: true }))) |
| 38 | + expect(container.hasAttribute('data-resizing')).toBe(true) |
| 39 | +} |
| 40 | + |
| 41 | +describe('sidebar resize lifecycle', () => { |
| 42 | + it.each(['pointerup', 'pointercancel', 'blur', 'unmount'])( |
| 43 | + 'restores animation and persists the final width after %s', |
| 44 | + (event) => { |
| 45 | + startResize() |
| 46 | + act(() => { |
| 47 | + document.dispatchEvent(new MouseEvent('pointermove', { clientX: 280 })) |
| 48 | + vi.advanceTimersByTime(20) |
| 49 | + }) |
| 50 | + expect(container.style.getPropertyValue('--sidebar-width')).toBe('280px') |
| 51 | + expect(useSidebarStore.getState().sidebarWidth).toBe(256) |
| 52 | + |
| 53 | + act(() => { |
| 54 | + if (event === 'unmount') root.render(null) |
| 55 | + else if (event === 'blur') window.dispatchEvent(new Event(event)) |
| 56 | + else document.dispatchEvent(new Event(event)) |
| 57 | + }) |
| 58 | + |
| 59 | + expect(container.hasAttribute('data-resizing')).toBe(false) |
| 60 | + expect(container.style.getPropertyValue('--sidebar-width')).toBe('') |
| 61 | + expect(useSidebarStore.getState().sidebarWidth).toBe(280) |
| 62 | + expect(document.body.style.cursor).toBe('') |
| 63 | + expect(document.body.style.userSelect).toBe('') |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + it('restores animation when released without moving', () => { |
| 68 | + startResize() |
| 69 | + act(() => document.dispatchEvent(new Event('pointerup'))) |
| 70 | + expect(container.hasAttribute('data-resizing')).toBe(false) |
| 71 | + expect(useSidebarStore.getState().sidebarWidth).toBe(256) |
| 72 | + }) |
| 73 | +}) |
0 commit comments