|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act } from 'react' |
| 3 | +import { createRoot } from 'react-dom/client' |
| 4 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | +import { EmbeddedFileFolder } from '@/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/components/embedded-file-folder' |
| 7 | + |
| 8 | +const { useFolders, useFiles, state } = vi.hoisted(() => { |
| 9 | + const state = { |
| 10 | + pending: false, |
| 11 | + placeholder: false, |
| 12 | + error: null as Error | null, |
| 13 | + folders: [ |
| 14 | + { id: 'parent', name: 'Reports', parentId: null }, |
| 15 | + { id: 'child', name: 'Client documents', parentId: 'parent' }, |
| 16 | + { id: 'sibling', name: 'Unrelated folder', parentId: null }, |
| 17 | + ], |
| 18 | + files: [ |
| 19 | + { id: 'document', name: 'Invoice.md', folderId: 'parent' }, |
| 20 | + { id: 'other', name: 'Unrelated document', folderId: 'sibling' }, |
| 21 | + ], |
| 22 | + } |
| 23 | + return { |
| 24 | + state, |
| 25 | + useFolders: vi.fn(() => ({ |
| 26 | + data: state.folders, |
| 27 | + isPending: false, |
| 28 | + isPlaceholderData: false, |
| 29 | + error: null, |
| 30 | + refetch: vi.fn(), |
| 31 | + })), |
| 32 | + useFiles: vi.fn(() => ({ |
| 33 | + data: state.files, |
| 34 | + isPending: state.pending, |
| 35 | + isPlaceholderData: state.placeholder, |
| 36 | + error: state.error, |
| 37 | + refetch: vi.fn(), |
| 38 | + })), |
| 39 | + } |
| 40 | +}) |
| 41 | +vi.mock('@/hooks/queries/workspace-file-folders', () => ({ useWorkspaceFileFolders: useFolders })) |
| 42 | +vi.mock('@/hooks/queries/workspace-files', () => ({ useWorkspaceFiles: useFiles })) |
| 43 | + |
| 44 | +const render = (folderId = 'parent') => |
| 45 | + renderToStaticMarkup( |
| 46 | + <EmbeddedFileFolder workspaceId='workspace' folderId={folderId} onOpen={vi.fn()} /> |
| 47 | + ) |
| 48 | + |
| 49 | +describe('EmbeddedFileFolder', () => { |
| 50 | + beforeEach(() => { |
| 51 | + vi.clearAllMocks() |
| 52 | + state.pending = false |
| 53 | + state.placeholder = false |
| 54 | + state.error = null |
| 55 | + }) |
| 56 | + |
| 57 | + it('shows direct child folders and files without including siblings', () => { |
| 58 | + const markup = render() |
| 59 | + expect(markup).toContain('Reports') |
| 60 | + expect(markup).toContain('Client documents') |
| 61 | + expect(markup).toContain('Invoice.md') |
| 62 | + expect(markup).not.toContain('Unrelated') |
| 63 | + expect(markup).toContain('Open folder') |
| 64 | + expect(useFolders).toHaveBeenCalledWith('workspace') |
| 65 | + expect(useFiles).toHaveBeenCalledWith('workspace') |
| 66 | + }) |
| 67 | + |
| 68 | + it.each(['pending', 'placeholder'] as const)( |
| 69 | + 'does not render an empty or stale folder while files are %s', |
| 70 | + (key) => { |
| 71 | + state[key] = true |
| 72 | + const markup = render() |
| 73 | + expect(markup).toContain('Loading folder') |
| 74 | + expect(markup).not.toContain('Invoice.md') |
| 75 | + expect(markup).not.toContain('This folder is empty') |
| 76 | + } |
| 77 | + ) |
| 78 | + |
| 79 | + it('keeps a failed read distinct from an empty folder', () => { |
| 80 | + state.error = new Error('private diagnostic') |
| 81 | + const markup = render() |
| 82 | + expect(markup).toContain('Unable to load this folder') |
| 83 | + expect(markup).toContain('Try again') |
| 84 | + expect(markup).not.toContain('private diagnostic') |
| 85 | + expect(markup).not.toContain('Invoice.md') |
| 86 | + }) |
| 87 | + |
| 88 | + it('distinguishes an empty existing folder from a missing folder', () => { |
| 89 | + expect(render('child')).toContain('This folder is empty') |
| 90 | + expect(render('missing')).toContain('Folder not found') |
| 91 | + }) |
| 92 | + |
| 93 | + it('opens the exact selected folder or file through normal workspace destinations', () => { |
| 94 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 95 | + const container = document.createElement('div') |
| 96 | + document.body.appendChild(container) |
| 97 | + const root = createRoot(container) |
| 98 | + const onOpen = vi.fn() |
| 99 | + try { |
| 100 | + act(() => |
| 101 | + root.render( |
| 102 | + <EmbeddedFileFolder workspaceId='workspace' folderId='parent' onOpen={onOpen} /> |
| 103 | + ) |
| 104 | + ) |
| 105 | + for (const name of ['Open folder', 'Client documents', 'Invoice.md']) { |
| 106 | + const button = Array.from(container.querySelectorAll('button')).find( |
| 107 | + (element) => element.textContent === name |
| 108 | + ) |
| 109 | + expect(button).toBeDefined() |
| 110 | + act(() => button!.click()) |
| 111 | + } |
| 112 | + expect(onOpen.mock.calls).toEqual([ |
| 113 | + ['/workspace/workspace/files?folderId=parent'], |
| 114 | + ['/workspace/workspace/files?folderId=child'], |
| 115 | + ['/workspace/workspace/files/document'], |
| 116 | + ]) |
| 117 | + } finally { |
| 118 | + act(() => root.unmount()) |
| 119 | + container.remove() |
| 120 | + vi.unstubAllGlobals() |
| 121 | + } |
| 122 | + }) |
| 123 | +}) |
0 commit comments