Files
faunosteo/apps/app/tests/unit/editor-tabs-lists.test.tsx
T
bchanot 3baac4dbb5 feat(editor): search, sort, count and add in the Références tab
Search (citation, DOI, URL) with « Effacer la recherche » and Escape;
« Trier par » sheet order, citation (locale collator), citation count,
type (business order); every sort stable on stored order, display only:
document, stored order, ids and history unchanged (unit + e2e). Count
« {n} références » / « {n} sur {total} » counts matching references
only. « Monter » / « Descendre » only in sheet order without search
(A1). « Ajouter une référence » resets search and sort and appends a
draft tile in edit; a tile in edit stays pinned while sorted or
searched; tile state pruned of absent ids. Reset path ready for « Tout
afficher » (R2.4).

Gates: typecheck, lint, test:run (app 1452), build, e2e:ui 251;
e2e:shell 16 specs on db:sandbox (editor-equine never listed): 115
passed / 102 skipped / 2 failed: editor-structures (test-side, fixed,
rerun with editor-references 12 passed / 11 skipped) and admin.spec A0
« Target crashed » during axe on the rights screen (not R2: that
screen renders every grant, 1 282 accumulated e2e fixtures; reported as
an open risk); full-row guard: no pre-existing row changed in any table.

## R-24 (removed or rewritten assertions)
| file | old | new |
|---|---|---|
| editor-references-tab.test.tsx | `#tab-reference-new-citation` present on load | null until « Ajouter une référence » |
| same | no match text « Aucune référence ne correspond. » | notice « Aucune référence ne contient « zzz ». » |
| same | add: form always visible, add clears the search, `getByText('Nouvelle source')` | click « Ajouter une référence » first; new tile header open, focused, search empty, 4 tiles |
| same | « a refused add keeps the search » | REMOVED (P24: add resets the search at once); replaced by « draft opens with citation focused, refused add keeps the draft, no write » |
| editor-tabs-lists.test.tsx | add form directly | click « Ajouter une référence » before the form assertions (same assertions after) |
| editor-keep-alive.test.tsx | form survives tab round trip | same, after opening the draft |
| editor-no-loss.test.tsx | reference form keeps typed text | same, after opening the draft |
| e2e editor-shell.spec (2), editor-shell-crossing.spec, editor-structures.spec | fill `#tab-reference-new-*` directly; `getByText(citation)` visible | open the draft first; structures asserts the new header is focused |
| e2e editor-references.spec keyboard walk | focus search, Tab reaches header 0 | focus « Ajouter une référence » (search, sort, add precede the headers) |
Other readers of `editor.reference.add`/new ids: grep of src, tests, e2e incl. editor-equine found none left unlisted. editor-equine is not affected ("updated, not run": nothing to update).

## i18n (fr / en)
Added: `editor.references.add` Ajouter une référence / Add a reference; `add_help` Type et citation obligatoires. / Type and citation required.; `clear_search` Effacer la recherche / Clear the search; `sort_by` Trier par / Sort by; `sort.sheet|citation|count|type` Ordre de la fiche, Citation (A → Z), Nombre de citations, Type / Sheet order, Citation (A → Z), Number of citations, Type; `count_one|other` {count} référence(s) / reference(s); `count_of_one|other` {count} référence(s) sur {total} / {count} reference(s) of {total}; `reorder_hint` Pour réorganiser, affichez l'ordre de la fiche sans recherche. / To reorder, show the sheet order without a search.; `no_result` Aucune référence ne contient « {q} ». / No reference contains “{q}”.; `no_result_scope` La recherche porte sur la citation, le DOI et l'URL. / The search covers the citation, the DOI and the URL. (REVUE sentence split in two keys so the first half is bold).
Changed: none. Removed: none (`editor.references.no_match` kept, the panel's EmptyLine still uses it).
2026-11-12 21:18:56 +01:00

265 lines
9.7 KiB
TypeScript

/** Lot F: the Références tab and Vertèbres. */
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 type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
import ReferencesTab from '@/components/editor/tabs/ReferencesTab'
import RegionsSection from '@/components/editor/annotations/sections/RegionsSection'
import { useLocaleStore } from '@/state/localeStore'
import { edit, insert, point } from '../helpers/editorFixtures'
import {
baseText,
openReady,
resetStore,
store,
} from '../helpers/editorHarness'
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
vi.mock('@faunosteo/supabase', async (orig) => ({
...(await orig<typeof import('@faunosteo/supabase')>()),
getSheetVersion: vi.fn(),
downloadSheetDrawing: vi.fn(),
saveSheetDraft: vi.fn(),
getOfficialSheetAdmin: vi.fn(),
submitSheetReview: vi.fn(),
}))
const props = { readOnly: false, navigate: vi.fn(), reloadSheet: vi.fn() }
const doc = (): AnatomicalSheetDocument =>
JSON.parse(store.getState().documentText)
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
afterEach(() => {
cleanup()
resetStore()
})
const reference = (id: string, citation: string) =>
({
kind: 'reference',
value: { id, type: 'book', citation, url: null, doi: null },
}) as const
describe('Références', () => {
it('adds a reference only with a type and a citation', async () => {
await openReady()
const user = userEvent.setup()
render(<ReferencesTab {...props} />)
await user.click(
screen.getByRole('button', { name: 'Ajouter une référence' }),
)
await user.click(
screen.getByRole('button', { name: 'Ajouter la référence' }),
)
expect(screen.getByRole('alert')).toHaveTextContent('Choisissez un type')
expect(doc().references).toEqual([])
await user.selectOptions(screen.getByLabelText('Type'), 'web')
await user.type(screen.getByLabelText('Citation'), 'Anatomie du cheval')
await user.click(
screen.getByRole('button', { name: 'Ajouter la référence' }),
)
expect(doc().references).toHaveLength(1)
expect(doc().references[0]).toMatchObject({
type: 'web',
citation: 'Anatomie du cheval',
url: null,
doi: null,
})
expect(doc().references[0].id).toMatch(/^ref-[a-z2-7]{10}$/)
})
it('edits a field in a tile, applies it on blur, and reorders with the arrows', async () => {
const text = insert(
insert(baseText(), reference('ref-a', 'Alpha')),
reference('ref-b', 'Beta'),
)
await openReady({ text })
const user = userEvent.setup()
render(<ReferencesTab {...props} />)
await user.click(screen.getByRole('button', { name: /^Alpha/ }))
await user.click(screen.getByRole('button', { name: 'Modifier' }))
const url = screen.getByLabelText('Adresse web (URL)', {
selector: '#tab-reference-0-ref-a-url',
})
await user.type(url, 'https://example.org')
await user.tab()
expect(doc().references[0].url).toBe('https://example.org')
await user.click(screen.getByRole('button', { name: 'Terminer' }))
await user.click(screen.getByRole('button', { name: /^Beta/ }))
await user.click(screen.getAllByRole('button', { name: 'Monter' })[1])
expect(doc().references.map((r) => r.id)).toEqual(['ref-b', 'ref-a'])
expect(screen.getAllByRole('button', { name: 'Monter' })[0]).toBeDisabled()
})
it('a cited reference offers no deletion, only the hint; an uncited one is deleted', async () => {
let text = insert(
insert(baseText(), reference('ref-a', 'Alpha')),
reference('ref-b', 'Beta'),
)
text = insert(text, {
kind: 'zone',
value: { id: 'zone-a', name: { fr: 'Z', en: 'Z' }, categories: [] },
})
text = edit(text, {
op: 'insert',
parent: { kind: 'zone', id: 'zone-a' },
element: {
kind: 'finding_category',
value: {
id: 'fcat-a',
kind: 'dysfunction',
name: { fr: 'Boiterie', en: 'Lameness' },
description: {},
reference_ids: ['ref-a'],
review_status: 'to_confirm',
children: [],
},
},
})
await openReady({ text })
const user = userEvent.setup()
render(<ReferencesTab {...props} />)
await user.click(screen.getByRole('button', { name: /^Alpha/ }))
expect(screen.queryByRole('button', { name: 'Supprimer' })).toBeNull()
expect(screen.getByText(/Pour supprimer cette référence/)).toBeTruthy()
expect(screen.getByText('Boiterie')).toBeTruthy()
await user.click(screen.getByRole('button', { name: /^Beta/ }))
await user.click(screen.getByRole('button', { name: 'Supprimer' }))
const asked = await screen.findByRole('dialog')
await user.click(within(asked).getByRole('button', { name: 'Supprimer' }))
expect(doc().references.map((r) => r.id)).toEqual(['ref-a'])
})
it('read-only shows the facts and no control that changes the draft', async () => {
await openReady({ text: insert(baseText(), reference('ref-a', 'Alpha')) })
const user = userEvent.setup()
render(<ReferencesTab {...props} readOnly />)
await user.click(screen.getByRole('button', { name: /^Alpha/ }))
expect(screen.getByText('Aucun')).toBeTruthy()
expect(screen.queryByRole('textbox', { name: 'Citation' })).toBeNull()
expect(screen.queryByRole('button', { name: 'Modifier' })).toBeNull()
expect(screen.queryByRole('button', { name: 'Supprimer' })).toBeNull()
})
})
const region = (code: string, count: number) =>
({
kind: 'region',
value: {
code,
name: { fr: `R ${code}`, en: `R ${code}` },
count,
count_range: null,
},
}) as const
describe('Vertèbres', () => {
it('adds a region with a typed code; a bad count or half range never reaches the core', async () => {
await openReady()
const user = userEvent.setup()
render(<RegionsSection {...props} />)
await user.type(screen.getByLabelText('Code'), 'C')
await user.type(screen.getByLabelText('Nom (FR)'), 'Cervicales')
await user.type(screen.getByLabelText('Nom (EN)'), 'Cervical')
await user.type(screen.getByLabelText('Nombre de vertèbres'), 'sept')
await user.click(screen.getByRole('button', { name: 'Ajouter la région' }))
expect(screen.getByRole('alert')).toHaveTextContent('nombre entier')
await user.clear(screen.getByLabelText('Nombre de vertèbres'))
await user.type(screen.getByLabelText('Nombre de vertèbres'), '7')
await user.type(screen.getByLabelText('Plage : minimum'), '6')
await user.click(screen.getByRole('button', { name: 'Ajouter la région' }))
expect(screen.getByRole('alert')).toHaveTextContent('les deux bornes')
expect(doc().vertebral_config.regions).toEqual([])
await user.type(screen.getByLabelText('Plage : maximum'), '8')
await user.click(screen.getByRole('button', { name: 'Ajouter la région' }))
expect(doc().vertebral_config.regions).toEqual([
{
code: 'C',
name: { fr: 'Cervicales', en: 'Cervical' },
count: 7,
count_range: { min: 6, max: 8 },
},
])
})
it('edits the count, applies and clears a range, reorders', async () => {
const text = insert(insert(baseText(), region('C', 7)), region('T', 18))
await openReady({ text })
const user = userEvent.setup()
render(<RegionsSection {...props} />)
const count = screen.getByLabelText('Nombre de vertèbres', {
selector: '#region-T-count',
})
await user.clear(count)
await user.type(count, '17')
await user.tab()
expect(doc().vertebral_config.regions[1].count).toBe(17)
await user.type(
screen.getByLabelText('Plage : minimum', {
selector: '#region-T-range-min',
}),
'17',
)
await user.type(
screen.getByLabelText('Plage : maximum', {
selector: '#region-T-range-max',
}),
'19',
)
await user.click(
screen.getAllByRole('button', { name: 'Appliquer la plage' })[1],
)
expect(doc().vertebral_config.regions[1].count_range).toEqual({
min: 17,
max: 19,
})
await user.clear(
screen.getByLabelText('Plage : minimum', {
selector: '#region-T-range-min',
}),
)
await user.clear(
screen.getByLabelText('Plage : maximum', {
selector: '#region-T-range-max',
}),
)
await user.click(
screen.getAllByRole('button', { name: 'Appliquer la plage' })[1],
)
expect(doc().vertebral_config.regions[1].count_range).toBeNull()
await user.click(screen.getAllByRole('button', { name: 'Monter' })[1])
expect(doc().vertebral_config.regions.map((r) => r.code)).toEqual([
'T',
'C',
])
})
it('a region used by an annotation is refused with that annotation listed', async () => {
const used = point('anno-v', 20, 20, 3)
const value = {
...used.value,
vertebral_region_code: 'C',
} as typeof used.value
const text = insert(insert(baseText(), region('C', 7)), {
kind: 'annotation',
value,
})
await openReady({ text })
const user = userEvent.setup()
render(<RegionsSection {...props} />)
await user.click(screen.getByRole('button', { name: 'Supprimer' }))
const dialog = await screen.findByRole('dialog')
expect(
within(dialog).getByText(/utilise cette région vertébrale/),
).toBeTruthy()
expect(doc().vertebral_config.regions).toHaveLength(1)
})
it('read-only has no control', async () => {
await openReady({ text: insert(baseText(), region('C', 7)) })
render(<RegionsSection {...props} readOnly />)
expect(screen.queryByRole('textbox')).toBeNull()
expect(screen.queryByRole('button')).toBeNull()
})
})