- A field blurred by a 1 024 px crossing or a read-only switch hands its focus to the shown tab (or Plus), never to body (focusRequest, useViewport). - TextField no longer commits a blurred value twice when it unmounts before the render that brings the new value: one undo step, one count. - Annuler and Retablir share the field left by Tab, so Shift+Tab to Annuler and Enter still skips the undo of a refused field. - Adding a reference clears the finding filter and the search. Unit tests: no-loss (focus on tab and on Plus, one undo entry), history buttons (field kept across the pair), references panel (add clears, refused add keeps).
319 lines
11 KiB
TypeScript
319 lines
11 KiB
TypeScript
/** U1 Lot D: the references side panel, filter, search, counts, non-modal layout. */
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
act,
|
|
cleanup,
|
|
fireEvent,
|
|
render,
|
|
screen,
|
|
within,
|
|
} from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import ReferencesPanel from '@/components/editor/references/ReferencesPanel'
|
|
import { targetOfKey } from '@/editor/navigation'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { edit, insert } from '../helpers/editorFixtures'
|
|
import {
|
|
baseText,
|
|
commitMove,
|
|
openReady,
|
|
resetStore,
|
|
serverHas,
|
|
store,
|
|
} from '../helpers/editorHarness'
|
|
import { mountDraft, setWidth } from '../helpers/editorRoute'
|
|
|
|
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
|
|
vi.mock('@faunosteo/supabase', async (orig) => ({
|
|
...(await orig<typeof import('@faunosteo/supabase')>()),
|
|
getSheetVersion: vi.fn(),
|
|
downloadSheetDrawing: vi.fn(),
|
|
saveSheetDraft: vi.fn(),
|
|
getOfficialSheetAdmin: vi.fn(),
|
|
submitSheetReview: vi.fn(),
|
|
}))
|
|
|
|
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
|
|
afterEach(() => {
|
|
cleanup()
|
|
resetStore()
|
|
setWidth(1024)
|
|
})
|
|
|
|
const name = (n: string) => ({ fr: n, en: n })
|
|
const ref = (id: string, citation: string, url = '', doi = '') =>
|
|
({
|
|
kind: 'reference',
|
|
value: {
|
|
id,
|
|
type: 'book',
|
|
citation,
|
|
url: url || null,
|
|
doi: doi || null,
|
|
},
|
|
}) as const
|
|
|
|
function sheetText(): string {
|
|
let text = baseText()
|
|
text = insert(text, ref('ref-a', 'Barone, Ostéologie'))
|
|
text = insert(text, ref('ref-b', 'Denoix', 'https://exemple.org/Épaule'))
|
|
text = insert(text, ref('ref-c', 'Code rural', '', '10.1000/ABC'))
|
|
text = insert(text, {
|
|
kind: 'zone',
|
|
value: { id: 'zone-a', name: name('Zone A'), categories: [] },
|
|
})
|
|
return edit(text, {
|
|
op: 'insert',
|
|
parent: { kind: 'zone', id: 'zone-a' },
|
|
element: {
|
|
kind: 'finding_category',
|
|
value: {
|
|
id: 'fcat-a',
|
|
kind: 'dysfunction',
|
|
name: name('Cat A'),
|
|
description: {},
|
|
reference_ids: ['ref-a', 'ref-b'],
|
|
review_status: 'to_confirm',
|
|
children: [],
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
const ready = () =>
|
|
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
|
|
const header = () => screen.getByRole('button', { name: 'Références' })
|
|
const panel = () => screen.getByTestId('editor-references-panel')
|
|
const cards = () => within(panel()).getAllByRole('listitem')
|
|
const search = () =>
|
|
within(panel()).getByRole('searchbox', { name: 'Rechercher une référence' })
|
|
|
|
async function mount(width = 1280) {
|
|
serverHas(sheetText(), 3)
|
|
const user = userEvent.setup()
|
|
mountDraft({ width })
|
|
await ready()
|
|
return user
|
|
}
|
|
|
|
async function openFromFinding(user: ReturnType<typeof userEvent.setup>) {
|
|
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
|
await user.click(screen.getByRole('treeitem', { name: /Cat A/ }))
|
|
const tabPanel = screen.getByRole('tabpanel', { name: 'Constats' })
|
|
await user.click(
|
|
within(tabPanel).getByRole('button', { name: '2 références' }),
|
|
)
|
|
}
|
|
|
|
describe('references panel opening', () => {
|
|
it('the header button opens it on its heading', async () => {
|
|
const user = await mount()
|
|
await user.click(header())
|
|
expect(document.activeElement).toBe(
|
|
within(panel()).getByRole('heading', { name: 'Références' }),
|
|
)
|
|
expect(cards()).toHaveLength(3)
|
|
})
|
|
|
|
it('a finding that cites references opens the panel filtered to it', async () => {
|
|
const user = await mount()
|
|
await openFromFinding(user)
|
|
const chip = within(panel()).getByTestId('references-filter-chip')
|
|
expect(chip).toHaveTextContent('Citées par « Cat A » (2)')
|
|
expect(cards()).toHaveLength(2)
|
|
await user.click(
|
|
within(panel()).getByRole('button', { name: 'Tout afficher (3)' }),
|
|
)
|
|
expect(within(panel()).queryByTestId('references-filter-chip')).toBeNull()
|
|
expect(cards()).toHaveLength(3)
|
|
expect(document.activeElement).toBe(search())
|
|
})
|
|
|
|
it('each card says how often it is cited', async () => {
|
|
const user = await mount()
|
|
await user.click(header())
|
|
const texts = cards().map((card) => card.textContent)
|
|
expect(texts[0]).toContain('citée 1 fois')
|
|
expect(texts[2]).toContain('citée 0 fois')
|
|
})
|
|
|
|
it('undo removing the cited finding drops the filter', async () => {
|
|
const user = await mount()
|
|
await openFromFinding(user)
|
|
act(() => {
|
|
store.getState().commit({
|
|
op: 'remove',
|
|
key: { kind: 'finding', zone_id: 'zone-a', path: ['fcat-a'] },
|
|
})
|
|
})
|
|
expect(within(panel()).queryByTestId('references-filter-chip')).toBeNull()
|
|
expect(cards()).toHaveLength(3)
|
|
})
|
|
})
|
|
|
|
describe('search and order buttons', () => {
|
|
it('matches the citation, the DOI and the URL, ignoring case and accents', async () => {
|
|
const user = await mount()
|
|
await user.click(header())
|
|
await user.type(search(), 'OSTEOLOGIE')
|
|
expect(cards()).toHaveLength(1)
|
|
await user.clear(search())
|
|
await user.type(search(), '10.1000/abc')
|
|
expect(cards()).toHaveLength(1)
|
|
await user.clear(search())
|
|
await user.type(search(), 'epaule')
|
|
expect(cards()).toHaveLength(1)
|
|
await user.clear(search())
|
|
await user.type(search(), 'zzz')
|
|
expect(
|
|
within(panel()).getByText('Aucune référence ne correspond.'),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows ↑ / ↓ only with neither filter nor search', async () => {
|
|
const user = await mount()
|
|
await user.click(header())
|
|
expect(
|
|
within(panel()).getAllByRole('button', { name: 'Descendre' }).length,
|
|
).toBe(3)
|
|
await user.type(search(), 'a')
|
|
expect(
|
|
within(panel()).queryByRole('button', { name: 'Descendre' }),
|
|
).toBeNull()
|
|
})
|
|
|
|
it('hides ↑ / ↓ while filtered to a finding', async () => {
|
|
const user = await mount()
|
|
await openFromFinding(user)
|
|
expect(
|
|
within(panel()).queryByRole('button', { name: 'Descendre' }),
|
|
).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('closing and layout', () => {
|
|
it('Escape, ✕ and the scrim close it, the focus goes back to the opener', async () => {
|
|
const user = await mount()
|
|
await user.click(header())
|
|
await user.keyboard('{Escape}')
|
|
expect(panel()).not.toBeVisible()
|
|
expect(document.activeElement).toBe(header())
|
|
await user.click(header())
|
|
await user.click(
|
|
within(panel()).getByRole('button', { name: 'Fermer les références' }),
|
|
)
|
|
expect(panel()).not.toBeVisible()
|
|
await user.click(header())
|
|
fireEvent.click(screen.getByTestId('references-scrim'))
|
|
expect(panel()).not.toBeVisible()
|
|
expect(document.activeElement).toBe(header())
|
|
})
|
|
|
|
it('the finding button gets the focus back when it is still shown', async () => {
|
|
const user = await mount()
|
|
await openFromFinding(user)
|
|
await user.keyboard('{Escape}')
|
|
expect(document.activeElement).toHaveTextContent('2 références')
|
|
})
|
|
|
|
it('makes the tab panels inert while it is open, the header stays usable', async () => {
|
|
const user = await mount()
|
|
const tabPanel = () => screen.getAllByRole('tabpanel', { hidden: true })[0]
|
|
act(() => void commitMove(40))
|
|
await user.click(header())
|
|
expect(tabPanel().parentElement).toHaveAttribute('inert')
|
|
expect(screen.getByRole('button', { name: /Enregistrer/ })).toBeEnabled()
|
|
expect(panel().closest('header')).toBeNull()
|
|
await user.click(header())
|
|
expect(tabPanel().parentElement).not.toHaveAttribute('inert')
|
|
})
|
|
|
|
it('Ctrl+Z reverts a reference edit while open', async () => {
|
|
const user = await mount()
|
|
await user.click(header())
|
|
const field = within(panel()).getByLabelText('Citation', {
|
|
selector: '#reference-ref-a-citation',
|
|
})
|
|
await user.clear(field)
|
|
await user.type(field, 'Neuf')
|
|
fireEvent.blur(field)
|
|
const doc = () => JSON.parse(store.getState().documentText)
|
|
expect(doc().references[0].citation).toBe('Neuf')
|
|
fireEvent.keyDown(document.body, { key: 'z', ctrlKey: true })
|
|
expect(doc().references[0].citation).toBe('Barone, Ostéologie')
|
|
})
|
|
})
|
|
|
|
describe('read-only modes', () => {
|
|
it('show no edit control, only the search', async () => {
|
|
const user = await mount(800)
|
|
await user.click(header())
|
|
expect(
|
|
within(panel()).queryByRole('button', { name: 'Supprimer' }),
|
|
).toBeNull()
|
|
expect(
|
|
within(panel()).queryByRole('button', { name: 'Ajouter la référence' }),
|
|
).toBeNull()
|
|
expect(search()).toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('« Aller à » a reference', () => {
|
|
it('clears the search that hides it and focuses its card', async () => {
|
|
serverHas(sheetText(), 3)
|
|
await openReady({ text: sheetText() })
|
|
const user = userEvent.setup()
|
|
const props = { readOnly: false, onClose: vi.fn() }
|
|
const view = render(<ReferencesPanel {...props} />)
|
|
const box = screen.getByRole('searchbox', {
|
|
name: 'Rechercher une référence',
|
|
})
|
|
await user.type(box, 'Code')
|
|
expect(screen.getAllByRole('listitem')).toHaveLength(1)
|
|
const focus = {
|
|
target: targetOfKey({ kind: 'reference', id: 'ref-a' }),
|
|
serial: 1,
|
|
}
|
|
view.rerender(<ReferencesPanel {...props} focus={focus} />)
|
|
expect(box).toHaveValue('')
|
|
expect(screen.getAllByRole('listitem')).toHaveLength(3)
|
|
expect(document.activeElement?.getAttribute('data-focus-key')).toContain(
|
|
'ref-a',
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('adding a reference', () => {
|
|
async function add(user: ReturnType<typeof userEvent.setup>) {
|
|
const byId = (id: string) => panel().querySelector<HTMLElement>(id)!
|
|
await user.selectOptions(byId('#reference-new-type'), 'book')
|
|
await user.type(byId('#reference-new-citation'), 'Nouvelle source')
|
|
await user.click(
|
|
within(panel()).getByRole('button', { name: 'Ajouter la référence' }),
|
|
)
|
|
}
|
|
|
|
it('clears the finding filter and the search so the new reference shows', async () => {
|
|
const user = await mount()
|
|
await openFromFinding(user)
|
|
await user.type(search(), 'Barone')
|
|
expect(cards()).toHaveLength(1)
|
|
await add(user)
|
|
expect(search()).toHaveValue('')
|
|
expect(within(panel()).queryByTestId('references-filter-chip')).toBeNull()
|
|
expect(cards()).toHaveLength(4)
|
|
expect(within(panel()).getByText('Nouvelle source')).toBeInTheDocument()
|
|
})
|
|
|
|
it('a refused add keeps the filter and the search', async () => {
|
|
const user = await mount()
|
|
await openFromFinding(user)
|
|
await user.type(search(), 'Barone')
|
|
await user.click(
|
|
within(panel()).getByRole('button', { name: 'Ajouter la référence' }),
|
|
)
|
|
expect(search()).toHaveValue('Barone')
|
|
expect(within(panel()).getByTestId('references-filter-chip')).toBeInTheDocument()
|
|
})
|
|
})
|