Files
faunosteo/apps/app/tests/helpers/editorRoute.tsx
T
bchanot d9a5846d0d feat(supabase): admin sheet summary carries last_modified_at
Schema requires last_modified_at as an ISO 8601 timestamp with offset. Fixtures in routes.admin.test.tsx and editorRoute.tsx gain the key.
2026-11-16 15:47:54 +01:00

159 lines
4.6 KiB
TypeScript

import { StrictMode } from 'react'
import { render } from '@testing-library/react'
import {
MemoryRouter,
Route,
Routes,
useLocation,
useNavigate,
type InitialEntry,
type Location,
type NavigateFunction,
} from 'react-router-dom'
import {
SheetFailure,
type OwnCatalogGrant,
type PlatformRole,
type SheetFailureKind,
} from '@faunosteo/supabase'
import AdminSheetDraftRoute from '@/app/routes/app/admin/AdminSheetDraftRoute'
import { useAuthStore } from '@/state/authStore'
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
import { useLocaleStore } from '@/state/localeStore'
import { SHEET, USER, VERSION, sb } from './editorHarness'
/** The route harness: the calling file declares the same module mocks as the store harness. */
export const grant = (kind: OwnCatalogGrant['kind']): OwnCatalogGrant => ({
kind,
expires_at: null,
revoked_at: null,
})
export function adminAnswer(patch: Record<string, unknown> = {}) {
return {
sheet_id: SHEET,
species_id: 'equine',
slug: 'cheval',
status: 'active',
archived_at: null,
created_at: '2026-11-01T10:00:00Z',
updated_at: '2026-11-01T10:00:00Z',
last_modified_at: '2026-11-01T10:00:00.000000+00:00',
name: {
fr: 'Cheval, squelette et rachis',
en: 'Horse, skeleton and spine',
},
historical: false,
working_copy: true,
working_copy_active: null,
working_copy_source: null,
latest_version: {
version_id: VERSION,
version_number: 4,
status: 'draft',
revision: 3,
},
current_version: null,
draft_version: { version_id: VERSION, version_number: 4, revision: 3 },
draft_last_save: null,
version_statuses: ['draft'],
open_rounds: 0,
latest_round: null,
versions: [],
rounds: [],
...patch,
}
}
export function round(status: string, patch: Record<string, unknown> = {}) {
return {
review_id: 'r-1',
version_id: VERSION,
version_number: 4,
round: 1,
revision: 3,
status,
submitted_by: null,
submitted_at: null,
verified_at: null,
failure_outcome: null,
failure_code: null,
decided_by: null,
decided_at: null,
withdrawn_at: null,
...patch,
}
}
export function setWidth(width: number): void {
Object.defineProperty(window, 'innerWidth', {
configurable: true,
value: width,
})
}
export interface MountOptions {
grants?: OwnCatalogGrant[]
roles?: PlatformRole[]
grantsStatus?: 'loading' | 'ready' | 'failed'
width?: number
answer?: Record<string, unknown>
/** The admin read is refused with this closed kind. */
adminFailure?: SheetFailureKind
/** Renders inside <StrictMode>: effects run twice on mount, as in development. */
strict?: boolean
/** The router's history before the draft: the draft is the last entry (default: only it). */
entries?: InitialEntry[]
/** Called on every render with the router's navigate and location: Back, Forward, state. */
probe?: (navigate: NavigateFunction, location: Location) => void
/** The signed-in account's id (default: USER). */
user?: string
}
/** Hands the router to a test: MemoryRouter listens to no popstate, a test calls navigate(-1). */
function RouterProbe({ probe }: { probe: NonNullable<MountOptions['probe']> }) {
probe(useNavigate(), useLocation())
return null
}
/** Mounts the draft route as an editor with the admin role, at a wide screen, by default. */
export function mountDraft(options: MountOptions = {}) {
setWidth(options.width ?? 1280)
useLocaleStore.setState({ locale: 'fr' })
useLeaveGuardStore.setState({ armed: false })
useAuthStore.setState({
user: { id: options.user ?? USER } as never,
catalogGrants: options.grants ?? [grant('sheet_editor')],
catalogGrantsStatus: options.grantsStatus ?? 'ready',
roles: options.roles ?? ['admin'],
})
if (options.adminFailure) {
sb.getOfficialSheetAdmin.mockRejectedValue(
new SheetFailure(options.adminFailure),
)
} else {
sb.getOfficialSheetAdmin.mockResolvedValue(
(options.answer ?? adminAnswer()) as never,
)
}
const page = (
<MemoryRouter
initialEntries={options.entries ?? [`/app/admin/sheets/${SHEET}/draft`]}
initialIndex={options.entries ? options.entries.length - 1 : undefined}
>
{options.probe && <RouterProbe probe={options.probe} />}
<Routes>
<Route
path="/app/admin/sheets/:sheetId/draft"
element={<AdminSheetDraftRoute />}
/>
<Route
path="/app/admin/sheets/:sheetId"
element={<p>overview page</p>}
/>
</Routes>
</MemoryRouter>
)
return render(options.strict ? <StrictMode>{page}</StrictMode> : page)
}