feat(editor): cite references from the finding edit form only

This commit is contained in:
bchanot committed 2026-11-14 14:11:13 +01:00
1 parent afc1051313
commit f0e49fd447
16 files changed
+519 -395

No files matched your search

@@ -102,8 +102,8 @@ function sheetText(): string {
type User = ReturnType<typeof userEvent.setup>
async function mount(width = 1280): Promise<User> {
serverHas(sheetText(), 3)
async function mount(width = 1280, text = sheetText()): Promise<User> {
serverHas(text, 3)
const user = userEvent.setup()
mountDraft({ width, probe })
await screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
@@ -556,3 +556,134 @@ describe('final review: the category creation title', () => {
expect(title.textContent).not.toContain('editor.')
})
})
const reference = (id: string, citation: string) => ({
kind: 'reference' as const,
value: { id, type: 'book' as const, citation, url: null, doi: null },
})
/** Node A cites Alpha; Beta is cited by nobody. */
function citingText(): string {
let text = insert(sheetText(), reference('ref-a', 'Alpha'))
text = insert(text, reference('ref-b', 'Beta'))
const key = {
kind: 'finding',
zone_id: 'zone-a',
path: ['fcat-a', 'fnode-a'],
} as const
const node = JSON.parse(text).finding_zones[0].categories[0].children[0]
const element = {
kind: 'finding_node',
value: { ...node, reference_ids: ['ref-a'] },
}
return edit(text, { op: 'update', key, element } as never)
}
const nodeIds = () =>
doc().finding_zones[0].categories[0].children[0].reference_ids
const cite = (user: User, label: string) =>
user.selectOptions(inDialog().getByLabelText('Citer une référence'), label)
const save = (user: User) =>
user.click(inDialog().getByRole('button', { name: 'Enregistrer' }))
const panelOpen = () => screen.queryByTestId('editor-citations-panel')
describe('citing references lives in the edit form (owner item 1)', () => {
it('the view has no cite control and no remove; the trigger opens the sheet', async () => {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
expect(inDialog().queryByLabelText('Citer une référence')).toBeNull()
expect(inDialog().queryByRole('button', { name: /^Retirer/ })).toBeNull()
expect(
inDialog().getByRole('button', { name: '1 référence' }),
).toBeVisible()
})
it('a family view lists its citations read-only', async () => {
let text = insert(sheetText(), reference('ref-a', 'Alpha'))
const key = { kind: 'finding', zone_id: 'zone-a', path: ['fcat-a'] }
const cat = JSON.parse(text).finding_zones[0].categories[0]
const element = {
kind: 'finding_category',
value: { ...cat, reference_ids: ['ref-a'] },
}
text = edit(text, { op: 'update', key, element } as never)
const user = await mount(1280, text)
await openFinding(user, /Cat A/)
expect(inDialog().getByText('Alpha')).toBeVisible()
expect(inDialog().queryByRole('button', { name: /^Retirer/ })).toBeNull()
expect(inDialog().queryByLabelText('Citer une référence')).toBeNull()
})
it('the form stages a cite and a remove, written only by « Enregistrer »', async () => {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
await modify(user)
const before = entries()
await cite(user, 'Beta')
await user.click(inDialog().getByRole('button', { name: 'Retirer Alpha' }))
expect(nodeIds()).toEqual(['ref-a'])
expect(entries()).toBe(before)
expect(
inDialog().getByRole('button', { name: '1 référence' }),
).toBeVisible()
await save(user)
expect(nodeIds()).toEqual(['ref-b'])
expect(entries()).toBe(before + 1)
})
it('one undo restores the names and the citations together', async () => {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
await modify(user)
await user.clear(nameField())
await user.type(nameField(), 'Renommé')
await cite(user, 'Beta')
await save(user)
expect(doc().finding_zones[0].categories[0].children[0].name.fr).toBe(
'Renommé',
)
expect(nodeIds()).toEqual(['ref-a', 'ref-b'])
act(() => void store.getState().undoStep())
expect(doc().finding_zones[0].categories[0].children[0].name.fr).toBe(
'Node A',
)
expect(nodeIds()).toEqual(['ref-a'])
})
it('a cite then its remove is pristine: « Enregistrer » writes nothing', async () => {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
await modify(user)
const before = entries()
await cite(user, 'Beta')
await user.click(inDialog().getByRole('button', { name: 'Retirer Beta' }))
await save(user)
expect(entries()).toBe(before)
})
it('« Annuler » with only a citation change asks, « Abandonner » writes nothing', async () => {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
await modify(user)
const before = entries()
await cite(user, 'Beta')
await user.click(inDialog().getByRole('button', { name: 'Annuler' }))
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
expect(entries()).toBe(before)
expect(nodeIds()).toEqual(['ref-a'])
})
it('the sheet opened from the form lists the draft and cannot remove', async () => {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
await modify(user)
await cite(user, 'Beta')
await user.click(inDialog().getByRole('button', { name: '2 références' }))
await waitFor(() => expect(panelOpen()).not.toBeNull())
const sheet = within(panelOpen()!)
expect(sheet.getByText(/2 références citées/)).toBeVisible()
expect(sheet.queryByRole('button', { name: /^Retirer/ })).toBeNull()
expect(nodeIds()).toEqual(['ref-a'])
})
})
+23 -9
View File
@@ -590,7 +590,7 @@ describe('Constats tab', () => {
)
})
it('edits descriptions (blank side omitted) and the vet flag in one save, cites at once', async () => {
it('edits descriptions (blank side omitted), the vet flag and the citations in one save', async () => {
let text = treeText()
text = insert(text, {
kind: 'reference',
@@ -628,22 +628,22 @@ describe('Constats tab', () => {
await user.clear(inFindingDialog().getByLabelText('Description (FR)'))
await save()
expect(node().description).toEqual({})
// « Citer une référence » is no part of the form: it acts at once, in view mode
// the view cites nothing: « Citer une référence » is the edit form's
expect(inFindingDialog().queryByLabelText('Citer une référence')).toBeNull()
await startEdit(user)
await user.selectOptions(
inFindingDialog().getByLabelText('Citer une référence'),
'ref-a',
)
expect(node().reference_ids).toEqual([])
await save()
expect(node().reference_ids).toEqual(['ref-a'])
// a finding node lists its citations in the sheet, not inline
expect(
inFindingDialog().queryByRole('button', { name: /Retirer Alpha/ }),
).toBeNull()
expect(
inFindingDialog().getByRole('button', { name: '1 référence' }),
).toBeInTheDocument()
})
it('a family keeps its inline list of citations with « Retirer »', async () => {
it('a family cites and removes in its edit form, one save writes both', async () => {
let text = treeText()
text = insert(text, {
kind: 'reference',
@@ -660,18 +660,32 @@ describe('Constats tab', () => {
render(<FindingsHost />)
await user.click(items()[1])
const family = () => doc().finding_zones[0].categories[0]
await startEdit(user)
await user.selectOptions(
inFindingDialog().getByLabelText('Citer une référence'),
'ref-a',
)
expect(family().reference_ids).toEqual(['ref-a'])
expect(
inFindingDialog().getByRole('button', { name: /Retirer Alpha/ }),
).toBeInTheDocument()
await user.click(
inFindingDialog().getByRole('button', { name: /Retirer Alpha/ }),
)
expect(family().reference_ids).toEqual([])
expect(
inFindingDialog().getByText('Aucune référence citée.'),
).toBeInTheDocument()
await user.selectOptions(
inFindingDialog().getByLabelText('Citer une référence'),
'ref-a',
)
expect(family().reference_ids).toEqual([])
await user.click(
inFindingDialog().getByRole('button', { name: 'Enregistrer' }),
)
expect(family().reference_ids).toEqual(['ref-a'])
expect(
inFindingDialog().queryByRole('button', { name: /Retirer/ }),
).toBeNull()
})
it('the trigger shows exactly for the keys open_citations accepts', async () => {
@@ -178,16 +178,17 @@ describe('opening', () => {
expect(panel()).toHaveAttribute('aria-labelledby', 'editor-citations-title')
})
it('a family keeps its inline list: no trigger, no panel', async () => {
it('a family lists its citations read-only: no trigger, no Retirer, no panel', async () => {
const user = await mount()
await select(user, /Cat A/)
const detail = screen.getByRole('tabpanel', { name: 'Constats' })
expect(
within(detail).queryByRole('button', { name: /références?$/ }),
).toBeNull()
expect(within(detail).getByText('Barone, Ostéologie')).toBeVisible()
expect(
within(detail).getAllByRole('button', { name: /^Retirer / }),
).toHaveLength(2)
within(detail).queryByRole('button', { name: /^Retirer/ }),
).toBeNull()
expect(hasPanel()).toBe(false)
})
@@ -204,7 +205,7 @@ describe('opening', () => {
expect(panel()).not.toHaveTextContent('Denoix')
})
it('an open tile marks the current finding and offers Modifier and « Retirer de ce constat »', async () => {
it('an open tile marks the current finding and offers Modifier only', async () => {
const user = await mount()
await openFromFinding(user)
await user.click(within(panel()).getByRole('button', { name: /^Barone/ }))
@@ -214,10 +215,12 @@ describe('opening', () => {
within(tile).getAllByRole('button', { name: /^Aller à / }),
).toHaveLength(2)
expect(within(tile).getByRole('button', { name: 'Modifier' })).toBeVisible()
expect(
within(tile).getByRole('button', { name: 'Retirer de ce constat' }),
).toBeVisible()
for (const absent of ['Supprimer', 'Monter', 'Descendre']) {
for (const absent of [
'Retirer de ce constat',
'Supprimer',
'Monter',
'Descendre',
]) {
expect(within(panel()).queryByRole('button', { name: absent })).toBeNull()
}
})
@@ -486,101 +489,26 @@ describe('the library and the history around the panel', () => {
})
})
describe('« Retirer de ce constat »', () => {
const retire = (user: User, at = 0) =>
user.click(
within(panel()).getAllByRole('button', { name: 'Retirer de ce constat' })[
at
],
)
async function openTile(user: User, citation: RegExp) {
await user.click(within(panel()).getByRole('button', { name: citation }))
}
it('removes only that id, the reference stays, one undo restores it with the status', async () => {
describe('the sheet cannot remove a citation (the edit form does)', () => {
it('no tile offers « Retirer de ce constat »', async () => {
const user = await mount()
act(() => {
store.getState().commit({
op: 'set_status',
key: {
kind: 'finding',
zone_id: 'zone-a',
path: ['fcat-a', 'fnode-a'],
},
status: 'reviewed',
})
})
expect(nodeA().review_status).toBe('reviewed')
await openFromFinding(user)
const history = store.getState().undo.length
await openTile(user, /^Barone/)
await retire(user)
expect(nodeA().reference_ids).toEqual(['ref-c'])
expect(nodeA().review_status).toBe('to_confirm')
expect(doc().references).toHaveLength(3)
expect(store.getState().undo.length).toBe(history + 1)
expect(tiles()).toHaveLength(1)
expect(panel()).toHaveTextContent('1 référence citée par ce constat')
act(() => void store.getState().undoStep())
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
expect(nodeA().review_status).toBe('reviewed')
expect(tiles()).toHaveLength(2)
})
it('the focus goes to the next tile, else the previous, else the heading', async () => {
const ids = ['ref-a', 'ref-b', 'ref-c']
const user = await mount(1280, nodeText(ids))
await openFromFinding(user, '3 références')
const header = (at: number) => () =>
tiles()[at].querySelector('button') as HTMLElement
await openTile(user, /^Barone/)
await retire(user)
await focusIs(header(0))
expect(header(0)().textContent).toContain('Denoix')
await openTile(user, /^Code rural/)
await retire(user, 0)
await focusIs(header(0))
expect(tiles()).toHaveLength(1)
await openTile(user, /^Denoix/)
await retire(user)
await focusIs(heading)
})
it('the last citation gone: the panel stays with its line and « Tout afficher »', async () => {
const user = await mount(1280, nodeText(['ref-a']))
await openFromFinding(user, '1 référence')
await user.click(within(panel()).getByRole('button', { name: /^Barone/ }))
await retire(user)
expect(panel()).toHaveTextContent(
'Ce constat ne cite plus aucune référence.',
)
expect(
within(panel()).getByRole('button', {
name: "Tout afficher dans l'onglet Références",
}),
).toBeVisible()
expect(hasPanel()).toBe(true)
await user.click(closeButton())
await closed()
// the trigger is gone with its count: the focus stays inside the finding dialog
expect(findingDialog()!.contains(document.activeElement)).toBe(true)
within(panel()).queryByRole('button', { name: /^Retirer/ }),
).toBeNull()
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
})
it('a dangling id is a « Référence introuvable » row with Retirer, counted like the rest', async () => {
it('a dangling id is a « Référence introuvable » row, counted, without Retirer', async () => {
const user = await mount(1280, nodeText(['ref-a', 'ref-gone']))
await openFromFinding(user)
expect(panel()).toHaveTextContent('2 références citées par ce constat')
expect(tiles()).toHaveLength(2)
expect(tiles()[1]).toHaveTextContent('Référence introuvable')
await user.click(
within(tiles()[1] as HTMLElement).getByRole('button', {
name: /^Retirer/,
}),
)
expect(nodeA().reference_ids).toEqual(['ref-a'])
expect(tiles()).toHaveLength(1)
await focusIs(() => tiles()[0].querySelector('button')!)
expect(
within(panel()).queryByRole('button', { name: /^Retirer/ }),
).toBeNull()
})
})