Draft tile gets « Annuler l'ajout »; Escape cancels from non-text targets and from a pristine text field, never while composing or already handled. Nothing reaches the document, history untouched, focus back on the add button.
825 lines
29 KiB
TypeScript
825 lines
29 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 {
|
||
act,
|
||
cleanup,
|
||
fireEvent,
|
||
render,
|
||
screen,
|
||
within,
|
||
} from '@testing-library/react'
|
||
import userEvent from '@testing-library/user-event'
|
||
import ReferenceFacts from '@/components/editor/references/ReferenceFacts'
|
||
import ReferencesTab from '@/components/editor/tabs/ReferencesTab'
|
||
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(
|
||
<ReferencesTab
|
||
readOnly={false}
|
||
navigate={vi.fn()}
|
||
reloadSheet={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(
|
||
<ReferencesTab
|
||
readOnly={false}
|
||
navigate={vi.fn()}
|
||
reloadSheet={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(
|
||
<ReferencesTab
|
||
readOnly={false}
|
||
navigate={vi.fn()}
|
||
reloadSheet={vi.fn()}
|
||
libraryReset={0}
|
||
/>,
|
||
)
|
||
const box = screen.getByRole('searchbox', {
|
||
name: 'Rechercher une référence',
|
||
})
|
||
await user.type(box, 'Code')
|
||
view.rerender(
|
||
<ReferencesTab
|
||
readOnly={false}
|
||
navigate={vi.fn()}
|
||
reloadSheet={vi.fn()}
|
||
libraryReset={0}
|
||
/>,
|
||
)
|
||
expect(box).toHaveValue('Code')
|
||
view.rerender(
|
||
<ReferencesTab
|
||
readOnly={false}
|
||
navigate={vi.fn()}
|
||
reloadSheet={vi.fn()}
|
||
libraryReset={1}
|
||
/>,
|
||
)
|
||
expect(box).toHaveValue('')
|
||
await user.type(box, 'a')
|
||
view.rerender(
|
||
<ReferencesTab
|
||
readOnly={false}
|
||
navigate={vi.fn()}
|
||
reloadSheet={vi.fn()}
|
||
libraryReset={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)
|
||
})
|
||
})
|
||
|
||
describe("« Annuler l'ajout » (C2)", () => {
|
||
const addButton = () =>
|
||
within(tabPanel()).getByRole('button', { name: 'Ajouter une référence' })
|
||
const field = (id: string) =>
|
||
tabPanel().querySelector<HTMLElement>(`#tab-reference-new-${id}`)!
|
||
const draftTile = () =>
|
||
within(tabPanel()).queryByLabelText('Nouvelle référence')
|
||
const cancelButton = () =>
|
||
within(tabPanel()).getByRole('button', { name: "Annuler l'ajout" })
|
||
const history = () => ({
|
||
undo: store.getState().undo.length,
|
||
redo: store.getState().redo.length,
|
||
})
|
||
const refs = () => JSON.parse(store.getState().documentText).references
|
||
|
||
/** One real change undone, so a redo is pending; then the draft is opened. */
|
||
async function openWithPendingRedo() {
|
||
const user = await mount()
|
||
await startEditAndType(user)
|
||
act(() => store.getState().undoStep())
|
||
await user.click(addButton())
|
||
return user
|
||
}
|
||
async function startEditAndType(user: User) {
|
||
await user.click(header('Barone, Ostéologie'))
|
||
const item = header('Barone, Ostéologie').closest('li')!
|
||
await user.click(within(item).getByRole('button', { name: 'Modifier' }))
|
||
await user.type(within(item).getByLabelText('DOI'), '10.1/x')
|
||
await user.click(within(item).getByRole('button', { name: 'Terminer' }))
|
||
}
|
||
function expectCancelled(before: { refs: unknown; history: object }) {
|
||
expect(draftTile()).toBeNull()
|
||
expect(refs()).toEqual(before.refs)
|
||
expect(history()).toEqual(before.history)
|
||
expect(document.activeElement).toBe(addButton())
|
||
}
|
||
const snapshot = () => ({ refs: refs(), history: history() })
|
||
|
||
it('the button removes the tile, writes nothing, keeps a pending redo', async () => {
|
||
const user = await openWithPendingRedo()
|
||
expect(history().redo).toBe(1)
|
||
const before = snapshot()
|
||
await user.click(cancelButton())
|
||
expectCancelled(before)
|
||
})
|
||
|
||
it('Escape on the type select and on a button does the same', async () => {
|
||
const user = await openWithPendingRedo()
|
||
const before = snapshot()
|
||
await user.type(field('citation'), 'Brouillon')
|
||
fireEvent.keyDown(field('type'), { key: 'Escape' })
|
||
expectCancelled(before)
|
||
await user.click(addButton())
|
||
await user.type(field('citation'), 'Brouillon')
|
||
fireEvent.keyDown(cancelButton(), { key: 'Escape' })
|
||
expectCancelled(before)
|
||
})
|
||
|
||
it('Escape in a field cancels while the form is pristine, not once typed', async () => {
|
||
const user = await openWithPendingRedo()
|
||
const before = snapshot()
|
||
fireEvent.keyDown(field('citation'), { key: 'Escape' })
|
||
expectCancelled(before)
|
||
await user.click(addButton())
|
||
await user.type(field('citation'), 'Brouillon')
|
||
fireEvent.keyDown(field('citation'), { key: 'Escape' })
|
||
expect(draftTile()).not.toBeNull()
|
||
expect(field('citation')).toHaveValue('Brouillon')
|
||
})
|
||
|
||
it('Escape while composing, or already handled, does nothing', async () => {
|
||
const user = await mount()
|
||
await user.click(addButton())
|
||
fireEvent.keyDown(field('type'), { key: 'Escape', isComposing: true })
|
||
fireEvent.keyDown(field('type'), { key: 'Escape', keyCode: 229 })
|
||
expect(draftTile()).not.toBeNull()
|
||
const handled = new KeyboardEvent('keydown', {
|
||
key: 'Escape',
|
||
bubbles: true,
|
||
cancelable: true,
|
||
})
|
||
handled.preventDefault()
|
||
act(() => void field('type').dispatchEvent(handled))
|
||
expect(draftTile()).not.toBeNull()
|
||
})
|
||
|
||
it('Escape that cancels is marked handled', async () => {
|
||
const user = await mount()
|
||
await user.click(addButton())
|
||
const event = new KeyboardEvent('keydown', {
|
||
key: 'Escape',
|
||
bubbles: true,
|
||
cancelable: true,
|
||
})
|
||
act(() => void field('type').dispatchEvent(event))
|
||
expect(event.defaultPrevented).toBe(true)
|
||
})
|
||
|
||
it('goes with its refusal message, and a new add starts from an empty form', async () => {
|
||
const user = await mount()
|
||
await user.click(addButton())
|
||
await user.type(field('citation'), 'Brouillon')
|
||
await user.click(
|
||
within(tabPanel()).getByRole('button', { name: 'Ajouter la référence' }),
|
||
)
|
||
expect(within(tabPanel()).getByRole('alert')).toBeInTheDocument()
|
||
await user.click(cancelButton())
|
||
expect(within(tabPanel()).queryByRole('alert')).toBeNull()
|
||
await user.click(addButton())
|
||
expect(field('citation')).toHaveValue('')
|
||
expect(field('type')).toHaveValue('')
|
||
expect(within(tabPanel()).queryByRole('alert')).toBeNull()
|
||
})
|
||
|
||
it('« Défaire » after a cancel undoes the previous real change', async () => {
|
||
const user = await mount()
|
||
await startEditAndType(user)
|
||
expect(history().undo).toBe(1)
|
||
await user.click(addButton())
|
||
await user.click(cancelButton())
|
||
expect(history().undo).toBe(1)
|
||
act(() => store.getState().undoStep())
|
||
expect(history()).toEqual({ undo: 0, redo: 1 })
|
||
expect(refs()[0].doi).toBeNull()
|
||
})
|
||
})
|
||
|
||
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)
|
||
const before = store.getState().undo.length
|
||
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(store.getState().undo.length - before).toBe(2)
|
||
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('the disclosure button in edit (M7)', () => {
|
||
it('is named for what it does, and pressing it ends the edit', async () => {
|
||
const user = await mount()
|
||
const item = await startEdit(user, 'Barone')
|
||
const button = within(item).getByRole('button', {
|
||
name: /^Terminer\. Modifier la référence/,
|
||
})
|
||
expect(button).toHaveAttribute('aria-expanded', 'true')
|
||
await user.click(button)
|
||
expect(within(item).queryByLabelText('Citation')).toBeNull()
|
||
})
|
||
})
|
||
|
||
describe('one user edit, one history entry (C1)', () => {
|
||
const entries = () => store.getState().undo.length
|
||
const undo = () => act(() => store.getState().undoStep())
|
||
|
||
it('« Terminer » after one typed field adds one entry, the first « Défaire » undoes it', async () => {
|
||
const user = await mount()
|
||
const view = within(await startEdit(user, 'Barone'))
|
||
const before = entries()
|
||
await user.type(view.getByLabelText('Adresse web (URL)'), 'https://a.org/')
|
||
await user.click(view.getByRole('button', { name: 'Terminer' }))
|
||
expect(entries() - before).toBe(1)
|
||
expect(doc().references[0].url).toBe('https://a.org/')
|
||
undo()
|
||
expect(doc().references[0].url).toBeNull()
|
||
})
|
||
|
||
it('« Terminer » pressed with the field still focused (no pointer) adds one entry', async () => {
|
||
const user = await mount()
|
||
const view = within(await startEdit(user, 'Barone'))
|
||
const before = entries()
|
||
const url = view.getByLabelText('Adresse web (URL)')
|
||
await user.type(url, 'https://a.org/')
|
||
expect(document.activeElement).toBe(url)
|
||
fireEvent.click(view.getByRole('button', { name: 'Terminer' }))
|
||
expect(entries() - before).toBe(1)
|
||
undo()
|
||
expect(doc().references[0].url).toBeNull()
|
||
})
|
||
|
||
it('a tab switch with the field still focused adds one entry', async () => {
|
||
const user = await mount()
|
||
const view = within(await startEdit(user, 'Barone'))
|
||
const before = entries()
|
||
await user.type(view.getByLabelText('Adresse web (URL)'), 'https://a.org/')
|
||
fireEvent.click(screen.getByRole('tab', { name: 'Constats' }))
|
||
expect(entries() - before).toBe(1)
|
||
undo()
|
||
expect(doc().references[0].url).toBeNull()
|
||
})
|
||
|
||
it('a blur after the field was applied adds nothing; a changed text adds one more', async () => {
|
||
const user = await mount()
|
||
const view = within(await startEdit(user, 'Barone'))
|
||
const before = entries()
|
||
const url = view.getByLabelText('Adresse web (URL)')
|
||
await user.type(url, 'https://a.org/')
|
||
await user.tab()
|
||
await user.click(url)
|
||
await user.tab()
|
||
expect(entries() - before).toBe(1)
|
||
await user.type(url, 'x')
|
||
await user.tab()
|
||
expect(entries() - before).toBe(2)
|
||
})
|
||
})
|
||
|
||
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()
|
||
})
|
||
})
|