Search (citation, DOI, URL) with « Effacer la recherche » and Escape;
« Trier par » sheet order, citation (locale collator), citation count,
type (business order); every sort stable on stored order, display only:
document, stored order, ids and history unchanged (unit + e2e). Count
« {n} références » / « {n} sur {total} » counts matching references
only. « Monter » / « Descendre » only in sheet order without search
(A1). « Ajouter une référence » resets search and sort and appends a
draft tile in edit; a tile in edit stays pinned while sorted or
searched; tile state pruned of absent ids. Reset path ready for « Tout
afficher » (R2.4).
Gates: typecheck, lint, test:run (app 1452), build, e2e:ui 251;
e2e:shell 16 specs on db:sandbox (editor-equine never listed): 115
passed / 102 skipped / 2 failed: editor-structures (test-side, fixed,
rerun with editor-references 12 passed / 11 skipped) and admin.spec A0
« Target crashed » during axe on the rights screen (not R2: that
screen renders every grant, 1 282 accumulated e2e fixtures; reported as
an open risk); full-row guard: no pre-existing row changed in any table.
## R-24 (removed or rewritten assertions)
| file | old | new |
|---|---|---|
| editor-references-tab.test.tsx | `#tab-reference-new-citation` present on load | null until « Ajouter une référence » |
| same | no match text « Aucune référence ne correspond. » | notice « Aucune référence ne contient « zzz ». » |
| same | add: form always visible, add clears the search, `getByText('Nouvelle source')` | click « Ajouter une référence » first; new tile header open, focused, search empty, 4 tiles |
| same | « a refused add keeps the search » | REMOVED (P24: add resets the search at once); replaced by « draft opens with citation focused, refused add keeps the draft, no write » |
| editor-tabs-lists.test.tsx | add form directly | click « Ajouter une référence » before the form assertions (same assertions after) |
| editor-keep-alive.test.tsx | form survives tab round trip | same, after opening the draft |
| editor-no-loss.test.tsx | reference form keeps typed text | same, after opening the draft |
| e2e editor-shell.spec (2), editor-shell-crossing.spec, editor-structures.spec | fill `#tab-reference-new-*` directly; `getByText(citation)` visible | open the draft first; structures asserts the new header is focused |
| e2e editor-references.spec keyboard walk | focus search, Tab reaches header 0 | focus « Ajouter une référence » (search, sort, add precede the headers) |
Other readers of `editor.reference.add`/new ids: grep of src, tests, e2e incl. editor-equine found none left unlisted. editor-equine is not affected ("updated, not run": nothing to update).
## i18n (fr / en)
Added: `editor.references.add` Ajouter une référence / Add a reference; `add_help` Type et citation obligatoires. / Type and citation required.; `clear_search` Effacer la recherche / Clear the search; `sort_by` Trier par / Sort by; `sort.sheet|citation|count|type` Ordre de la fiche, Citation (A → Z), Nombre de citations, Type / Sheet order, Citation (A → Z), Number of citations, Type; `count_one|other` {count} référence(s) / reference(s); `count_of_one|other` {count} référence(s) sur {total} / {count} reference(s) of {total}; `reorder_hint` Pour réorganiser, affichez l'ordre de la fiche sans recherche. / To reorder, show the sheet order without a search.; `no_result` Aucune référence ne contient « {q} ». / No reference contains “{q}”.; `no_result_scope` La recherche porte sur la citation, le DOI et l'URL. / The search covers the citation, the DOI and the URL. (REVUE sentence split in two keys so the first half is bold).
Changed: none. Removed: none (`editor.references.no_match` kept, the panel's EmptyLine still uses it).
597 lines
21 KiB
TypeScript
597 lines
21 KiB
TypeScript
/** 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 {
|
||
cleanup,
|
||
fireEvent,
|
||
render,
|
||
screen,
|
||
within,
|
||
} from '@testing-library/react'
|
||
import userEvent from '@testing-library/user-event'
|
||
import ReferenceFacts from '@/components/editor/references/ReferenceFacts'
|
||
import ReferenceLibrary from '@/components/editor/references/ReferenceLibrary'
|
||
import { useLocaleStore } from '@/state/localeStore'
|
||
import { targetOfKey } from '@/editor/navigation'
|
||
import { edit, insert } from '../helpers/editorFixtures'
|
||
import {
|
||
baseText,
|
||
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: [],
|
||
},
|
||
},
|
||
})
|
||
}
|
||
|
||
/** The sheet with a finding under « Cat A » that cites ref-a too (ref-a: two citers). */
|
||
function citedTwiceText(): string {
|
||
return edit(sheetText(), {
|
||
op: 'insert',
|
||
parent: { kind: 'finding', zone_id: 'zone-a', path: ['fcat-a'] },
|
||
element: {
|
||
kind: 'finding_node',
|
||
value: {
|
||
id: 'fnode-a',
|
||
name: name('Node A'),
|
||
description: {},
|
||
requires_vet_referral: false,
|
||
reference_ids: ['ref-a'],
|
||
review_status: 'to_confirm',
|
||
children: [],
|
||
},
|
||
},
|
||
})
|
||
}
|
||
|
||
type User = ReturnType<typeof userEvent.setup>
|
||
|
||
const ready = () =>
|
||
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
|
||
const tabPanel = () => screen.getByRole('tabpanel', { name: 'Références' })
|
||
const cards = () => within(tabPanel()).getAllByRole('listitem')
|
||
const header = (citation: string) =>
|
||
within(tabPanel()).getByRole('button', { name: new RegExp(`^${citation}`) })
|
||
const openAll = async (user: User) => {
|
||
for (const button of within(tabPanel()).getAllByRole('button', {
|
||
expanded: false,
|
||
})) {
|
||
if (button.hasAttribute('aria-controls')) await user.click(button)
|
||
}
|
||
}
|
||
const search = () =>
|
||
within(tabPanel()).getByRole('searchbox', {
|
||
name: 'Rechercher une référence',
|
||
})
|
||
|
||
async function mount(width = 1280, text = citedTwiceText()) {
|
||
serverHas(text, 3)
|
||
const user = userEvent.setup()
|
||
mountDraft({ width })
|
||
await ready()
|
||
await user.click(screen.getByRole('tab', { name: 'Références' }))
|
||
return user
|
||
}
|
||
|
||
describe('the Références tab', () => {
|
||
it('is the fifth tab, there is no header button, and it lists every card', async () => {
|
||
await mount()
|
||
const tabs = screen.getAllByRole('tab').map((tab) => tab.textContent)
|
||
expect(tabs).toEqual([
|
||
'Général',
|
||
'Annotations',
|
||
'Constats',
|
||
'Références',
|
||
'Vérification',
|
||
])
|
||
expect(screen.queryByRole('button', { name: 'Références' })).toBeNull()
|
||
expect(cards()).toHaveLength(3)
|
||
expect(
|
||
within(tabPanel()).getByRole('heading', { name: 'Références' }),
|
||
).toBeInTheDocument()
|
||
})
|
||
|
||
it('each card says how often it is cited, the ids carry the tab prefix', async () => {
|
||
await mount()
|
||
const texts = cards().map((card) => card.textContent)
|
||
expect(texts[0]).toContain('Citée 2 fois')
|
||
expect(texts[1]).toContain('Citée 1 fois')
|
||
expect(texts[2]).toContain('Non citée')
|
||
expect(
|
||
tabPanel().querySelector('#tab-reference-0-ref-a-header'),
|
||
).not.toBeNull()
|
||
expect(tabPanel().querySelector('#tab-reference-new-citation')).toBeNull()
|
||
})
|
||
|
||
it('matches the citation, the DOI and the URL, ignoring case and accents', async () => {
|
||
const user = await mount()
|
||
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(tabPanel()).getByText('Aucune référence ne contient « zzz ».'),
|
||
).toBeInTheDocument()
|
||
})
|
||
|
||
it('shows ↑ / ↓ in open tiles, and only without a search', async () => {
|
||
const user = await mount()
|
||
await openAll(user)
|
||
expect(
|
||
within(tabPanel()).getAllByRole('button', { name: 'Descendre' }).length,
|
||
).toBe(3)
|
||
await user.type(search(), 'a')
|
||
expect(
|
||
within(tabPanel()).queryByRole('button', { name: 'Descendre' }),
|
||
).toBeNull()
|
||
})
|
||
|
||
it('Ctrl+Z reverts a reference edit', 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 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 openAll(user)
|
||
expect(
|
||
within(tabPanel()).queryByRole('button', { name: 'Supprimer' }),
|
||
).toBeNull()
|
||
expect(
|
||
within(tabPanel()).queryByRole('button', {
|
||
name: 'Ajouter la référence',
|
||
}),
|
||
).toBeNull()
|
||
expect(search()).toBeInTheDocument()
|
||
})
|
||
})
|
||
|
||
describe('« Aller à » a reference', () => {
|
||
it('clears the search that hides it, opens its tile and focuses its button', async () => {
|
||
serverHas(sheetText(), 3)
|
||
await openReady({ text: sheetText() })
|
||
const user = userEvent.setup()
|
||
const view = render(
|
||
<ReferenceLibrary readOnly={false} navigate={vi.fn()} />,
|
||
)
|
||
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(
|
||
<ReferenceLibrary readOnly={false} navigate={vi.fn()} focus={focus} />,
|
||
)
|
||
expect(box).toHaveValue('')
|
||
expect(document.querySelectorAll('button[aria-controls]')).toHaveLength(3)
|
||
expect(document.activeElement?.getAttribute('data-focus-key')).toContain(
|
||
'ref-a',
|
||
)
|
||
expect(document.activeElement).toHaveAttribute('aria-expanded', 'true')
|
||
})
|
||
})
|
||
|
||
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(
|
||
<ReferenceLibrary readOnly={false} navigate={vi.fn()} resetSignal={0} />,
|
||
)
|
||
const box = screen.getByRole('searchbox', {
|
||
name: 'Rechercher une référence',
|
||
})
|
||
await user.type(box, 'Code')
|
||
view.rerender(
|
||
<ReferenceLibrary readOnly={false} navigate={vi.fn()} resetSignal={0} />,
|
||
)
|
||
expect(box).toHaveValue('Code')
|
||
view.rerender(
|
||
<ReferenceLibrary readOnly={false} navigate={vi.fn()} resetSignal={1} />,
|
||
)
|
||
expect(box).toHaveValue('')
|
||
await user.type(box, 'a')
|
||
view.rerender(
|
||
<ReferenceLibrary readOnly={false} navigate={vi.fn()} resetSignal={1} />,
|
||
)
|
||
expect(box).toHaveValue('a')
|
||
})
|
||
})
|
||
|
||
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 () => {
|
||
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',
|
||
)
|
||
expect(doc().references).toHaveLength(3)
|
||
})
|
||
})
|
||
|
||
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. */
|
||
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
|
||
}
|
||
|
||
describe('the tile disclosure', () => {
|
||
it('opens and closes from the keyboard, and several stay open', async () => {
|
||
const user = await mount()
|
||
const first = header('Barone')
|
||
expect(first).toHaveAttribute('aria-expanded', 'false')
|
||
expect(first).toHaveAttribute('aria-controls', 'tab-reference-0-ref-a-body')
|
||
first.focus()
|
||
await user.keyboard('{Enter}')
|
||
expect(first).toHaveAttribute('aria-expanded', 'true')
|
||
header('Denoix').focus()
|
||
await user.keyboard(' ')
|
||
expect(first).toHaveAttribute('aria-expanded', 'true')
|
||
expect(header('Denoix')).toHaveAttribute('aria-expanded', 'true')
|
||
first.focus()
|
||
await user.keyboard('{Enter}')
|
||
expect(first).toHaveAttribute('aria-expanded', 'false')
|
||
expect(tabPanel().querySelector('#tab-reference-0-ref-a-body')).toBeNull()
|
||
})
|
||
|
||
it('is a natural tab stop that carries only the focus key', async () => {
|
||
await mount()
|
||
const button = header('Barone')
|
||
expect(button).not.toHaveAttribute('tabindex')
|
||
expect(button.closest('[data-focus-key]')).toBe(button)
|
||
})
|
||
})
|
||
|
||
describe('an open tile', () => {
|
||
it('shows type, DOI and URL as safe links, « Aucun » / « Aucune » when absent', async () => {
|
||
const user = await mount()
|
||
await user.click(header('Code rural'))
|
||
await user.click(header('Denoix'))
|
||
const doi = within(tile('Code rural')).getByRole('link', {
|
||
name: /10\.1000\/ABC/,
|
||
})
|
||
expect(doi).toHaveAttribute('href', 'https://doi.org/10.1000/ABC')
|
||
expect(doi).toHaveAttribute('target', '_blank')
|
||
expect(doi).toHaveAttribute('rel', 'noopener noreferrer')
|
||
expect(within(tile('Code rural')).getByText('Aucune')).toBeInTheDocument()
|
||
const url = within(tile('Denoix')).getByRole('link', {
|
||
name: /exemple\.org/,
|
||
})
|
||
expect(url).toHaveAttribute('href', 'https://exemple.org/%C3%89paule')
|
||
expect(within(tile('Denoix')).getByText('Aucun')).toBeInTheDocument()
|
||
})
|
||
|
||
it('lists the citers, a family marked, and the count is the list length', async () => {
|
||
const user = await mount()
|
||
await user.click(header('Barone'))
|
||
const open = within(tile('Barone'))
|
||
expect(open.getByText('Citée 2 fois, par :')).toBeInTheDocument()
|
||
expect(open.getAllByRole('button', { name: 'Aller à' })).toHaveLength(2)
|
||
expect(open.getAllByText('famille')).toHaveLength(1)
|
||
expect(open.getByText(/Zone A › Cat A ›/)).toBeInTheDocument()
|
||
})
|
||
|
||
it('says so when nothing cites it, and offers « Supprimer » only then', async () => {
|
||
const user = await mount()
|
||
await user.click(header('Barone'))
|
||
await user.click(header('Code rural'))
|
||
const cited = within(tile('Barone'))
|
||
expect(cited.queryByRole('button', { name: 'Supprimer' })).toBeNull()
|
||
expect(
|
||
cited.getByText(/Pour supprimer cette référence/),
|
||
).toBeInTheDocument()
|
||
const uncited = within(tile('Code rural'))
|
||
expect(
|
||
uncited.getByText(
|
||
'Aucun constat ni aucune famille ne cite cette référence.',
|
||
),
|
||
).toBeInTheDocument()
|
||
expect(
|
||
uncited.getByRole('button', { name: 'Supprimer' }),
|
||
).toBeInTheDocument()
|
||
expect(uncited.queryByText(/Pour supprimer cette référence/)).toBeNull()
|
||
})
|
||
|
||
it('« Aller à » on a citer opens the Constats tab on it', async () => {
|
||
const user = await mount()
|
||
await user.click(header('Barone'))
|
||
const [family] = within(tile('Barone')).getAllByRole('button', {
|
||
name: 'Aller à',
|
||
})
|
||
await user.click(family)
|
||
expect(screen.getByRole('tab', { name: 'Constats' })).toHaveAttribute(
|
||
'aria-selected',
|
||
'true',
|
||
)
|
||
})
|
||
|
||
it('never links a javascript: address', () => {
|
||
const risky = {
|
||
id: 'ref-x',
|
||
type: 'web',
|
||
citation: 'X',
|
||
url: 'javascript:alert(1)',
|
||
doi: 'javascript:alert(2)',
|
||
} as const
|
||
render(<ReferenceFacts reference={risky} />)
|
||
expect(screen.queryByRole('link')).toBeNull()
|
||
expect(screen.getByText('javascript:alert(1)')).toBeInTheDocument()
|
||
})
|
||
})
|
||
|
||
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)
|
||
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(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('read-only tiles', () => {
|
||
it('keep DOI, URL and « Aller à », and offer no edit control', async () => {
|
||
const user = await mount(800)
|
||
await user.click(header('Barone'))
|
||
await user.click(header('Denoix'))
|
||
const cited = within(tile('Barone'))
|
||
expect(cited.getAllByRole('button', { name: 'Aller à' })).toHaveLength(2)
|
||
expect(
|
||
within(tile('Denoix')).getByRole('link', { name: /exemple\.org/ }),
|
||
).toBeInTheDocument()
|
||
expect(
|
||
within(tabPanel()).queryByRole('button', { name: 'Modifier' }),
|
||
).toBeNull()
|
||
expect(
|
||
within(tabPanel()).queryByRole('textbox', { name: 'Citation' }),
|
||
).toBeNull()
|
||
})
|
||
})
|
||
|
||
describe('reopening the editor', () => {
|
||
it('starts with every tile closed and none in edit', async () => {
|
||
const user = await mount()
|
||
await startEdit(user, 'Barone')
|
||
await user.click(header('Denoix'))
|
||
cleanup()
|
||
await mount()
|
||
const opened = within(tabPanel()).queryAllByRole('button', {
|
||
expanded: true,
|
||
})
|
||
expect(opened).toHaveLength(0)
|
||
expect(
|
||
within(tabPanel()).queryByLabelText('Citation', {
|
||
selector: '#tab-reference-0-ref-a-citation',
|
||
}),
|
||
).toBeNull()
|
||
})
|
||
})
|