|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { ToastProvider } from '@sim/emcn' |
| 6 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 7 | +import { createRoot, type Root } from 'react-dom/client' |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +const mocks = vi.hoisted(() => ({ upload: vi.fn(), refresh: vi.fn() })) |
| 11 | +vi.mock('@/lib/uploads/client/session-upload', () => ({ |
| 12 | + uploadInternalFileSession: mocks.upload, |
| 13 | +})) |
| 14 | +vi.mock('next/navigation', () => ({ |
| 15 | + useRouter: () => ({ refresh: mocks.refresh }), |
| 16 | + usePathname: () => '/o/org-1/home', |
| 17 | +})) |
| 18 | + |
| 19 | +import { OrganizationHeader } from '@/app/o/[organizationId]/components/organization-sidebar/components/organization-header/organization-header' |
| 20 | +import { organizationKeys } from '@/hooks/queries/utils/organization-keys' |
| 21 | + |
| 22 | +const organization = { id: 'org-1', name: 'Design', slug: 'design', logo: null, memberCount: 2 } |
| 23 | +let root: Root |
| 24 | +let container: HTMLDivElement |
| 25 | +let queryClient: QueryClient |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + vi.clearAllMocks() |
| 29 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 30 | + vi.stubGlobal( |
| 31 | + 'ResizeObserver', |
| 32 | + class { |
| 33 | + observe() {} |
| 34 | + unobserve() {} |
| 35 | + disconnect() {} |
| 36 | + } |
| 37 | + ) |
| 38 | + mocks.upload.mockResolvedValue({ path: '/api/files/serve/organization-logos/logo.png' }) |
| 39 | + queryClient = new QueryClient({ defaultOptions: { mutations: { retry: false } } }) |
| 40 | + container = document.createElement('div') |
| 41 | + document.body.append(container) |
| 42 | + root = createRoot(container) |
| 43 | +}) |
| 44 | + |
| 45 | +afterEach(async () => { |
| 46 | + await act(async () => root.unmount()) |
| 47 | + container.remove() |
| 48 | + queryClient.clear() |
| 49 | + vi.unstubAllGlobals() |
| 50 | +}) |
| 51 | + |
| 52 | +async function render(canEditLogo = true) { |
| 53 | + await act(async () => { |
| 54 | + root.render( |
| 55 | + <QueryClientProvider client={queryClient}> |
| 56 | + <ToastProvider> |
| 57 | + <OrganizationHeader |
| 58 | + organization={organization} |
| 59 | + canEditLogo={canEditLogo} |
| 60 | + isCollapsed={false} |
| 61 | + onExpandSidebar={vi.fn()} |
| 62 | + /> |
| 63 | + </ToastProvider> |
| 64 | + </QueryClientProvider> |
| 65 | + ) |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +async function openMenu() { |
| 70 | + await act(async () => { |
| 71 | + container |
| 72 | + .querySelector('[aria-label="Organization menu"]')! |
| 73 | + .dispatchEvent(new MouseEvent('pointerdown', { bubbles: true, button: 0 })) |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +function menuItem(name: string) { |
| 78 | + return Array.from(document.querySelectorAll<HTMLElement>('[role="menuitem"]')).find( |
| 79 | + (item) => item.textContent === name |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +async function pickFile(file: File) { |
| 84 | + const input = container.querySelector<HTMLInputElement>('input[type="file"]')! |
| 85 | + Object.defineProperty(input, 'files', { configurable: true, value: [file] }) |
| 86 | + await act(async () => input.dispatchEvent(new Event('change', { bubbles: true }))) |
| 87 | +} |
| 88 | + |
| 89 | +describe('OrganizationHeader logo upload', () => { |
| 90 | + it('opens the same native file picker from the admin menu', async () => { |
| 91 | + await render() |
| 92 | + await openMenu() |
| 93 | + const input = container.querySelector<HTMLInputElement>('input[type="file"]')! |
| 94 | + const click = vi.spyOn(input, 'click').mockImplementation(() => {}) |
| 95 | + expect(input.accept).toContain('image/png') |
| 96 | + await act(async () => menuItem('Upload logo')!.click()) |
| 97 | + expect(click).toHaveBeenCalledOnce() |
| 98 | + }) |
| 99 | + |
| 100 | + it('does not offer logo changes to members', async () => { |
| 101 | + await render(false) |
| 102 | + await openMenu() |
| 103 | + expect(menuItem('Upload logo')).toBeUndefined() |
| 104 | + expect(container.querySelector('input[type="file"]')).toBeNull() |
| 105 | + }) |
| 106 | + |
| 107 | + it('uploads under the organization scope and refreshes its identity after success', async () => { |
| 108 | + await render() |
| 109 | + const invalidate = vi.spyOn(queryClient, 'invalidateQueries') |
| 110 | + const file = new File(['image'], 'logo.png', { type: 'image/png' }) |
| 111 | + await pickFile(file) |
| 112 | + expect(mocks.upload).toHaveBeenCalledWith({ |
| 113 | + purpose: 'organization_logo', |
| 114 | + organizationId: organization.id, |
| 115 | + file, |
| 116 | + }) |
| 117 | + expect(invalidate).toHaveBeenCalledWith({ queryKey: organizationKeys.detail('org-1') }) |
| 118 | + expect(invalidate).toHaveBeenCalledWith({ queryKey: organizationKeys.lists() }) |
| 119 | + expect(mocks.refresh).toHaveBeenCalledOnce() |
| 120 | + }) |
| 121 | + |
| 122 | + it('rejects unsupported files before uploading', async () => { |
| 123 | + await render() |
| 124 | + await pickFile(new File(['text'], 'notes.txt', { type: 'text/plain' })) |
| 125 | + expect(mocks.upload).not.toHaveBeenCalled() |
| 126 | + expect(mocks.refresh).not.toHaveBeenCalled() |
| 127 | + expect(document.body.textContent).toContain('not a supported image format') |
| 128 | + }) |
| 129 | + |
| 130 | + it('keeps the saved identity when upload fails and allows retrying the same file', async () => { |
| 131 | + const file = new File(['image'], 'logo.png', { type: 'image/png' }) |
| 132 | + mocks.upload.mockRejectedValueOnce(new Error('Upload failed')) |
| 133 | + await render() |
| 134 | + await pickFile(file) |
| 135 | + expect(mocks.refresh).not.toHaveBeenCalled() |
| 136 | + expect(document.body.textContent).toContain('Upload failed') |
| 137 | + expect(container.querySelector<HTMLInputElement>('input[type="file"]')!.value).toBe('') |
| 138 | + await pickFile(file) |
| 139 | + expect(mocks.upload).toHaveBeenCalledTimes(2) |
| 140 | + expect(mocks.refresh).toHaveBeenCalledOnce() |
| 141 | + }) |
| 142 | +}) |
0 commit comments