fix(editor): focus the first refused field with its own error

A user leave with several refused fields focused the first one while
the last refusal owned the single error slot. The leave now focuses
the first refused field, then re-applies it so its message shows (I1).
The add flow no longer re-pins the tile finish() just ended (M3).
This commit is contained in:
bchanot committed 2026-11-13 14:49:17 +01:00
1 parent 78fb2d076e
commit 94f8a8bf27
4 files changed
+134 -35

No files matched your search

@@ -1,4 +1,7 @@
/** C1: a read-only flip never drops a draft silently, writes nothing, and keeps the tile. */
/**
* C1: a read-only flip never drops a draft silently, writes nothing, and keeps the tile.
* I1: a user leave with several refused fields focuses the first one, with its own error.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
@@ -201,7 +204,12 @@ describe('a read-only flip with a refused draft (tab tile)', () => {
await editTile(user, 'ref-b', /^Denoix/)
await typeBadUrl(user, 'ref-b')
cross(800)
await flush()
const watch = watchNotes()
cross(1280)
await flush()
expect(watch.seen).toHaveLength(0)
watch.stop()
const item = tile('ref-b')
expect(within(item).getByText(`${NOTE} : Adresse web (URL)`)).toBeVisible()
expect(field('ref-b', 'url')).toHaveValue('https://exemple.org/Épaule')
@@ -337,29 +345,30 @@ describe('a read-only flip without a refused draft', () => {
})
})
describe('a read-only flip in the citations panel', () => {
async function openPanel(user: User) {
await user.click(screen.getByRole('tab', { name: 'Constats' }))
await user.click(screen.getByRole('treeitem', { name: /Cat A/ }))
await user.keyboard('{ArrowRight}')
await user.click(screen.getByRole('treeitem', { name: /Node A/ }))
await user.click(
within(screen.getByRole('tabpanel', { name: 'Constats' })).getByRole(
'button',
{ name: '2 références' },
),
)
await waitFor(() =>
expect(document.activeElement).toBe(
screen.getByRole('heading', { name: 'Références citées' }),
),
)
const panel = screen.getByTestId('editor-citations-panel')
await user.click(within(panel).getByRole('button', { name: /^Denoix/ }))
await user.click(within(panel).getByRole('button', { name: 'Modifier' }))
return panel
}
/** The citations panel of Node A, its Denoix tile in edit. */
async function openPanel(user: User) {
await user.click(screen.getByRole('tab', { name: 'Constats' }))
await user.click(screen.getByRole('treeitem', { name: /Cat A/ }))
await user.keyboard('{ArrowRight}')
await user.click(screen.getByRole('treeitem', { name: /Node A/ }))
await user.click(
within(screen.getByRole('tabpanel', { name: 'Constats' })).getByRole(
'button',
{ name: '2 références' },
),
)
await waitFor(() =>
expect(document.activeElement).toBe(
screen.getByRole('heading', { name: 'Références citées' }),
),
)
const panel = screen.getByTestId('editor-citations-panel')
await user.click(within(panel).getByRole('button', { name: /^Denoix/ }))
await user.click(within(panel).getByRole('button', { name: 'Modifier' }))
return panel
}
describe('a read-only flip in the citations panel', () => {
it('shows the note in the panel tile, says it once, focuses the tile header', async () => {
const user = await mount()
await openPanel(user)
@@ -412,3 +421,85 @@ async function typeBadUrlIn(user: User) {
await user.clear(url)
await user.type(url, 'exemple.org')
}
const REQUIRED = 'Ce champ est obligatoire.'
/** Citation cleared then a schemeless URL, both refused, in the tile `id` of `host`. */
async function twoRefused(user: User, id: string, host = 'tab') {
await user.clear(field(id, 'citation', host))
await user.tab()
const url = field(id, 'url', host)
await user.clear(url)
await user.type(url, 'exemple.org')
}
/** I1: the field focused by a refused leave shows its own refusal; no draft is lost. */
function expectCitationRefused(id: string, host = 'tab') {
const citation = field(id, 'citation', host)
expect(document.activeElement).toBe(citation)
expect(citation).toHaveAttribute('aria-invalid', 'true')
expect(citation).toHaveAccessibleDescription(REQUIRED)
expect(citation).toHaveValue('')
}
describe('a user leave with several refused fields (I1)', () => {
it('keyboard « Terminer » focuses Citation with its own error', async () => {
const user = await mount()
await editTile(user, 'ref-b', /^Denoix/)
await twoRefused(user, 'ref-b')
const done = within(tile('ref-b')).getByRole('button', { name: 'Terminer' })
for (let step = 0; step < 6 && document.activeElement !== done; step++) {
await user.tab()
}
expect(document.activeElement).toBe(done)
await user.keyboard('{Enter}')
expect(done).toBeInTheDocument()
expectCitationRefused('ref-b')
expect(field('ref-b', 'url')).toHaveValue('exemple.org')
expect(history()).toEqual({ undo: 0, redo: 0 })
})
it("another tile's « Modifier » focuses Citation with its own error", async () => {
const user = await mount()
await editTile(user, 'ref-b', /^Denoix/)
await twoRefused(user, 'ref-b')
await user.click(
within(tabPanel()).getByRole('button', { name: /^Barone/ }),
)
await user.click(
within(tile('ref-a')).getByRole('button', { name: 'Modifier' }),
)
expect(field('ref-a', 'citation')).toBeNull()
expectCitationRefused('ref-b')
expect(field('ref-b', 'url')).toHaveValue('exemple.org')
})
it('the panel ✕ is cancelled, Citation focused with its own error', async () => {
const user = await mount()
const panel = await openPanel(user)
await twoRefused(user, 'ref-b', 'panel')
await user.click(
within(panel).getByRole('button', { name: 'Fermer les références' }),
)
expect(screen.getByTestId('editor-citations-panel')).toBe(panel)
expectCitationRefused('ref-b', 'panel')
expect(field('ref-b', 'url', 'panel')).toHaveValue('exemple.org')
expect(history()).toEqual({ undo: 0, redo: 0 })
})
it('a mouse « Terminer » with a valid URL focused keeps edit on Citation', async () => {
const user = await mount()
await editTile(user, 'ref-b', /^Denoix/)
await user.clear(field('ref-b', 'citation'))
await user.tab()
const url = field('ref-b', 'url')
await user.clear(url)
await user.type(url, 'https://exemple.org/neuf')
const done = within(tile('ref-b')).getByRole('button', { name: 'Terminer' })
await user.click(done)
expect(done).toBeInTheDocument()
expectCitationRefused('ref-b')
expect(doc().references[1].url).toBe('https://exemple.org/neuf')
expect(history()).toEqual({ undo: 1, redo: 0 })
})
})