feat(editor): cancel adding a reference by button or Escape

Draft tile gets « Annuler l'ajout »; Escape cancels from non-text targets and
from a pristine text field, never while composing or already handled. Nothing
reaches the document, history untouched, focus back on the add button.
This commit is contained in:
bchanot committed 2026-11-13 12:39:57 +01:00
1 parent 1e19479853
commit d1779928a9
10 files changed
+1138 -524

No files matched your search

@@ -351,6 +351,133 @@ describe('adding a reference', () => {
})
})
describe("« Annuler l'ajout » (C2)", () => {
const addButton = () =>
within(tabPanel()).getByRole('button', { name: 'Ajouter une référence' })
const field = (id: string) =>
tabPanel().querySelector<HTMLElement>(`#tab-reference-new-${id}`)!
const draftTile = () =>
within(tabPanel()).queryByLabelText('Nouvelle référence')
const cancelButton = () =>
within(tabPanel()).getByRole('button', { name: "Annuler l'ajout" })
const history = () => ({
undo: store.getState().undo.length,
redo: store.getState().redo.length,
})
const refs = () => JSON.parse(store.getState().documentText).references
/** One real change undone, so a redo is pending; then the draft is opened. */
async function openWithPendingRedo() {
const user = await mount()
await startEditAndType(user)
act(() => store.getState().undoStep())
await user.click(addButton())
return user
}
async function startEditAndType(user: User) {
await user.click(header('Barone, Ostéologie'))
const item = header('Barone, Ostéologie').closest('li')!
await user.click(within(item).getByRole('button', { name: 'Modifier' }))
await user.type(within(item).getByLabelText('DOI'), '10.1/x')
await user.click(within(item).getByRole('button', { name: 'Terminer' }))
}
function expectCancelled(before: { refs: unknown; history: object }) {
expect(draftTile()).toBeNull()
expect(refs()).toEqual(before.refs)
expect(history()).toEqual(before.history)
expect(document.activeElement).toBe(addButton())
}
const snapshot = () => ({ refs: refs(), history: history() })
it('the button removes the tile, writes nothing, keeps a pending redo', async () => {
const user = await openWithPendingRedo()
expect(history().redo).toBe(1)
const before = snapshot()
await user.click(cancelButton())
expectCancelled(before)
})
it('Escape on the type select and on a button does the same', async () => {
const user = await openWithPendingRedo()
const before = snapshot()
await user.type(field('citation'), 'Brouillon')
fireEvent.keyDown(field('type'), { key: 'Escape' })
expectCancelled(before)
await user.click(addButton())
await user.type(field('citation'), 'Brouillon')
fireEvent.keyDown(cancelButton(), { key: 'Escape' })
expectCancelled(before)
})
it('Escape in a field cancels while the form is pristine, not once typed', async () => {
const user = await openWithPendingRedo()
const before = snapshot()
fireEvent.keyDown(field('citation'), { key: 'Escape' })
expectCancelled(before)
await user.click(addButton())
await user.type(field('citation'), 'Brouillon')
fireEvent.keyDown(field('citation'), { key: 'Escape' })
expect(draftTile()).not.toBeNull()
expect(field('citation')).toHaveValue('Brouillon')
})
it('Escape while composing, or already handled, does nothing', async () => {
const user = await mount()
await user.click(addButton())
fireEvent.keyDown(field('type'), { key: 'Escape', isComposing: true })
fireEvent.keyDown(field('type'), { key: 'Escape', keyCode: 229 })
expect(draftTile()).not.toBeNull()
const handled = new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
cancelable: true,
})
handled.preventDefault()
act(() => void field('type').dispatchEvent(handled))
expect(draftTile()).not.toBeNull()
})
it('Escape that cancels is marked handled', async () => {
const user = await mount()
await user.click(addButton())
const event = new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
cancelable: true,
})
act(() => void field('type').dispatchEvent(event))
expect(event.defaultPrevented).toBe(true)
})
it('goes with its refusal message, and a new add starts from an empty form', async () => {
const user = await mount()
await user.click(addButton())
await user.type(field('citation'), 'Brouillon')
await user.click(
within(tabPanel()).getByRole('button', { name: 'Ajouter la référence' }),
)
expect(within(tabPanel()).getByRole('alert')).toBeInTheDocument()
await user.click(cancelButton())
expect(within(tabPanel()).queryByRole('alert')).toBeNull()
await user.click(addButton())
expect(field('citation')).toHaveValue('')
expect(field('type')).toHaveValue('')
expect(within(tabPanel()).queryByRole('alert')).toBeNull()
})
it('« Défaire » after a cancel undoes the previous real change', async () => {
const user = await mount()
await startEditAndType(user)
expect(history().undo).toBe(1)
await user.click(addButton())
await user.click(cancelButton())
expect(history().undo).toBe(1)
act(() => store.getState().undoStep())
expect(history()).toEqual({ undo: 0, redo: 1 })
expect(refs()[0].doi).toBeNull()
})
})
const tile = (citation: string) => header(citation).closest('li')!
const doc = () => JSON.parse(store.getState().documentText)