|
| 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 | +const { mockUseTablesList, mockUseFolders, mockUsePinnedIds } = vi.hoisted(() => ({ |
| 9 | + mockUseTablesList: vi.fn(), |
| 10 | + mockUseFolders: vi.fn(), |
| 11 | + mockUsePinnedIds: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('next/link', () => ({ |
| 15 | + default: ({ href, children }: { href: string; children: React.ReactNode }) => ( |
| 16 | + <a href={href}>{children}</a> |
| 17 | + ), |
| 18 | +})) |
| 19 | +vi.mock('next/navigation', () => ({ useParams: () => ({ workspaceId: 'w1' }) })) |
| 20 | +vi.mock('@/hooks/queries/tables', () => ({ useTablesList: mockUseTablesList })) |
| 21 | +vi.mock('@/hooks/queries/folders', () => ({ useFolders: mockUseFolders })) |
| 22 | +vi.mock('@/hooks/queries/pinned-items', () => ({ usePinnedIds: mockUsePinnedIds })) |
| 23 | +vi.mock('@/hooks/queries/workspace-files', () => ({ useWorkspaceFiles: vi.fn() })) |
| 24 | +vi.mock('@/hooks/queries/workspace-file-folders', () => ({ useWorkspaceFileFolders: vi.fn() })) |
| 25 | + |
| 26 | +import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@sim/emcn' |
| 27 | +import { TablesRailFlyout } from '@/app/workspace/[workspaceId]/w/components/sidebar/components/rail-resource-flyout' |
| 28 | + |
| 29 | +type QueryStub = { data?: unknown; isPending: boolean; isPlaceholderData: boolean } |
| 30 | + |
| 31 | +const resolved = (data: unknown): QueryStub => ({ |
| 32 | + data, |
| 33 | + isPending: false, |
| 34 | + isPlaceholderData: false, |
| 35 | +}) |
| 36 | +const placeholder = (data: unknown): QueryStub => ({ |
| 37 | + data, |
| 38 | + isPending: false, |
| 39 | + isPlaceholderData: true, |
| 40 | +}) |
| 41 | + |
| 42 | +const TABLE = { |
| 43 | + id: 't1', |
| 44 | + name: 'Leads', |
| 45 | + folderId: 'f1', |
| 46 | + updatedAt: new Date('2026-01-01'), |
| 47 | +} |
| 48 | +const FOLDER = { id: 'f1', name: 'Sales', parentId: null, updatedAt: new Date('2026-01-01') } |
| 49 | + |
| 50 | +describe('TablesRailFlyout', () => { |
| 51 | + let container: HTMLDivElement |
| 52 | + let root: Root |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 56 | + vi.stubGlobal( |
| 57 | + 'ResizeObserver', |
| 58 | + class { |
| 59 | + observe() {} |
| 60 | + unobserve() {} |
| 61 | + disconnect() {} |
| 62 | + } |
| 63 | + ) |
| 64 | + mockUsePinnedIds.mockReturnValue(new Set<string>()) |
| 65 | + container = document.createElement('div') |
| 66 | + document.body.appendChild(container) |
| 67 | + root = createRoot(container) |
| 68 | + }) |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + act(() => root.unmount()) |
| 72 | + container.remove() |
| 73 | + vi.clearAllMocks() |
| 74 | + vi.unstubAllGlobals() |
| 75 | + }) |
| 76 | + |
| 77 | + function render() { |
| 78 | + act(() => { |
| 79 | + root.render( |
| 80 | + <DropdownMenu open modal={false}> |
| 81 | + <DropdownMenuTrigger asChild> |
| 82 | + <button type='button'>rail</button> |
| 83 | + </DropdownMenuTrigger> |
| 84 | + <DropdownMenuContent> |
| 85 | + <TablesRailFlyout workspaceId='w1' /> |
| 86 | + </DropdownMenuContent> |
| 87 | + </DropdownMenu> |
| 88 | + ) |
| 89 | + }) |
| 90 | + return document.body.textContent ?? '' |
| 91 | + } |
| 92 | + |
| 93 | + it('nests a table under its folder once both lists have resolved', () => { |
| 94 | + mockUseTablesList.mockReturnValue(resolved([TABLE])) |
| 95 | + mockUseFolders.mockReturnValue(resolved([FOLDER])) |
| 96 | + |
| 97 | + const text = render() |
| 98 | + |
| 99 | + expect(text).toContain('Sales') |
| 100 | + expect(text).not.toContain('Loading...') |
| 101 | + /* The table sits inside the folder's submenu, which is closed until hovered. */ |
| 102 | + expect(document.querySelector('a[href="/workspace/w1/tables/t1"]')).toBeNull() |
| 103 | + }) |
| 104 | + |
| 105 | + it('waits rather than filing tables at the root while the folders are the previous workspace’s', () => { |
| 106 | + mockUseTablesList.mockReturnValue(resolved([TABLE])) |
| 107 | + mockUseFolders.mockReturnValue(placeholder([])) |
| 108 | + |
| 109 | + const text = render() |
| 110 | + |
| 111 | + expect(text).toContain('Loading...') |
| 112 | + expect(text).not.toContain('Leads') |
| 113 | + }) |
| 114 | + |
| 115 | + it('waits while the tables themselves are still placeholder data', () => { |
| 116 | + mockUseTablesList.mockReturnValue(placeholder([TABLE])) |
| 117 | + mockUseFolders.mockReturnValue(resolved([FOLDER])) |
| 118 | + |
| 119 | + expect(render()).toContain('Loading...') |
| 120 | + }) |
| 121 | + |
| 122 | + it('still lists every table when the folder query failed outright', () => { |
| 123 | + mockUseTablesList.mockReturnValue(resolved([TABLE])) |
| 124 | + mockUseFolders.mockReturnValue({ data: undefined, isPending: false, isPlaceholderData: false }) |
| 125 | + |
| 126 | + const text = render() |
| 127 | + |
| 128 | + expect(text).not.toContain('Loading...') |
| 129 | + expect(document.querySelector('a[href="/workspace/w1/tables/t1"]')?.textContent).toContain( |
| 130 | + 'Leads' |
| 131 | + ) |
| 132 | + }) |
| 133 | +}) |
0 commit comments