Files
faunosteo/apps/app/tests/helpers/editorRoute.tsx
T

129 lines
3.5 KiB
TypeScript

import { render } from '@testing-library/react'
import { MemoryRouter, Route, Routes } 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',
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
}
/** 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: 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,
)
}
return render(
<MemoryRouter initialEntries={[`/app/admin/sheets/${SHEET}/draft`]}>
<Routes>
<Route
path="/app/admin/sheets/:sheetId/draft"
element={<AdminSheetDraftRoute />}
/>
<Route
path="/app/admin/sheets/:sheetId"
element={<p>overview page</p>}
/>
</Routes>
</MemoryRouter>,
)
}