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,10 +1,15 @@
/** R2.3: search, sort, count, no-result notice, A1, the add flow, the pin and the reset. */
/** R2.3: search, sort, count, no-result notice, A1, the pin (the dialog's tile) and the reset. */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, render, screen, within } from '@testing-library/react'
import {
act,
cleanup,
render,
screen,
waitFor,
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'
@@ -14,6 +19,7 @@ import {
resetStore,
store,
} from '../helpers/editorHarness'
import ReferencesHost from '../helpers/referencesHost'
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
vi.mock('@faunosteo/supabase', async (orig) => ({
@@ -87,12 +93,7 @@ function sortText(): string {
type User = ReturnType<typeof userEvent.setup>
const doc = () => JSON.parse(store.getState().documentText)
const lib = (readOnly = false, extra = {}) => (
<ReferencesTab
readOnly={readOnly}
navigate={vi.fn()}
reloadSheet={vi.fn()}
{...extra}
/>
<ReferencesHost readOnly={readOnly} {...extra} />
)
async function mount(readOnly = false) {
await openReady({ text: sortText() })
@@ -183,16 +184,6 @@ describe('the count line', () => {
})
})
describe('the count with a pinned tile', () => {
it('counts the matches only, the pinned tile stays shown', async () => {
const { user } = await mount()
await startEdit(user, 'r1')
await user.type(search(), 'alpha')
expect(order()).toEqual(['r1', 'r5'])
expect(screen.getByText('1 référence sur 5')).toBeInTheDocument()
})
})
describe('clearing the search', () => {
it('the button appears with a query, empties it and refocuses the field', async () => {
const { user } = await mount()
@@ -303,96 +294,72 @@ describe('the move buttons (A1)', () => {
})
})
/** Opens the tile of `id` and enters edit. */
/** Opens the tile of `id` and its « Modifier » dialog. */
async function startEdit(user: User, id: string) {
await user.click(header(id))
const item = header(id).closest('li')!
await user.click(within(item).getByRole('button', { name: 'Modifier' }))
return item
return screen.getByRole('dialog', { name: 'Modifier la référence' })
}
describe('the tile in edit is pinned (P23)', () => {
it('keeps its index when the sort changes', async () => {
/** Types a new citation in the open dialog and saves it. */
async function saveCitation(user: User, text: string) {
const dialog = screen.getByRole('dialog', { name: 'Modifier la référence' })
const citation = within(dialog).getByLabelText('Citation')
await user.clear(citation)
await user.type(citation, text)
await user.click(within(dialog).getByRole('button', { name: 'Enregistrer' }))
await waitFor(() => expect(document.querySelector('dialog[open]')).toBeNull())
}
const cancelDialog = async (user: User) => {
await user.click(screen.getByRole('button', { name: 'Annuler' }))
await waitFor(() => expect(document.querySelector('dialog[open]')).toBeNull())
}
describe('the tile the dialog edits is pinned (P23)', () => {
it('keeps its index when the sort changes after the dialog closes', async () => {
const { user } = await mount()
await user.selectOptions(sortBox(), 'citation')
await startEdit(user, 'r1')
await cancelDialog(user)
expect(order()).toEqual(['r5', 'r3', 'r4', 'r2', 'r1'])
await user.selectOptions(sortBox(), 'count')
expect(order()).toEqual(['r2', 'r5', 'r3', 'r4', 'r1'])
await user.selectOptions(sortBox(), 'sheet')
expect(order()).toEqual(['r1', 'r2', 'r3', 'r4', 'r5'])
expect(order()).toEqual(['r2', 'r1', 'r5', 'r3', 'r4'])
})
it('starts in sheet order and holds its place under a later sort', async () => {
it('stays shown under a search it stops matching, until the next search change (I1)', async () => {
const { user } = await mount()
await startEdit(user, 'r3')
await user.selectOptions(sortBox(), 'citation')
expect(order()).toEqual(['r5', 'r4', 'r3', 'r2', 'r1'])
})
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 startEdit(user, 'r5')
await saveCitation(user, 'Autre titre')
expect(order()).toEqual(['r5'])
await user.clear(search())
await user.type(search(), 'alpha')
expect(order()).toEqual(['r5'])
expect(order()).toEqual([])
})
it('keeps the tile shown and focused when its edit stops matching the search (I1)', async () => {
it('keeps the tile shown and the opener focused when its edit stops matching (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' }))
await startEdit(user, 'r1')
await saveCitation(user, 'Autre titre')
expect(order()).toEqual(['r1', 'r2'])
expect(document.activeElement).toBe(header('r1'))
expect(search()).toHaveValue('zèbre')
expect(document.activeElement).toBe(
within(header('r1').closest('li')!).getByRole('button', {
name: 'Modifier',
}),
)
})
it('keeps the tile in edit at its index across a sort change made while read-only (M3)', async () => {
it('keeps its index across a sort made in read-only after the dialog', async () => {
const { user, view } = await mount()
await user.selectOptions(sortBox(), 'citation')
await startEdit(user, 'r3')
await cancelDialog(user)
view.rerender(lib(true))
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('stays pinned in read-only: the read view of the tile, still shown after a search', async () => {
const { user, view } = await mount()
await startEdit(user, 'r1')
await user.selectOptions(sortBox(), 'citation')
view.rerender(lib(true))
expect(screen.queryByLabelText('Citation')).toBeNull()
expect(order()).toEqual(['r1', 'r5', 'r3', 'r4', 'r2'])
await user.type(search(), 'alpha')
expect(order()).toEqual(['r1', 'r5'])
view.rerender(lib(false))
expect(order()).toEqual(['r1', 'r5'])
expect(
within(header('r1').closest('li')!).getByLabelText('Citation'),
).toBeVisible()
})
it('a read-only toggle ends the edit state and collapses the tile', async () => {
const { user, view } = await mount()
await startEdit(user, 'r1')
view.rerender(lib(true))
expect(header('r1')).toHaveAttribute('aria-expanded', 'true')
await user.click(header('r1'))
expect(header('r1')).toHaveAttribute('aria-expanded', 'false')
view.rerender(lib(false))
expect(header('r1')).toHaveAttribute('aria-expanded', 'false')
expect(screen.queryByLabelText('Citation')).toBeNull()
})
})
@@ -402,194 +369,74 @@ describe('the tile outside the search (C3)', () => {
const lineOf = (id: string) =>
within(header(id).closest('li')!).queryByText(line)
async function editedOut(user: User) {
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')
return item
}
it('after « Terminer » shows the tile with its line, no notice, focus on its header', async () => {
it('after the save shows the tile with its line and no notice', async () => {
const { user } = await mount()
await user.clear(search())
await user.type(search(), 'alpha')
const item = await startEdit(user, 'r5')
await user.clear(within(item).getByLabelText('Citation'))
await user.type(within(item).getByLabelText('Citation'), 'Autre titre')
await user.click(within(item).getByRole('button', { name: 'Terminer' }))
await startEdit(user, 'r5')
await saveCitation(user, 'Autre titre')
expect(order()).toEqual(['r5'])
expect(lineOf('r5')).toBeVisible()
expect(notice()).toBeNull()
expect(screen.getByText('0 référence sur 5')).toBeInTheDocument()
expect(document.activeElement).toBe(header('r5'))
expect(header('r5').getAttribute('aria-describedby')).toBe(lineOf('r5')!.id)
expect(header('r5')).toHaveAccessibleDescription(line)
expect(
screen.getByRole('button', { name: 'Effacer la recherche' }),
).toBeVisible()
})
it('the next search change drops the tile and shows the notice', async () => {
const { user } = await mount()
await user.type(search(), 'alpha')
const item = await startEdit(user, 'r5')
await user.clear(within(item).getByLabelText('Citation'))
await user.type(within(item).getByLabelText('Citation'), 'Autre titre')
await user.click(within(item).getByRole('button', { name: 'Terminer' }))
await startEdit(user, 'r5')
await saveCitation(user, 'Autre titre')
await user.type(search(), 'x')
expect(order()).toEqual([])
expect(notice()).not.toBeNull()
})
it('a tile in edit that does not match carries the line', async () => {
const { user } = await mount()
const item = await editedOut(user)
expect(lineOf('r1')).toBeNull()
await user.type(search(), 'x')
expect(within(item).getByLabelText('Citation')).toBeVisible()
expect(lineOf('r1')).toBeVisible()
})
it('a matching pinned tile has no line', async () => {
const { user } = await mount()
await user.type(search(), 'zebre')
await startEdit(user, 'r1')
await user.type(search(), 'alpha')
expect(lineOf('r1')).toBeVisible()
expect(lineOf('r5')).toBeNull()
await user.clear(search())
await cancelDialog(user)
expect(lineOf('r1')).toBeNull()
})
it('a read-only flip keeps the tile and its line', async () => {
const { user, view } = await mount()
await startEdit(user, 'r1')
await user.type(search(), 'alpha')
await startEdit(user, 'r5')
await saveCitation(user, 'Autre titre')
view.rerender(lib(true))
expect(order()).toEqual(['r1', 'r5'])
expect(lineOf('r1')).toBeVisible()
expect(order()).toEqual(['r5'])
expect(lineOf('r5')).toBeVisible()
expect(notice()).toBeNull()
})
it('an open draft tile does not hide the notice for a search matching nothing', async () => {
const { user } = await mount()
await user.click(
screen.getByRole('button', { name: 'Ajouter une référence' }),
)
await user.type(search(), 'radiographie')
expect(order()).toEqual([])
expect(notice()).not.toBeNull()
})
})
describe('adding (P24)', () => {
const add = () =>
screen.getByRole('button', { name: 'Ajouter une référence' })
const draftCitation = () =>
document.querySelector<HTMLElement>('#tab-reference-new-citation')
it('resets search and sort, appends a draft tile in edit, citation focused', async () => {
describe('after a dialog closes, the view is unchanged (R2F-5)', () => {
it('keeps scroll position inputs: search, sort, open tiles', async () => {
const { user } = await mount()
await user.selectOptions(sortBox(), 'count')
await user.click(header('r2'))
await user.selectOptions(sortBox(), 'citation')
await user.type(search(), 'e')
await user.click(add())
expect(search()).toHaveValue('')
expect(sortBox()).toHaveValue('sheet')
expect(order()).toHaveLength(5)
expect(document.activeElement).toBe(draftCitation())
expect(screen.getByLabelText('Type')).toHaveValue('')
expect(
within(screen.getByLabelText('Type')).getByText('Choisir…'),
).toBeInTheDocument()
expect(
screen.getByText('Type et citation obligatoires.'),
).toBeInTheDocument()
expect(snapshot().history).toBe(0)
})
it('creates the reference in one modification; the tile stays open, out of edit, focused', async () => {
const { user } = await mount()
await user.click(add())
await user.selectOptions(screen.getByLabelText('Type'), 'book')
await user.type(draftCitation()!, 'Nouvelle source')
await user.click(
screen.getByRole('button', { name: 'Ajouter la référence' }),
)
expect(store.getState().undo.length).toBe(1)
expect(doc().references).toHaveLength(6)
const id = doc().references[5].id
expect(order()).toHaveLength(6)
expect(header(id)).toHaveAttribute('aria-expanded', 'true')
expect(document.activeElement).toBe(header(id))
expect(draftCitation()).toBeNull()
expect(
within(header(id).closest('li')!).getByRole('button', {
await startEdit(user, 'r3')
await cancelDialog(user)
expect(search()).toHaveValue('e')
expect(sortBox()).toHaveValue('citation')
expect(header('r2')).toHaveAttribute('aria-expanded', 'true')
expect(header('r3')).toHaveAttribute('aria-expanded', 'true')
expect(document.activeElement).toBe(
within(header('r3').closest('li')!).getByRole('button', {
name: 'Modifier',
}),
).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())
await user.click(search())
await user.click(add())
expect(document.activeElement).toBe(draftCitation())
expect(
document.querySelectorAll('#tab-reference-new-citation'),
).toHaveLength(1)
})
it('leaves the tile in edit first: a refused field cancels the add (P22)', async () => {
const { user } = await mount()
const item = await startEdit(user, 'r1')
const url = within(item).getByLabelText('Adresse web (URL)')
await user.type(url, 'exemple.org')
await user.click(add())
expect(draftCitation()).toBeNull()
expect(document.activeElement).toBe(url)
await user.clear(url)
await user.click(add())
expect(draftCitation()).not.toBeNull()
expect(within(item).queryByLabelText('Citation')).toBeNull()
})
it('keeps the draft hidden in read-only and resumes it when editable again', async () => {
const { user, view } = await mount()
await user.click(add())
await user.type(draftCitation()!, 'En cours')
view.rerender(lib(true))
expect(draftCitation()).not.toBeVisible()
view.rerender(lib(false))
expect(draftCitation()).toBeVisible()
expect(draftCitation()).toHaveValue('En cours')
})
})
describe('the reset path (« Tout afficher »)', () => {
it('empties the search and keeps the sort, keeping open tiles and the tile in edit (R2F-4)', async () => {
it('empties the search and keeps the sort, keeping open tiles (R2F-4)', async () => {
await openReady({ text: sortText() })
const user = userEvent.setup()
const view = render(lib(false, { libraryReset: 0 }))
await user.click(header('r2'))
const item = await startEdit(user, 'r1')
await user.selectOptions(sortBox(), 'citation')
await user.type(search(), 'e')
view.rerender(lib(false, { libraryReset: 1 }))
@@ -597,10 +444,6 @@ describe('the reset path (« Tout afficher »)', () => {
expect(sortBox()).toHaveValue('citation')
expect(order()).toHaveLength(5)
expect(header('r2')).toHaveAttribute('aria-expanded', 'true')
expect(within(item).getByLabelText('Citation')).toBeInTheDocument()
// The tile in edit stays pinned through the reset: a new search does not hide it.
await user.type(search(), 'alpha')
expect(order()).toEqual(['r1', 'r5'])
})
})
@@ -638,9 +481,9 @@ describe('« Aller à » a reference hidden by the search (P3, P8)', () => {
})
describe('state of a reference that left the document (P23-prime)', () => {
it('is dropped: undoing the removal brings the tile back closed and out of edit', async () => {
it('is dropped: undoing the removal brings the tile back closed', async () => {
const { user } = await mount()
await startEdit(user, 'r4')
await user.click(header('r4'))
act(() => {
store
.getState()
@@ -652,58 +495,5 @@ describe('state of a reference that left the document (P23-prime)', () => {
})
expect(order()).toContain('r4')
expect(header('r4')).toHaveAttribute('aria-expanded', 'false')
expect(
within(header('r4').closest('li')!).queryByLabelText('Citation'),
).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()
})
})