feat(editor): collapsible reference tiles in the Références tab

Board 23 / 24 / 27 tiles: closed (citation on two lines, type, « Citée
{n} fois » / « Non citée »), open (Type, DOI, URL as safe links, the
citers with « famille » badge and « Aller à »), edit (« Terminer » with a
leave guard re-committing every changed field; a refusal keeps edit
and focuses the field). A cited reference shows the deletion hint, an
uncited one keeps « Supprimer ». Read-only keeps DOI, URL, « Aller à ».
« Terminer » keeps focus in the field on press, so a refusal message
can no longer move the button under the pointer.

Gates: typecheck, lint, test:run (app 1419), build, e2e:ui 251;
e2e:shell 16 specs on db:sandbox (editor-equine never listed): 111
passed / 99 skipped / 3 failed in the new editor-references.spec (two
test bugs, one app bug), fixed and rerun: 5 passed / 4 skipped; full-row
guard: no pre-existing row changed in any table.

## R-24 (removed or rewritten assertions)
| file | old | new |
|---|---|---|
| editor-citations.test.ts | `citationCounts` counts 2/1/0, memoised, empty | `citersOf`: ordered citers (Cat, Node), family flag, trail from zone, key, memoised, empty |
| editor-references-tab.test.tsx | cards say « citée N fois » (ref-a 1, ref-c 0) | « Citée 2 fois » (fixture gains a node citing ref-a), « Citée 1 fois », « Non citée » |
| same | ids `#tab-reference-ref-a-citation` exist when closed | header id `#tab-reference-0-ref-a-header`; field ids only inside an open edit tile |
| same | ↑/↓ on every card | in open tiles only, absent under a search |
| same | Ctrl+Z on an always-visible field | open tile, Modifier, then same assertion on `#tab-reference-0-ref-a-citation` |
| same | « Aller à » focuses the card (`listitem` count 3, focus key) | tile opened (aria-expanded true), header focused, 3 tiles |
| same | add: `getByText('Nouvelle source')` | tile header named « Nouvelle source » |
| same | read-only: no Supprimer / add | same after opening every tile |
| editor-tabs-lists.test.tsx | edit on blur and arrows on always-open cards | open tile, Modifier, blur applies, Terminer, Monter on opened tile |
| same | cited reference deletion refused in a dialog listing citers | REMOVED: no Supprimer on a cited tile (hint asserted, citer listed); uncited delete kept. The dialog's blocked path stays covered by `DeleteDialog`'s own tests |
| same | read-only: no button at all | read-only: facts shown, no Modifier / Supprimer / Citation textbox (header buttons exist now) |
| editor-keep-alive.test.tsx:~330 | tab-click commit of `#tab-reference-ref-1-citation` | opens tile + Modifier first, id `#tab-reference-0-ref-1-citation` |
| editor-keep-alive.test.tsx:~350,367 | `#panel-reference-ref-1-citation` | `#panel-reference-0-ref-1-citation` |
| editor-references-panel.test.tsx | « citée 2 fois / 1 fois » | « Citée 2 fois / 1 fois » |
| same | `#panel-reference-ref-a-citation`; no `#tab-reference-ref-a-citation` | `#panel-reference-0-ref-a-citation`; no `[id^="tab-reference-"]` while the tab tiles are closed |
| same | ids unique, tab and panel field ids present | tab tile opened in edit first; `tab-reference-0-ref-a-citation` and `panel-reference-0-ref-a-citation` unique |
| e2e editor-structures.spec.ts:~88-110 | citation inputs count 3/1/2, « citée 0 fois » x2, « citée 1 fois », move via row buttons | tile headers count, « Non citée » x2, « Citée 1 fois », tile opened before Monter/Supprimer, `tiles.nth(1)` contains the moved text (saved-list assertion unchanged) |

## i18n (fr / en)
Changed (in place, P18): `editor.references.cited_one|other`: « citée {count} fois » -> « Citée {count} fois »; « cited {count} time(s) » -> « Cited {count} time(s) ».
Added: `editor.references.uncited` Non citée / Not cited; `citers_one|other` « Citée {count} fois, par : » / « Cited {count} time(s), by: »; `no_citers` « Aucun constat ni aucune famille ne cite cette référence. » / « No finding or family cites this reference. »; `family` famille / family; `go_to` Aller à / Go to; `edit` Modifier / Edit; `done` Terminer / Done; `edit_title` Modifier la référence / Edit the reference; `url_label` URL / URL; `none_doi` Aucun / None; `none_url` Aucune / None; `new_tab` (nouvel onglet) / (new tab) (sr-only); `delete_hint` (REVUE wording, fr and en).
Removed: none. R10 grep (src, tests, e2e incl. editor-equine) found no reader of the old lowercase wording left.
This commit is contained in:
bchanot committed 2026-11-12 20:50:25 +01:00
1 parent 4499bc740c
commit c21995b3fb
26 files changed
+1532 -155

No files matched your search

@@ -1,4 +1,4 @@
/** R2.1: the Références tab, the library (search, order, counts, add, « Aller à »). */
/** R2.1 and R2.2: the Références tab, the library (search, order, counts, add, « Aller à »). */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
cleanup,
@@ -8,6 +8,7 @@ import {
within,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import ReferenceFacts from '@/components/editor/references/ReferenceFacts'
import ReferenceLibrary from '@/components/editor/references/ReferenceLibrary'
import { useLocaleStore } from '@/state/localeStore'
import { targetOfKey } from '@/editor/navigation'
@@ -78,19 +79,48 @@ function sheetText(): string {
})
}
/** The sheet with a finding under « Cat A » that cites ref-a too (ref-a: two citers). */
function citedTwiceText(): string {
return edit(sheetText(), {
op: 'insert',
parent: { kind: 'finding', zone_id: 'zone-a', path: ['fcat-a'] },
element: {
kind: 'finding_node',
value: {
id: 'fnode-a',
name: name('Node A'),
description: {},
requires_vet_referral: false,
reference_ids: ['ref-a'],
review_status: 'to_confirm',
children: [],
},
},
})
}
type User = ReturnType<typeof userEvent.setup>
const ready = () =>
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
const tabPanel = () => screen.getByRole('tabpanel', { name: 'Références' })
const cards = () => within(tabPanel()).getAllByRole('listitem')
const header = (citation: string) =>
within(tabPanel()).getByRole('button', { name: new RegExp(`^${citation}`) })
const openAll = async (user: User) => {
for (const button of within(tabPanel()).getAllByRole('button', {
expanded: false,
})) {
if (button.hasAttribute('aria-controls')) await user.click(button)
}
}
const search = () =>
within(tabPanel()).getByRole('searchbox', {
name: 'Rechercher une référence',
})
type User = ReturnType<typeof userEvent.setup>
async function mount(width = 1280) {
serverHas(sheetText(), 3)
async function mount(width = 1280, text = citedTwiceText()) {
serverHas(text, 3)
const user = userEvent.setup()
mountDraft({ width })
await ready()
@@ -119,10 +149,11 @@ describe('the Références tab', () => {
it('each card says how often it is cited, the ids carry the tab prefix', async () => {
await mount()
const texts = cards().map((card) => card.textContent)
expect(texts[0]).toContain('citée 1 fois')
expect(texts[2]).toContain('citée 0 fois')
expect(texts[0]).toContain('Citée 2 fois')
expect(texts[1]).toContain('Citée 1 fois')
expect(texts[2]).toContain('Non citée')
expect(
tabPanel().querySelector('#tab-reference-ref-a-citation'),
tabPanel().querySelector('#tab-reference-0-ref-a-header'),
).not.toBeNull()
expect(
tabPanel().querySelector('#tab-reference-new-citation'),
@@ -146,8 +177,9 @@ describe('the Références tab', () => {
).toBeInTheDocument()
})
it('shows ↑ / ↓ only without a search', async () => {
it('shows ↑ / ↓ in open tiles, and only without a search', async () => {
const user = await mount()
await openAll(user)
expect(
within(tabPanel()).getAllByRole('button', { name: 'Descendre' }).length,
).toBe(3)
@@ -159,8 +191,12 @@ describe('the Références tab', () => {
it('Ctrl+Z reverts a reference edit', async () => {
const user = await mount()
await user.click(header('Barone'))
await user.click(
within(tabPanel()).getByRole('button', { name: 'Modifier' }),
)
const field = within(tabPanel()).getByLabelText('Citation', {
selector: '#tab-reference-ref-a-citation',
selector: '#tab-reference-0-ref-a-citation',
})
await user.clear(field)
await user.type(field, 'Neuf')
@@ -174,7 +210,8 @@ describe('the Références tab', () => {
describe('read-only modes', () => {
it('show no edit control, only the search', async () => {
await mount(800)
const user = await mount(800)
await openAll(user)
expect(
within(tabPanel()).queryByRole('button', { name: 'Supprimer' }),
).toBeNull()
@@ -188,11 +225,13 @@ describe('read-only modes', () => {
})
describe('« Aller à » a reference', () => {
it('clears the search that hides it and focuses its card', async () => {
it('clears the search that hides it, opens its tile and focuses its button', async () => {
serverHas(sheetText(), 3)
await openReady({ text: sheetText() })
const user = userEvent.setup()
const view = render(<ReferenceLibrary readOnly={false} />)
const view = render(
<ReferenceLibrary readOnly={false} navigate={vi.fn()} />,
)
const box = screen.getByRole('searchbox', {
name: 'Rechercher une référence',
})
@@ -202,12 +241,15 @@ describe('« Aller à » a reference', () => {
target: targetOfKey({ kind: 'reference', id: 'ref-a' }),
serial: 1,
}
view.rerender(<ReferenceLibrary readOnly={false} focus={focus} />)
view.rerender(
<ReferenceLibrary readOnly={false} navigate={vi.fn()} focus={focus} />,
)
expect(box).toHaveValue('')
expect(screen.getAllByRole('listitem')).toHaveLength(3)
expect(document.querySelectorAll('button[aria-controls]')).toHaveLength(3)
expect(document.activeElement?.getAttribute('data-focus-key')).toContain(
'ref-a',
)
expect(document.activeElement).toHaveAttribute('aria-expanded', 'true')
})
})
@@ -215,17 +257,25 @@ describe('the reset signal', () => {
it('empties the search once per bump and leaves other state alone', async () => {
await openReady({ text: sheetText() })
const user = userEvent.setup()
const view = render(<ReferenceLibrary readOnly={false} resetSignal={0} />)
const view = render(
<ReferenceLibrary readOnly={false} navigate={vi.fn()} resetSignal={0} />,
)
const box = screen.getByRole('searchbox', {
name: 'Rechercher une référence',
})
await user.type(box, 'Code')
view.rerender(<ReferenceLibrary readOnly={false} resetSignal={0} />)
view.rerender(
<ReferenceLibrary readOnly={false} navigate={vi.fn()} resetSignal={0} />,
)
expect(box).toHaveValue('Code')
view.rerender(<ReferenceLibrary readOnly={false} resetSignal={1} />)
view.rerender(
<ReferenceLibrary readOnly={false} navigate={vi.fn()} resetSignal={1} />,
)
expect(box).toHaveValue('')
await user.type(box, 'a')
view.rerender(<ReferenceLibrary readOnly={false} resetSignal={1} />)
view.rerender(
<ReferenceLibrary readOnly={false} navigate={vi.fn()} resetSignal={1} />,
)
expect(box).toHaveValue('a')
})
})
@@ -247,7 +297,7 @@ describe('adding a reference', () => {
await add(user)
expect(search()).toHaveValue('')
expect(cards()).toHaveLength(4)
expect(within(tabPanel()).getByText('Nouvelle source')).toBeInTheDocument()
expect(header('Nouvelle source')).toBeInTheDocument()
})
it('a refused add keeps the search', async () => {
@@ -259,3 +309,277 @@ describe('adding a reference', () => {
expect(search()).toHaveValue('Barone')
})
})
const tile = (citation: string) => header(citation).closest('li')!
const doc = () => JSON.parse(store.getState().documentText)
/** Opens the tile and enters edit; answers the tile, whose header then names the edit. */
async function startEdit(user: User, citation: string) {
const item = tile(citation)
await user.click(header(citation))
await user.click(within(item).getByRole('button', { name: 'Modifier' }))
return item
}
describe('the tile disclosure', () => {
it('opens and closes from the keyboard, and several stay open', async () => {
const user = await mount()
const first = header('Barone')
expect(first).toHaveAttribute('aria-expanded', 'false')
expect(first).toHaveAttribute('aria-controls', 'tab-reference-0-ref-a-body')
first.focus()
await user.keyboard('{Enter}')
expect(first).toHaveAttribute('aria-expanded', 'true')
header('Denoix').focus()
await user.keyboard(' ')
expect(first).toHaveAttribute('aria-expanded', 'true')
expect(header('Denoix')).toHaveAttribute('aria-expanded', 'true')
first.focus()
await user.keyboard('{Enter}')
expect(first).toHaveAttribute('aria-expanded', 'false')
expect(tabPanel().querySelector('#tab-reference-0-ref-a-body')).toBeNull()
})
it('is a natural tab stop that carries only the focus key', async () => {
await mount()
const button = header('Barone')
expect(button).not.toHaveAttribute('tabindex')
expect(button.closest('[data-focus-key]')).toBe(button)
})
})
describe('an open tile', () => {
it('shows type, DOI and URL as safe links, « Aucun » / « Aucune » when absent', async () => {
const user = await mount()
await user.click(header('Code rural'))
await user.click(header('Denoix'))
const doi = within(tile('Code rural')).getByRole('link', {
name: /10\.1000\/ABC/,
})
expect(doi).toHaveAttribute('href', 'https://doi.org/10.1000/ABC')
expect(doi).toHaveAttribute('target', '_blank')
expect(doi).toHaveAttribute('rel', 'noopener noreferrer')
expect(within(tile('Code rural')).getByText('Aucune')).toBeInTheDocument()
const url = within(tile('Denoix')).getByRole('link', {
name: /exemple\.org/,
})
expect(url).toHaveAttribute('href', 'https://exemple.org/%C3%89paule')
expect(within(tile('Denoix')).getByText('Aucun')).toBeInTheDocument()
})
it('lists the citers, a family marked, and the count is the list length', async () => {
const user = await mount()
await user.click(header('Barone'))
const open = within(tile('Barone'))
expect(open.getByText('Citée 2 fois, par :')).toBeInTheDocument()
expect(open.getAllByRole('button', { name: 'Aller à' })).toHaveLength(2)
expect(open.getAllByText('famille')).toHaveLength(1)
expect(open.getByText(/Zone A › Cat A ›/)).toBeInTheDocument()
})
it('says so when nothing cites it, and offers « Supprimer » only then', async () => {
const user = await mount()
await user.click(header('Barone'))
await user.click(header('Code rural'))
const cited = within(tile('Barone'))
expect(cited.queryByRole('button', { name: 'Supprimer' })).toBeNull()
expect(
cited.getByText(/Pour supprimer cette référence/),
).toBeInTheDocument()
const uncited = within(tile('Code rural'))
expect(
uncited.getByText(
'Aucun constat ni aucune famille ne cite cette référence.',
),
).toBeInTheDocument()
expect(
uncited.getByRole('button', { name: 'Supprimer' }),
).toBeInTheDocument()
expect(uncited.queryByText(/Pour supprimer cette référence/)).toBeNull()
})
it('« Aller à » on a citer opens the Constats tab on it', async () => {
const user = await mount()
await user.click(header('Barone'))
const [family] = within(tile('Barone')).getAllByRole('button', {
name: 'Aller à',
})
await user.click(family)
expect(screen.getByRole('tab', { name: 'Constats' })).toHaveAttribute(
'aria-selected',
'true',
)
})
it('never links a javascript: address', () => {
const risky = {
id: 'ref-x',
type: 'web',
citation: 'X',
url: 'javascript:alert(1)',
doi: 'javascript:alert(2)',
} as const
render(<ReferenceFacts reference={risky} />)
expect(screen.queryByRole('link')).toBeNull()
expect(screen.getByText('javascript:alert(1)')).toBeInTheDocument()
})
})
describe('a tile in edit', () => {
it('shows the fields, the citers and « Terminer », and hides the other actions', async () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
const view = within(item)
expect(view.getByText('Modifier la référence')).toBeInTheDocument()
expect(document.activeElement).toBe(view.getByLabelText('Citation'))
expect(view.getByText('Citée 2 fois, par :')).toBeInTheDocument()
expect(view.getAllByRole('button', { name: 'Aller à' })).toHaveLength(2)
for (const name of ['Modifier', 'Supprimer', 'Monter', 'Descendre']) {
expect(view.queryByRole('button', { name })).toBeNull()
}
await user.click(view.getByRole('button', { name: 'Terminer' }))
expect(view.queryByLabelText('Citation')).toBeNull()
expect(document.activeElement).toBe(
item.querySelector('button[aria-expanded]'),
)
expect(view.getByRole('button', { name: 'Modifier' })).toBeInTheDocument()
})
it('keeps edit and focuses the field when the URL is refused', async () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
const view = within(item)
const url = view.getByLabelText('Adresse web (URL)')
await user.type(url, 'exemple.org')
await user.click(view.getByRole('button', { name: 'Terminer' }))
expect(view.getByRole('button', { name: 'Terminer' })).toBeInTheDocument()
expect(document.activeElement).toBe(url)
expect(view.getByRole('alert')).toBeInTheDocument()
expect(doc().references[0].url).toBeNull()
})
it('keeps edit and focuses the citation when it is left blank', async () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
const view = within(item)
const citation = view.getByLabelText('Citation')
await user.clear(citation)
await user.click(view.getByRole('button', { name: 'Terminer' }))
expect(view.getByRole('button', { name: 'Terminer' })).toBeInTheDocument()
expect(document.activeElement).toBe(citation)
expect(view.getByText('Ce champ est obligatoire.')).toBeInTheDocument()
expect(doc().references[0].citation).toBe('Barone, Ostéologie')
})
it('a refused field A, then B applied, then « Terminer » keeps edit', async () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
const view = within(item)
const url = view.getByLabelText('Adresse web (URL)')
await user.type(url, 'exemple.org')
await user.tab()
await user.type(view.getByLabelText('DOI'), '10.1000/xyz')
await user.tab()
expect(doc().references[0].doi).toBe('10.1000/xyz')
await user.click(view.getByRole('button', { name: 'Terminer' }))
expect(view.getByRole('button', { name: 'Terminer' })).toBeInTheDocument()
expect(document.activeElement).toBe(url)
})
it('a press on « Terminer » keeps the focus in the field (no refusal shifts it), the click keeps edit', async () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
const view = within(item)
const url = view.getByLabelText('Adresse web (URL)')
await user.type(url, 'exemple.org')
const done = view.getByRole('button', { name: 'Terminer' })
await user.pointer({ keys: '[MouseLeft>]', target: done })
expect(view.queryByRole('alert')).toBeNull()
expect(document.activeElement).toBe(url)
await user.pointer({ keys: '[/MouseLeft]', target: done })
expect(view.getByRole('alert')).toBeInTheDocument()
expect(document.activeElement).toBe(url)
expect(view.getByRole('button', { name: 'Terminer' })).toBeInTheDocument()
})
it('applies every pending field on « Terminer », as one history entry each', async () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
const view = within(item)
await user.type(view.getByLabelText('DOI'), '10.1000/xyz')
await user.type(view.getByLabelText('Adresse web (URL)'), 'https://a.org/')
await user.click(view.getByRole('button', { name: 'Terminer' }))
expect(doc().references[0]).toMatchObject({
doi: '10.1000/xyz',
url: 'https://a.org/',
})
expect(view.queryByLabelText('Citation')).toBeNull()
})
it('switching to another tile leaves edit only when the fields apply', async () => {
const user = await mount()
const barone = await startEdit(user, 'Barone')
const denoix = tile('Denoix')
const url = within(barone).getByLabelText('Adresse web (URL)')
await user.type(url, 'exemple.org')
await user.click(header('Denoix'))
const modify = () =>
within(denoix).getByRole('button', { name: 'Modifier' })
await user.click(modify())
expect(within(barone).getByLabelText('Citation')).toBeInTheDocument()
expect(within(denoix).queryByLabelText('Citation')).toBeNull()
await user.clear(url)
await user.click(modify())
expect(within(barone).queryByLabelText('Citation')).toBeNull()
expect(within(denoix).getByLabelText('Citation')).toBeInTheDocument()
})
it('survives a tab change with the typed text', async () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
await user.type(within(item).getByLabelText('DOI'), '10.1000/kept')
await user.click(screen.getByRole('tab', { name: 'Constats' }))
await user.click(screen.getByRole('tab', { name: 'Références' }))
const view = within(item)
expect(view.getByLabelText('DOI')).toHaveValue('10.1000/kept')
expect(doc().references[0].doi).toBe('10.1000/kept')
})
})
describe('read-only tiles', () => {
it('keep DOI, URL and « Aller à », and offer no edit control', async () => {
const user = await mount(800)
await user.click(header('Barone'))
await user.click(header('Denoix'))
const cited = within(tile('Barone'))
expect(cited.getAllByRole('button', { name: 'Aller à' })).toHaveLength(2)
expect(
within(tile('Denoix')).getByRole('link', { name: /exemple\.org/ }),
).toBeInTheDocument()
expect(
within(tabPanel()).queryByRole('button', { name: 'Modifier' }),
).toBeNull()
expect(
within(tabPanel()).queryByRole('textbox', { name: 'Citation' }),
).toBeNull()
})
})
describe('reopening the editor', () => {
it('starts with every tile closed and none in edit', async () => {
const user = await mount()
await startEdit(user, 'Barone')
await user.click(header('Denoix'))
cleanup()
await mount()
const opened = within(tabPanel()).queryAllByRole('button', {
expanded: true,
})
expect(opened).toHaveLength(0)
expect(
within(tabPanel()).queryByLabelText('Citation', {
selector: '#tab-reference-0-ref-a-citation',
}),
).toBeNull()
})
})