feat(editor): references side panel with filter, search and counts
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.
This commit is contained in:
1 parent
cf9c300c80
commit
ab51ea764d
19 files changed
+891
-85
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
|
||||
import { citationCounts } from '@/editor/references/citations'
|
||||
import { edit, insert } from '../helpers/editorFixtures'
|
||||
import { baseText } from '../helpers/editorHarness'
|
||||
|
||||
const name = (n: string) => ({ fr: n, en: n })
|
||||
const reference = (id: string) =>
|
||||
({
|
||||
kind: 'reference',
|
||||
value: { id, type: 'book', citation: id, url: null, doi: null },
|
||||
}) as const
|
||||
|
||||
function citing(): AnatomicalSheetDocument {
|
||||
let text = insert(baseText(), reference('ref-a'))
|
||||
text = insert(text, reference('ref-b'))
|
||||
text = insert(text, reference('ref-none'))
|
||||
text = insert(text, {
|
||||
kind: 'zone',
|
||||
value: { id: 'zone-a', name: name('Zone'), categories: [] },
|
||||
})
|
||||
text = edit(text, {
|
||||
op: 'insert',
|
||||
parent: { kind: 'zone', id: 'zone-a' },
|
||||
element: {
|
||||
kind: 'finding_category',
|
||||
value: {
|
||||
id: 'fcat-a',
|
||||
kind: 'dysfunction',
|
||||
name: name('Cat'),
|
||||
description: {},
|
||||
reference_ids: ['ref-a', 'ref-b'],
|
||||
review_status: 'to_confirm',
|
||||
children: [],
|
||||
},
|
||||
},
|
||||
})
|
||||
text = edit(text, {
|
||||
op: 'insert',
|
||||
parent: { kind: 'finding', zone_id: 'zone-a', path: ['fcat-a'] },
|
||||
element: {
|
||||
kind: 'finding_node',
|
||||
value: {
|
||||
id: 'fnode-a',
|
||||
name: name('Node'),
|
||||
description: {},
|
||||
requires_vet_referral: false,
|
||||
reference_ids: ['ref-a'],
|
||||
review_status: 'to_confirm',
|
||||
children: [],
|
||||
},
|
||||
},
|
||||
})
|
||||
return JSON.parse(text)
|
||||
}
|
||||
|
||||
describe('citationCounts', () => {
|
||||
it('counts the citers of a category and of nested nodes, zero when uncited', () => {
|
||||
const counts = citationCounts(citing())
|
||||
expect(counts.get('ref-a')).toBe(2)
|
||||
expect(counts.get('ref-b')).toBe(1)
|
||||
expect(counts.get('ref-none') ?? 0).toBe(0)
|
||||
})
|
||||
|
||||
it('is memoised on the parsed document', () => {
|
||||
const doc = citing()
|
||||
expect(citationCounts(doc)).toBe(citationCounts(doc))
|
||||
})
|
||||
|
||||
it('is empty without findings', () => {
|
||||
expect(citationCounts(JSON.parse(baseText())).size).toBe(0)
|
||||
})
|
||||
})
|
||||
@@ -135,6 +135,16 @@ describe('navigationReducer', () => {
|
||||
expect(state.references).toEqual({ open: true, citedBy: view })
|
||||
})
|
||||
|
||||
it('clear_cited_by drops the filter and keeps the panel open', () => {
|
||||
const state = run(
|
||||
initialNavigation('annotations'),
|
||||
{ type: 'open_references', citedBy: view },
|
||||
{ type: 'clear_cited_by' },
|
||||
)
|
||||
expect(state.references).toEqual({ open: true, citedBy: null })
|
||||
expect(state.focus).toBeUndefined()
|
||||
})
|
||||
|
||||
it('opens a section and goes back to the drawing', () => {
|
||||
const state = run(initialNavigation('annotations'), {
|
||||
type: 'open_section',
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
/** 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',
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user