|
1 | 1 | /** |
2 | | - * @vitest-environment node |
| 2 | + * @vitest-environment jsdom |
3 | 3 | */ |
4 | | -import { authMockFns } from '@sim/testing/mocks/auth.mock' |
5 | | -import { nextNavigationMock } from '@sim/testing/mocks/next-navigation.mock' |
6 | | -import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { act } from 'react' |
| 5 | +import { authClientMock, authClientMockFns } from '@sim/testing/mocks/auth-client.mock' |
| 6 | +import { nextNavigationMock, nextNavigationMockFns } from '@sim/testing/mocks/next-navigation.mock' |
| 7 | +import { createRoot, type Root } from 'react-dom/client' |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
7 | 9 |
|
8 | | -const { mockResolveWorkspacelessMemberPath } = vi.hoisted(() => ({ |
9 | | - mockResolveWorkspacelessMemberPath: vi.fn(), |
| 10 | +const { mockUseWorkspacesWithMetadata } = vi.hoisted(() => ({ |
| 11 | + mockUseWorkspacesWithMetadata: vi.fn(), |
10 | 12 | })) |
11 | 13 |
|
12 | 14 | vi.mock('next/navigation', () => nextNavigationMock) |
13 | | - |
14 | | -vi.mock('@/lib/navigation/resolve-app-entry', () => ({ |
15 | | - resolveWorkspacelessMemberPath: mockResolveWorkspacelessMemberPath, |
| 15 | +vi.mock('@/lib/auth/client', () => authClientMock) |
| 16 | +vi.mock('@/hooks/queries/workspace', () => ({ |
| 17 | + useWorkspacesWithMetadata: mockUseWorkspacesWithMetadata, |
16 | 18 | })) |
17 | 19 |
|
18 | | -vi.mock('@/app/workspace/workspace-entry', () => ({ WorkspaceEntry: () => null })) |
19 | | - |
20 | 20 | import WorkspacePage from '@/app/workspace/page' |
21 | 21 |
|
22 | | -const mockGetSession = authMockFns.mockGetSession |
23 | | -const session = { user: { id: 'viewer' } } |
| 22 | +const mockReplace = nextNavigationMockFns.router.replace |
| 23 | + |
| 24 | +describe('WorkspacePage for a viewer with nothing to pick', () => { |
| 25 | + let container: HTMLDivElement |
| 26 | + let root: Root |
24 | 27 |
|
25 | | -function visit(search: { redirect?: string } = {}) { |
26 | | - return WorkspacePage({ searchParams: Promise.resolve(search) }) |
27 | | -} |
| 28 | + function visit(search: string, list: { forwardOrganizationId?: string }) { |
| 29 | + window.history.pushState({}, '', `/workspace${search}`) |
| 30 | + mockUseWorkspacesWithMetadata.mockReturnValue({ |
| 31 | + data: { |
| 32 | + workspaces: [], |
| 33 | + lastActiveWorkspaceId: null, |
| 34 | + pinnedWorkspaceIds: [], |
| 35 | + creationPolicy: { canCreate: false, workspaceMode: 'organization' }, |
| 36 | + ...list, |
| 37 | + }, |
| 38 | + isLoading: false, |
| 39 | + error: null, |
| 40 | + }) |
| 41 | + act(() => root.render(<WorkspacePage />)) |
| 42 | + } |
28 | 43 |
|
29 | | -describe('WorkspacePage', () => { |
30 | 44 | beforeEach(() => { |
31 | | - mockGetSession.mockResolvedValue(session) |
32 | | - mockResolveWorkspacelessMemberPath.mockResolvedValue(null) |
| 45 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 46 | + mockReplace.mockClear() |
| 47 | + authClientMockFns.mockUseSession.mockReturnValue({ |
| 48 | + data: { user: { id: 'viewer' } }, |
| 49 | + isPending: false, |
| 50 | + error: null, |
| 51 | + }) |
| 52 | + container = document.createElement('div') |
| 53 | + document.body.appendChild(container) |
| 54 | + root = createRoot(container) |
33 | 55 | }) |
34 | 56 |
|
35 | | - it('forwards a workspaceless member before the picker renders', async () => { |
36 | | - mockResolveWorkspacelessMemberPath.mockResolvedValue('/o/org-1/home') |
37 | | - await expect(visit()).rejects.toThrow('NEXT_REDIRECT:/o/org-1/home') |
38 | | - expect(mockResolveWorkspacelessMemberPath).toHaveBeenCalledWith(session, 'home') |
| 57 | + afterEach(() => { |
| 58 | + act(() => root.unmount()) |
| 59 | + container.remove() |
39 | 60 | }) |
40 | 61 |
|
41 | | - it('carries a settings request through to the destination', async () => { |
42 | | - mockResolveWorkspacelessMemberPath.mockResolvedValue('/o/org-1/settings/general') |
43 | | - await expect(visit({ redirect: 'settings' })).rejects.toThrow( |
44 | | - 'NEXT_REDIRECT:/o/org-1/settings/general' |
45 | | - ) |
46 | | - expect(mockResolveWorkspacelessMemberPath).toHaveBeenCalledWith(session, 'settings') |
| 62 | + it('forwards to the organization without showing the blocked card', () => { |
| 63 | + visit('', { forwardOrganizationId: 'org-1' }) |
| 64 | + expect(mockReplace).toHaveBeenCalledExactlyOnceWith('/o/org-1/home') |
| 65 | + expect(container.querySelector('h1')).toBeNull() |
47 | 66 | }) |
48 | 67 |
|
49 | | - it('leaves a stale session on the recovery surface', async () => { |
50 | | - mockGetSession.mockResolvedValue(null) |
51 | | - await expect(visit()).resolves.toBeTruthy() |
52 | | - expect(mockResolveWorkspacelessMemberPath).not.toHaveBeenCalled() |
| 68 | + it('carries a settings request to the organization settings', () => { |
| 69 | + visit('?redirect=settings', { forwardOrganizationId: 'org-1' }) |
| 70 | + expect(mockReplace).toHaveBeenCalledExactlyOnceWith('/o/org-1/settings/general') |
53 | 71 | }) |
54 | 72 |
|
55 | | - it.each([ |
56 | | - ['session', () => mockGetSession.mockRejectedValue(new Error('down'))], |
57 | | - ['destination', () => mockResolveWorkspacelessMemberPath.mockRejectedValue(new Error('down'))], |
58 | | - ])('keeps the recovery surface when the %s lookup fails', async (_label, fail) => { |
59 | | - fail() |
60 | | - await expect(visit()).resolves.toBeTruthy() |
| 73 | + it('stays on the blocked card when no organization can serve them', () => { |
| 74 | + visit('', {}) |
| 75 | + expect(mockReplace).not.toHaveBeenCalled() |
| 76 | + expect(container.querySelector('h1')).not.toBeNull() |
61 | 77 | }) |
62 | 78 | }) |
0 commit comments