fix(editor): one history entry per reference edit, focus kept

Final review fixes. « Terminer » and every panel close re-committed the
edited field against a stale value, adding a second identical history
entry so the first « Défaire » seemed to do nothing: the tile now reads
the latest applied value (tests assert the undo-length delta). A tile
leaving the search on « Terminer » stays pinned until the next view
change and keeps focus; « Ajouter la référence » resets search and sort
and focuses the new tile. Also: refused draft dropped on a read-only
flip is announced; arrow keys respect a refused tab switch; pinned
index kept across read-only; the panel announces « Références citées »;
search text inserted literally; « Aller à » names its citer; the edit
header's name starts with « Terminer. »; undo removing the focused panel
tile moves focus per P20; no-result announced once; docs corrected.

Gates: typecheck, lint, test:run (app 1510), build, e2e:ui 251;
e2e:shell 16 specs (admin and editor-equine excluded): 117 passed / 108
skipped / 1 failed (crossing:211 stuck on the MFA step-up screen before
the editor), rerun alone 11 passed; full-row guard: no pre-existing row
changed in any table.
This commit is contained in:
bchanot committed 2026-11-13 01:08:00 +01:00
1 parent 2182307c62
commit 1bf27014b3
21 files changed
+677 -136

No files matched your search

@@ -9,6 +9,7 @@ import {
within,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import { useLocaleStore } from '@/state/localeStore'
import { edit, insert } from '../helpers/editorFixtures'
import {
@@ -205,7 +206,7 @@ describe('opening', () => {
const tile = tiles()[0] as HTMLElement
expect(within(tile).getByText('ce constat')).toBeInTheDocument()
expect(
within(tile).getAllByRole('button', { name: 'Aller à' }),
within(tile).getAllByRole('button', { name: /^Aller à / }),
).toHaveLength(2)
expect(within(tile).getByRole('button', { name: 'Modifier' })).toBeVisible()
expect(
@@ -358,7 +359,7 @@ describe.each(MODES)('closing, %s', (_label, reduced) => {
const user = await mount()
await openFromFinding(user)
await user.click(within(panel()).getByRole('button', { name: /^Barone/ }))
const go = within(panel()).getAllByRole('button', { name: 'Aller à' })
const go = within(panel()).getAllByRole('button', { name: /^Aller à / })
await user.click(go[0])
await closed()
await focusIs(() => screen.getByRole('treeitem', { name: /Cat A/ }))
@@ -705,3 +706,149 @@ describe("a tile in edit when the panel closes (P22'')", () => {
expect(doc().references[0].citation).toBe('Barone, Ostéologie')
})
})
const live = () => document.querySelector('p.sr-only[aria-live="polite"]')
const entries = () => store.getState().undo.length
const undo = () => act(() => store.getState().undoStep())
const nodeKey: ElementKey = {
kind: 'finding',
zone_id: 'zone-a',
path: ['fcat-a', 'fnode-a'],
}
/** The finding node cites `ids` from now on (one history entry). */
function cite(ids: string[]) {
act(() => {
store.getState().commit({
op: 'update',
key: nodeKey,
element: {
kind: 'finding_node',
value: {
...doc().finding_zones[0].categories[0].children[0],
reference_ids: ids,
},
},
})
})
}
describe('one user edit, one history entry in the panel (C1)', () => {
/** Opens Barone, enters edit and types a URL; the field keeps the focus. */
async function typeUrl(user: User) {
await openFromFinding(user)
await user.click(within(panel()).getByRole('button', { name: /^Barone/ }))
await user.click(within(panel()).getByRole('button', { name: 'Modifier' }))
const url = panel().querySelector<HTMLInputElement>(
'#panel-reference-0-ref-a-url',
)!
await user.type(url, 'https://a.org/')
expect(document.activeElement).toBe(url)
return entries()
}
it('✕ pressed with the field still focused (iPadOS) adds one entry', async () => {
const user = await mount()
const before = await typeUrl(user)
fireEvent.click(closeButton())
await closed()
expect(entries() - before).toBe(1)
expect(doc().references[0].url).toBe('https://a.org/')
undo()
expect(doc().references[0].url).toBeNull()
})
it('a tab switch with the field still focused adds one entry', async () => {
const user = await mount()
const before = await typeUrl(user)
fireEvent.click(screen.getByRole('tab', { name: 'Vérification' }))
await closed()
expect(entries() - before).toBe(1)
undo()
expect(doc().references[0].url).toBeNull()
})
it('a forced close (finding vanished) adds the edit once, besides the removal', async () => {
const user = await mount()
const before = await typeUrl(user)
act(() => {
store.getState().commit({ op: 'remove', key: nodeKey })
})
await closed()
expect(entries() - before).toBe(2)
expect(doc().references[0].url).toBe('https://a.org/')
})
})
describe('the panel review fixes', () => {
it('opening announces the panel title, not the tab name (M4)', async () => {
const user = await mount()
await openFromFinding(user)
expect(live()?.textContent?.trim()).toBe('Références citées')
})
it('every « Aller à » names its citer (M6)', async () => {
const user = await mount()
await openFromFinding(user)
await user.click(within(panel()).getByRole('button', { name: /^Barone/ }))
const names = within(panel())
.getAllByRole('button', { name: /^Aller à / })
.map((button) => button.getAttribute('aria-label'))
expect(new Set(names).size).toBe(names.length)
expect(names).toEqual([
'Aller à Zone A › Cat A',
'Aller à Zone A › Cat A › Node A',
])
})
it('an undo that removes the focused tile moves the focus to the next one (M8)', async () => {
const user = await mount(1280, nodeText(['ref-c']))
cite(['ref-a', 'ref-c'])
await openFromFinding(user)
await user.click(within(panel()).getByRole('button', { name: /^Barone/ }))
expect(document.activeElement).toBe(tiles()[0].querySelector('button'))
undo()
expect(tiles()).toHaveLength(1)
await focusIs(() => tiles()[0].querySelector('button')!)
expect(tiles()[0]).toHaveTextContent('Code rural')
})
it('an undo that removes the last focused tile moves the focus to the previous one (M8)', async () => {
const user = await mount(1280, nodeText(['ref-a']))
cite(['ref-a', 'ref-c'])
await openFromFinding(user)
await user.click(
within(panel()).getByRole('button', { name: /^Code rural/ }),
)
undo()
expect(tiles()).toHaveLength(1)
await focusIs(() => tiles()[0].querySelector('button')!)
expect(tiles()[0]).toHaveTextContent('Barone')
})
it('an undo that removes the only tile puts the focus on the heading (M8)', async () => {
const user = await mount(1280, nodeText([]))
cite(['ref-a'])
await openFromFinding(user, '1 référence')
await user.click(within(panel()).getByRole('button', { name: /^Barone/ }))
undo()
expect(tiles()).toHaveLength(0)
await focusIs(heading)
})
it('an arrow key in the tab row leaves the focus on the refused field (M2)', async () => {
const user = await mount()
await openFromFinding(user)
await user.click(within(panel()).getByRole('button', { name: /^Barone/ }))
await user.click(within(panel()).getByRole('button', { name: 'Modifier' }))
const field = panel().querySelector<HTMLTextAreaElement>(
'#panel-reference-0-ref-a-citation',
)!
await user.clear(field)
const tab = screen.getByRole('tab', { name: 'Constats' })
fireEvent.keyDown(tab, { key: 'ArrowRight' })
expect(hasPanel()).toBe(true)
expect(document.activeElement).toBe(field)
expect(tab).toHaveAttribute('aria-selected', 'true')
})
})
@@ -1,6 +1,7 @@
/** 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 {
act,
cleanup,
fireEvent,
render,
@@ -412,7 +413,7 @@ describe('an open tile', () => {
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.getAllByRole('button', { name: /^Aller à / })).toHaveLength(2)
expect(open.getAllByText('famille')).toHaveLength(1)
expect(open.getByText(/Zone A › Cat A ›/)).toBeInTheDocument()
})
@@ -442,7 +443,7 @@ describe('an open tile', () => {
const user = await mount()
await user.click(header('Barone'))
const [family] = within(tile('Barone')).getAllByRole('button', {
name: 'Aller à',
name: /^Aller à /,
})
await user.click(family)
expect(screen.getByRole('tab', { name: 'Constats' })).toHaveAttribute(
@@ -473,7 +474,7 @@ describe('a tile in edit', () => {
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)
expect(view.getAllByRole('button', { name: /^Aller à / })).toHaveLength(2)
for (const name of ['Modifier', 'Supprimer', 'Monter', 'Descendre']) {
expect(view.queryByRole('button', { name })).toBeNull()
}
@@ -546,9 +547,11 @@ describe('a tile in edit', () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
const view = within(item)
const before = store.getState().undo.length
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(store.getState().undo.length - before).toBe(2)
expect(doc().references[0]).toMatchObject({
doi: '10.1000/xyz',
url: 'https://a.org/',
@@ -586,13 +589,82 @@ describe('a tile in edit', () => {
})
})
describe('the disclosure button in edit (M7)', () => {
it('is named for what it does, and pressing it ends the edit', async () => {
const user = await mount()
const item = await startEdit(user, 'Barone')
const button = within(item).getByRole('button', {
name: /^Terminer\. Modifier la référence/,
})
expect(button).toHaveAttribute('aria-expanded', 'true')
await user.click(button)
expect(within(item).queryByLabelText('Citation')).toBeNull()
})
})
describe('one user edit, one history entry (C1)', () => {
const entries = () => store.getState().undo.length
const undo = () => act(() => store.getState().undoStep())
it('« Terminer » after one typed field adds one entry, the first « Défaire » undoes it', async () => {
const user = await mount()
const view = within(await startEdit(user, 'Barone'))
const before = entries()
await user.type(view.getByLabelText('Adresse web (URL)'), 'https://a.org/')
await user.click(view.getByRole('button', { name: 'Terminer' }))
expect(entries() - before).toBe(1)
expect(doc().references[0].url).toBe('https://a.org/')
undo()
expect(doc().references[0].url).toBeNull()
})
it('« Terminer » pressed with the field still focused (no pointer) adds one entry', async () => {
const user = await mount()
const view = within(await startEdit(user, 'Barone'))
const before = entries()
const url = view.getByLabelText('Adresse web (URL)')
await user.type(url, 'https://a.org/')
expect(document.activeElement).toBe(url)
fireEvent.click(view.getByRole('button', { name: 'Terminer' }))
expect(entries() - before).toBe(1)
undo()
expect(doc().references[0].url).toBeNull()
})
it('a tab switch with the field still focused adds one entry', async () => {
const user = await mount()
const view = within(await startEdit(user, 'Barone'))
const before = entries()
await user.type(view.getByLabelText('Adresse web (URL)'), 'https://a.org/')
fireEvent.click(screen.getByRole('tab', { name: 'Constats' }))
expect(entries() - before).toBe(1)
undo()
expect(doc().references[0].url).toBeNull()
})
it('a blur after the field was applied adds nothing; a changed text adds one more', async () => {
const user = await mount()
const view = within(await startEdit(user, 'Barone'))
const before = entries()
const url = view.getByLabelText('Adresse web (URL)')
await user.type(url, 'https://a.org/')
await user.tab()
await user.click(url)
await user.tab()
expect(entries() - before).toBe(1)
await user.type(url, 'x')
await user.tab()
expect(entries() - before).toBe(2)
})
})
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(cited.getAllByRole('button', { name: /^Aller à / })).toHaveLength(2)
expect(
within(tile('Denoix')).getByRole('link', { name: /exemple\.org/ }),
).toBeInTheDocument()
@@ -4,6 +4,7 @@ import { act, cleanup, render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { ReferenceType } from '@faunosteo/core-wasm/types'
import ReferencesTab from '@/components/editor/tabs/ReferencesTab'
import { AnnounceContext } from '@/editor/focusRequest'
import { targetOfKey } from '@/editor/navigation'
import { useLocaleStore } from '@/state/localeStore'
import { edit, insert } from '../helpers/editorFixtures'
@@ -208,6 +209,22 @@ describe('clearing the search', () => {
expect(order()).toHaveLength(5)
})
it('is announced once: the count line is the live region, the notice is not (M11)', async () => {
const { user } = await mount()
await user.type(search(), 'radiographie')
const live = document.querySelectorAll('[aria-live], [role="status"]')
expect(live).toHaveLength(1)
expect(live[0]).toHaveTextContent('0 référence sur 5')
})
it('writes the typed text as it is, `$&` and friends included (M5)', async () => {
const { user } = await mount()
await user.type(search(), "$& $` $'")
expect(screen.getByTestId('editor-references-no-result')).toHaveTextContent(
"Aucune référence ne contient « $& $` $' ».",
)
})
it('Escape empties a non-empty search and stops there (P12)', async () => {
const { user } = await mount()
const bubbled = vi.fn()
@@ -229,9 +246,7 @@ describe('no result (board 24 B)', () => {
const { user } = await mount()
await user.type(search(), 'radiographie')
expect(order()).toEqual([])
const notice = screen.getByRole('status', {
name: '',
})
const notice = screen.getByTestId('editor-references-no-result')
expect(notice).toHaveTextContent(
"Aucune référence ne contient « radiographie ». La recherche porte sur la citation, le DOI et l'URL.",
)
@@ -315,16 +330,43 @@ describe('the tile in edit is pinned (P23)', () => {
expect(order()).toEqual(['r5', 'r4', 'r3', 'r2', 'r1'])
})
it('stays shown under a search that does not match it, until it leaves edit', async () => {
it('stays shown under a search that does not match it, also after « Terminer », until the next search change (I1)', async () => {
const { user } = await mount()
const item = await startEdit(user, 'r1')
await user.type(search(), 'alpha')
expect(order()).toEqual(['r1', 'r5'])
expect(within(item).getByLabelText('Citation')).toBeInTheDocument()
await user.click(within(item).getByRole('button', { name: 'Terminer' }))
expect(order()).toEqual(['r1', 'r5'])
expect(document.activeElement).toBe(header('r1'))
await user.clear(search())
await user.type(search(), 'alpha')
expect(order()).toEqual(['r5'])
})
it('keeps the tile shown and focused when its edit stops matching the search (I1)', async () => {
const { user } = await mount()
await user.type(search(), 'zèbre')
const item = await startEdit(user, 'r1')
const citation = within(item).getByLabelText('Citation')
await user.clear(citation)
await user.type(citation, 'Autre titre')
await user.click(within(item).getByRole('button', { name: 'Terminer' }))
expect(order()).toEqual(['r1', 'r2'])
expect(document.activeElement).toBe(header('r1'))
})
it('keeps its index across a search or sort change made while read-only (M3)', async () => {
const { user, view } = await mount()
await user.selectOptions(sortBox(), 'citation')
await startEdit(user, 'r3')
expect(order()).toEqual(['r5', 'r3', 'r4', 'r2', 'r1'])
view.rerender(lib(true))
await user.selectOptions(sortBox(), 'count')
view.rerender(lib(false))
expect(order()).toEqual(['r2', 'r3', 'r1', 'r5', 'r4'])
})
it('is suspended in read-only: the tile renders its read view', async () => {
const { user, view } = await mount()
await startEdit(user, 'r1')
@@ -384,6 +426,22 @@ describe('adding (P24)', () => {
).toBeInTheDocument()
})
it('a search typed after « Ajouter une référence » is cleared by the add, the new tile takes the focus (I1)', async () => {
const { user } = await mount()
await user.click(add())
await user.selectOptions(screen.getByLabelText('Type'), 'book')
await user.type(draftCitation()!, 'Nouvelle source')
await user.type(search(), 'alpha')
expect(order()).toEqual(['r5'])
await user.click(
screen.getByRole('button', { name: 'Ajouter la référence' }),
)
const id = doc().references[5].id
expect(search()).toHaveValue('')
expect(order()).toHaveLength(6)
expect(document.activeElement).toBe(header(id))
})
it('a second click on the button takes the focus back to the citation', async () => {
const { user } = await mount()
await user.click(add())
@@ -492,3 +550,53 @@ describe('state of a reference that left the document (P23-prime)', () => {
).toBeNull()
})
})
describe('a read-only flip with a tile in edit (M1)', () => {
const ui = (announce: (text: string) => void, readOnly: boolean) => (
<AnnounceContext value={announce}>{lib(readOnly)}</AnnounceContext>
)
async function refusedUrl() {
await openReady({ text: sortText() })
const user = userEvent.setup()
const announce = vi.fn()
const view = render(ui(announce, false))
const item = await startEdit(user, 'r1')
await user.type(
within(item).getByLabelText('Adresse web (URL)'),
'exemple.org',
)
await user.tab()
return { view, announce, item, user }
}
it('says which refused draft is dropped, once', async () => {
const { view, announce } = await refusedUrl()
view.rerender(ui(announce, true))
expect(announce).toHaveBeenCalledTimes(1)
expect(announce).toHaveBeenCalledWith(
'Saisie non appliquée : Adresse web (URL)',
)
view.rerender(ui(announce, false))
expect(announce).toHaveBeenCalledTimes(1)
})
it('does not move the focus to Citation when editing is possible again', async () => {
const { view, announce } = await refusedUrl()
view.rerender(ui(announce, true))
view.rerender(ui(announce, false))
const citation = screen.getByLabelText('Citation')
expect(document.activeElement).not.toBe(citation)
expect(screen.getByLabelText('Adresse web (URL)')).toHaveValue('')
})
it('says nothing when no draft is lost', async () => {
await openReady({ text: sortText() })
const user = userEvent.setup()
const announce = vi.fn()
const view = render(ui(announce, false))
await startEdit(user, 'r1')
view.rerender(ui(announce, true))
expect(announce).not.toHaveBeenCalled()
})
})