Non-modal panel in a relative container after the tab row (scrim over the tab panels, which are inert while it is open). Chip "Citées par" opened from a finding's Citations block, "Tout afficher (N)", accent- and case-insensitive search on citation, DOI and URL, "citée n fois" per card (citationCounts, memoised TS read; the core stays the authority for refused deletions). Up/down only without filter or search. "Aller à" a reference clears a hiding search then focuses the card. Rewritten assertions (R-24): editor-shell "the references panel keeps its form text and no id repeats" closes the panel before reaching the inert tab panels. Added: Chromium clickability at 1 024 and 1 280 px (top and scrolled), editor-structures count and search, unit editor-references-panel, editor-citations, navigation clear_cited_by.
285 lines
9.2 KiB
TypeScript
285 lines
9.2 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',
|
|
)
|
|
})
|
|
})
|