test(editor): reference dialog, filter and the owner journey, specs follow the dialog

This commit is contained in:
bchanot committed 2026-11-13 20:27:01 +01:00
1 parent 7a64daf251
commit 8b42816270
20 files changed
+1546 -1171

No files matched your search

@@ -1,16 +1,15 @@
/** 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,
screen,
waitFor,
within,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import ReferenceFacts from '@/components/editor/references/ReferenceFacts'
import ReferencesTab from '@/components/editor/tabs/ReferencesTab'
import { useLocaleStore } from '@/state/localeStore'
import { targetOfKey } from '@/editor/navigation'
import { edit, insert } from '../helpers/editorFixtures'
@@ -22,6 +21,7 @@ import {
store,
} from '../helpers/editorHarness'
import { mountDraft, setWidth } from '../helpers/editorRoute'
import ReferencesHost from '../helpers/referencesHost'
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
vi.mock('@faunosteo/supabase', async (orig) => ({
@@ -188,19 +188,19 @@ describe('the Références tab', () => {
).toBeNull()
})
it('Ctrl+Z reverts a reference edit', async () => {
it('Ctrl+Z reverts a reference edit made in the dialog, once it is closed', 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-0-ref-a-citation',
})
await startEdit(user, 'Barone')
const dialog = screen.getByRole('dialog', { name: 'Modifier la référence' })
const field = within(dialog).getByLabelText('Citation')
await user.clear(field)
await user.type(field, 'Neuf')
fireEvent.blur(field)
const doc = () => JSON.parse(store.getState().documentText)
await user.click(
within(dialog).getByRole('button', { name: 'Enregistrer' }),
)
await waitFor(() =>
expect(document.querySelector('dialog[open]')).toBeNull(),
)
expect(doc().references[0].citation).toBe('Neuf')
fireEvent.keyDown(document.body, { key: 'z', ctrlKey: true })
expect(doc().references[0].citation).toBe('Barone, Ostéologie')
@@ -228,13 +228,7 @@ describe('« Aller à » a reference', () => {
serverHas(sheetText(), 3)
await openReady({ text: sheetText() })
const user = userEvent.setup()
const view = render(
<ReferencesTab
readOnly={false}
navigate={vi.fn()}
reloadSheet={vi.fn()}
/>,
)
const view = render(<ReferencesHost />)
const box = screen.getByRole('searchbox', {
name: 'Rechercher une référence',
})
@@ -244,14 +238,7 @@ describe('« Aller à » a reference', () => {
target: targetOfKey({ kind: 'reference', id: 'ref-a' }),
serial: 1,
}
view.rerender(
<ReferencesTab
readOnly={false}
navigate={vi.fn()}
reloadSheet={vi.fn()}
focus={focus}
/>,
)
view.rerender(<ReferencesHost focus={focus} />)
expect(box).toHaveValue('')
expect(document.querySelectorAll('button[aria-controls]')).toHaveLength(3)
expect(document.activeElement?.getAttribute('data-focus-key')).toContain(
@@ -265,45 +252,17 @@ 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(
<ReferencesTab
readOnly={false}
navigate={vi.fn()}
reloadSheet={vi.fn()}
libraryReset={0}
/>,
)
const view = render(<ReferencesHost libraryReset={0} />)
const box = screen.getByRole('searchbox', {
name: 'Rechercher une référence',
})
await user.type(box, 'Code')
view.rerender(
<ReferencesTab
readOnly={false}
navigate={vi.fn()}
reloadSheet={vi.fn()}
libraryReset={0}
/>,
)
view.rerender(<ReferencesHost libraryReset={0} />)
expect(box).toHaveValue('Code')
view.rerender(
<ReferencesTab
readOnly={false}
navigate={vi.fn()}
reloadSheet={vi.fn()}
libraryReset={1}
/>,
)
view.rerender(<ReferencesHost libraryReset={1} />)
expect(box).toHaveValue('')
await user.type(box, 'a')
view.rerender(
<ReferencesTab
readOnly={false}
navigate={vi.fn()}
reloadSheet={vi.fn()}
libraryReset={1}
/>,
)
view.rerender(<ReferencesHost libraryReset={1} />)
expect(box).toHaveValue('a')
})
})
@@ -311,182 +270,50 @@ describe('the reset signal', () => {
describe('adding a reference', () => {
const addButton = () =>
within(tabPanel()).getByRole('button', { name: 'Ajouter une référence' })
async function add(user: User) {
await user.click(addButton())
const byId = (id: string) => tabPanel().querySelector<HTMLElement>(id)!
await user.selectOptions(byId('#tab-reference-new-type'), 'book')
await user.type(byId('#tab-reference-new-citation'), 'Nouvelle source')
await user.click(
within(tabPanel()).getByRole('button', { name: 'Ajouter la référence' }),
)
}
it('resets the search, shows the new tile open and focuses its button', async () => {
const user = await mount()
await user.type(search(), 'Barone')
expect(cards()).toHaveLength(1)
await add(user)
expect(search()).toHaveValue('')
expect(cards()).toHaveLength(4)
expect(header('Nouvelle source')).toHaveAttribute('aria-expanded', 'true')
expect(document.activeElement).toBe(header('Nouvelle source'))
expect(within(tabPanel()).queryByLabelText('Citation')).toBeNull()
})
it('opens a draft tile with the citation focused, and a refused add keeps it', async () => {
it('opens the creation dialog with the type to choose, the search untouched', async () => {
const user = await mount()
await user.type(search(), 'Barone')
await user.click(addButton())
expect(search()).toHaveValue('')
expect(document.activeElement).toBe(
tabPanel().querySelector('#tab-reference-new-citation'),
)
await user.click(
within(tabPanel()).getByRole('button', { name: 'Ajouter la référence' }),
)
expect(within(tabPanel()).getByRole('alert')).toHaveTextContent(
'Choisissez un type',
)
const dialog = screen.getByRole('dialog', { name: 'Nouvelle référence' })
expect(within(dialog).getByLabelText('Type')).toHaveValue('')
expect(search()).toHaveValue('Barone')
expect(doc().references).toHaveLength(3)
})
})
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() {
it('creates the reference in one modification and leaves the list where it was', async () => {
const user = await mount()
await startEditAndType(user)
act(() => store.getState().undoStep())
await user.type(search(), 'Barone')
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' }),
const dialog = screen.getByRole('dialog', { name: 'Nouvelle référence' })
await user.selectOptions(within(dialog).getByLabelText('Type'), 'book')
await user.type(
within(dialog).getByLabelText('Citation'),
'Nouvelle source',
)
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()
await user.click(
within(dialog).getByRole('button', { name: 'Ajouter la référence' }),
)
await waitFor(() =>
expect(document.querySelector('dialog[open]')).toBeNull(),
)
expect(store.getState().undo.length).toBe(1)
expect(doc().references).toHaveLength(4)
expect(search()).toHaveValue('Barone')
expect(cards()).toHaveLength(1)
expect(document.activeElement).toBe(addButton())
})
})
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. */
/** Opens the tile and its « Modifier » dialog. */
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
await user.click(
within(tile(citation)).getByRole('button', { name: 'Modifier' }),
)
}
describe('the tile disclosure', () => {
@@ -593,198 +420,6 @@ describe('an open tile', () => {
})
})
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)
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/',
})
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('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)
@@ -805,9 +440,8 @@ describe('read-only tiles', () => {
})
describe('reopening the editor', () => {
it('starts with every tile closed and none in edit', async () => {
it('starts with every tile closed', async () => {
const user = await mount()
await startEdit(user, 'Barone')
await user.click(header('Denoix'))
cleanup()
await mount()
@@ -815,10 +449,5 @@ describe('reopening the editor', () => {
expanded: true,
})
expect(opened).toHaveLength(0)
expect(
within(tabPanel()).queryByLabelText('Citation', {
selector: '#tab-reference-0-ref-a-citation',
}),
).toBeNull()
})
})