Files
faunosteo/apps/app/tests/unit/shell.navigation.test.tsx
T
bchanot 580ff28214 feat(app): editor sidebar hidden below 1 280 px, menu one tap away
AC9: on the draft editor path AppLayout uses the xl class set (sidebar
from 1 280 px, menu button and brand below); other pages keep lg.
AppLayout split: Sidebar and TopBar in ShellChrome.tsx. MobileDrawer
takes closesFrom ('64rem' | '80rem') and also closes at setup when
its query already matches.

Rewritten assertions (R-24):
- shell.AppLayout "opens the mobile drawer" and shell.navigation
  "carries the same entries into the mobile drawer": stub a narrow
  matchMedia, the drawer now closes at setup at the happy-dom width.
New: unit tests (class sets, 64rem/80rem close, setup close, leave
guard on a drawer link) and editor-shell.spec F journeys (sidebar at
1024/1180/1279/1280, one-tap menu, drawer open at 1180, guard).
2026-11-08 19:25:49 +01:00

145 lines
5.7 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 type { OwnCatalogGrant, PlatformRole } from '@faunosteo/supabase'
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 { navigationHint } from '@/auth/navigationHint'
import { useAuthStore } from '@/state/authStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
const NOW = Date.parse('2026-11-02T12:00:00Z')
function grant(kind: OwnCatalogGrant['kind'], patch: Partial<OwnCatalogGrant> = {}): OwnCatalogGrant {
return { kind, expires_at: null, revoked_at: null, ...patch }
}
describe('navigationHint', () => {
it('shows nothing extra without a grant or the owner role', () => {
expect(navigationHint([], [], NOW)).toEqual({ catalog: false, admin: false })
expect(navigationHint([], ['admin', 'support'], NOW)).toEqual({ catalog: false, admin: false })
})
it('shows the catalogue to a catalogue reader only', () => {
expect(navigationHint([grant('catalog_reader')], [], NOW)).toEqual({ catalog: true, admin: false })
})
it('shows both entries to any capability holder', () => {
for (const kind of ['sheet_editor', 'sheet_reviewer', 'sheet_publisher'] as const) {
expect(navigationHint([grant(kind)], [], NOW)).toEqual({ catalog: true, admin: true })
}
})
it('shows the administration to the owner role, the catalogue only with a grant', () => {
expect(navigationHint([], ['owner'], NOW)).toEqual({ catalog: false, admin: true })
})
it('ignores revoked and expired grants, keeps one expiring later', () => {
const revoked = grant('sheet_editor', { revoked_at: '2026-10-01T00:00:00Z' })
const expired = grant('catalog_reader', { expires_at: '2026-11-01T00:00:00Z' })
expect(navigationHint([revoked, expired], [], NOW)).toEqual({ catalog: false, admin: false })
const later = grant('catalog_reader', { expires_at: '2026-12-01T00:00:00Z' })
expect(navigationHint([later], [], NOW).catalog).toBe(true)
})
})
function mount(grants: OwnCatalogGrant[], roles: PlatformRole[] = []) {
useAuthStore.setState({ catalogGrants: grants, roles })
return render(
<MemoryRouter initialEntries={['/app']}>
<Routes>
<Route path="/app" element={<AppLayout />}>
<Route index element={<p>dashboard page</p>} />
</Route>
</Routes>
</MemoryRouter>,
)
}
function sidebar() {
return screen.getByRole('navigation', { name: fr['shell.nav_label'] })
}
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
mocks.fake = createFakeSupabase() satisfies FakeSupabase
useAuthStore.setState({
status: 'signed_in',
user: fakeUser(),
profile: fakeProfile(),
avatar: null,
avatarUrl: null,
})
})
afterEach(() => {
cleanup()
vi.unstubAllGlobals()
})
describe('SidebarNav entries', () => {
it('hides both V1-2 entries from an account without a right', () => {
mount([])
expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.anatomy'] })).toBeNull()
expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.admin'] })).toBeNull()
expect(within(sidebar()).queryByRole('separator')).toBeNull()
})
it('shows « Fiches anatomiques » to a catalogue reader, without the administration', () => {
mount([grant('catalog_reader')])
expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.anatomy'] })).toHaveAttribute(
'href',
'/app/anatomy',
)
expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.admin'] })).toBeNull()
})
it('shows both to a capability holder, « Administration » set apart before « Mon compte »', () => {
mount([grant('sheet_reviewer')])
const links = within(sidebar()).getAllByRole('link').map((link) => link.textContent)
expect(links).toEqual([
fr['shell.nav.dashboard'],
fr['shell.nav.clients'],
fr['shell.nav.animals'],
fr['shell.nav.anatomy'],
fr['shell.nav.admin'],
fr['shell.nav.account'],
fr['shell.nav.local_tool'] + fr['shell.nav.local_tool_hint'],
])
expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.admin'] })).toHaveAttribute(
'href',
'/app/admin',
)
expect(within(sidebar()).getAllByRole('separator')).toHaveLength(2)
})
it('shows « Administration » to the owner role without a grant', () => {
mount([], ['owner'])
expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.admin'] })).toBeInTheDocument()
expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.anatomy'] })).toBeNull()
})
it('carries the same entries into the mobile drawer', async () => {
const user = userEvent.setup()
// A narrow screen: the drawer closes by itself once the sidebar breakpoint matches.
vi.stubGlobal('matchMedia', () => ({
matches: false,
addEventListener: () => {},
removeEventListener: () => {},
}))
mount([grant('sheet_editor')])
await user.click(screen.getByRole('button', { name: fr['shell.open_menu'] }))
const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] })
expect(within(drawer).getByRole('link', { name: fr['shell.nav.anatomy'] })).toBeInTheDocument()
expect(within(drawer).getByRole('link', { name: fr['shell.nav.admin'] })).toBeInTheDocument()
})
})