packages/supabase assets module: closed AssetFailure kinds, begin / quarantine upload (upsert off) / confirm / remove, own-row state read with an explicit column list, download under the SELECT policy. App: client transport (magic byte sniff, never SVG, createImageBitmap, canvas 1024 px, JPEG 0.9, 5 MiB cap), AvatarSection (preview, save, 1 s polling up to 30 s, rejection and failure messages per closed code, removal with confirmation, in-flight row resumed at mount), auth store blob URL revoked on change and sign-out, AvatarCircle in UserBadge and the top bar link to Mon compte (D3), FR/EN strings, fake client extended, 27 new tests (app 300).
121 lines
4.6 KiB
TypeScript
121 lines
4.6 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { cleanup, render, screen, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom'
|
|
import { createFakeSupabase, fakeProfile, fakeUser, type FakeSupabase } from '../helpers/fakeSupabase'
|
|
|
|
const mocks = vi.hoisted(() => ({ fake: null as unknown }))
|
|
vi.mock('@/lib/supabase', () => ({
|
|
getSupabaseClient: () => (mocks.fake as FakeSupabase | null)?.client ?? null,
|
|
isSupabaseConfigured: () => mocks.fake !== null,
|
|
}))
|
|
|
|
import AppLayout from '@/components/shell/AppLayout'
|
|
import { monogram } from '@/components/shell/UserBadge'
|
|
import { useAuthStore } from '@/state/authStore'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { fr } from '@/i18n/fr'
|
|
|
|
let fake: FakeSupabase
|
|
|
|
beforeEach(() => {
|
|
useLocaleStore.setState({ locale: 'fr' })
|
|
fake = createFakeSupabase()
|
|
mocks.fake = fake
|
|
useAuthStore.setState({
|
|
status: 'signed_in',
|
|
user: fakeUser(),
|
|
profile: fakeProfile({ display_name: 'Alice Local' }),
|
|
roles: [],
|
|
avatar: null,
|
|
avatarUrl: null,
|
|
})
|
|
})
|
|
afterEach(cleanup)
|
|
|
|
function mount(path = '/app') {
|
|
return render(
|
|
<MemoryRouter initialEntries={[path]}>
|
|
<Routes>
|
|
<Route path="/" element={<p>local tool</p>} />
|
|
<Route path="/app" element={<AppLayout />}>
|
|
<Route index element={<p>dashboard page</p>} />
|
|
<Route path="account" element={<p>account page</p>} />
|
|
</Route>
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
)
|
|
}
|
|
|
|
describe('AppLayout', () => {
|
|
it('renders the sidebar navigation, the user and the page', () => {
|
|
mount()
|
|
const nav = screen.getByRole('navigation', { name: fr['shell.nav_label'] })
|
|
expect(nav).toHaveTextContent(fr['shell.nav.dashboard'])
|
|
expect(nav).toHaveTextContent(fr['shell.nav.account'])
|
|
expect(nav).toHaveTextContent(fr['shell.nav.local_tool'])
|
|
expect(screen.getAllByText('Alice Local').length).toBeGreaterThan(0)
|
|
expect(screen.getByText('dashboard page')).toBeInTheDocument()
|
|
})
|
|
|
|
it('links the top-bar avatar and name to the account page', async () => {
|
|
const user = userEvent.setup()
|
|
mount()
|
|
const link = screen.getByTestId('topbar-account')
|
|
expect(link).toHaveAttribute('href', '/app/account')
|
|
expect(link).toHaveTextContent('Alice Local')
|
|
expect(link).toHaveAttribute('aria-label', fr['shell.nav.account'])
|
|
await user.click(link)
|
|
expect(await screen.findByText('account page')).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows the photo everywhere once the store holds a blob URL, the monogram otherwise', () => {
|
|
mount()
|
|
expect(screen.queryAllByRole('img', { name: fr['account.avatar.alt'] })).toHaveLength(0)
|
|
expect(screen.getAllByText('AL').length).toBeGreaterThan(0)
|
|
cleanup()
|
|
useAuthStore.setState({ avatarUrl: 'blob:photo' })
|
|
mount()
|
|
const imgs = screen.getAllByRole('img', { name: fr['account.avatar.alt'] })
|
|
expect(imgs.length).toBeGreaterThanOrEqual(2)
|
|
imgs.forEach((img) => expect(img).toHaveAttribute('src', 'blob:photo'))
|
|
expect(screen.queryByText('AL')).toBeNull()
|
|
})
|
|
|
|
it('marks the active destination', () => {
|
|
mount('/app/account')
|
|
const nav = screen.getByRole('navigation', { name: fr['shell.nav_label'] })
|
|
const link = within(nav).getByRole('link', { name: new RegExp(fr['shell.nav.account']) })
|
|
expect(link).toHaveAttribute('aria-current', 'page')
|
|
})
|
|
|
|
it('opens the mobile drawer from the menu button and closes it on navigation', async () => {
|
|
const user = userEvent.setup()
|
|
mount()
|
|
await user.click(screen.getByRole('button', { name: fr['shell.open_menu'] }))
|
|
const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] })
|
|
expect(drawer).toBeInTheDocument()
|
|
const links = screen.getAllByRole('link', { name: new RegExp(fr['shell.nav.account']) })
|
|
await user.click(links[links.length - 1])
|
|
expect(await screen.findByText('account page')).toBeInTheDocument()
|
|
})
|
|
|
|
it('signs out and returns to the local tool', async () => {
|
|
const user = userEvent.setup()
|
|
mount()
|
|
const [button] = screen.getAllByRole('button', { name: fr['shell.sign_out'] })
|
|
await user.click(button)
|
|
expect(fake.auth.signOut).toHaveBeenCalled()
|
|
expect(await screen.findByText('local tool')).toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('monogram', () => {
|
|
it('takes initials, else two letters, else ?', () => {
|
|
expect(monogram('Alice Local', undefined)).toBe('AL')
|
|
expect(monogram('Alice', undefined)).toBe('AL')
|
|
expect(monogram(undefined, 'bob@x.test')).toBe('BO')
|
|
expect(monogram('', '')).toBe('?')
|
|
})
|
|
})
|