455 lines
16 KiB
TypeScript
455 lines
16 KiB
TypeScript
/** R2F-3: the finding dialog (view, edit, creation), its history entries and its guards. */
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
act,
|
|
cleanup,
|
|
fireEvent,
|
|
screen,
|
|
waitFor,
|
|
within,
|
|
} from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import type { Location, NavigateFunction } from 'react-router-dom'
|
|
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { edit, insert } from '../helpers/editorFixtures'
|
|
import { defaultView } from '@/editor/navigation'
|
|
import {
|
|
baseText,
|
|
resetStore,
|
|
serverHas,
|
|
SHEET,
|
|
store,
|
|
USER,
|
|
} from '../helpers/editorHarness'
|
|
import { mountDraft, setWidth } from '../helpers/editorRoute'
|
|
import {
|
|
findingDialog,
|
|
inFindingDialog,
|
|
openFinding,
|
|
treeRow,
|
|
} from '../helpers/findingDialog'
|
|
|
|
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(),
|
|
}))
|
|
|
|
let navigate: NavigateFunction
|
|
let location: Location
|
|
const probe = (n: NavigateFunction, l: Location) => {
|
|
navigate = n
|
|
location = l
|
|
}
|
|
const written = () =>
|
|
(location.state as Record<string, HistoryEntry> | null)?.[ENTRY_KEY]
|
|
const back = () => act(async () => navigate(-1))
|
|
|
|
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
|
|
afterEach(() => {
|
|
cleanup()
|
|
resetStore()
|
|
setWidth(1024)
|
|
})
|
|
|
|
const path = `/app/admin/sheets/${SHEET}/draft`
|
|
const name = (n: string) => ({ fr: n, en: n })
|
|
|
|
/** Zone A > Cat A > Node A (cites nothing). */
|
|
function sheetText(): string {
|
|
let text = insert(baseText(), {
|
|
kind: 'zone',
|
|
value: { id: 'zone-a', name: name('Zone A'), 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 A'),
|
|
description: {},
|
|
reference_ids: [],
|
|
review_status: 'to_confirm',
|
|
children: [],
|
|
},
|
|
},
|
|
})
|
|
return 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 A'),
|
|
description: {},
|
|
requires_vet_referral: false,
|
|
reference_ids: [],
|
|
review_status: 'to_confirm',
|
|
children: [],
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
type User = ReturnType<typeof userEvent.setup>
|
|
|
|
async function mount(width = 1280): Promise<User> {
|
|
serverHas(sheetText(), 3)
|
|
const user = userEvent.setup()
|
|
mountDraft({ width, probe })
|
|
await screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
|
|
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
|
await waitFor(() => expect(written()?.view.tab).toBe('findings'))
|
|
return user
|
|
}
|
|
|
|
const doc = () => JSON.parse(store.getState().documentText)
|
|
const entries = () => store.getState().undo.length
|
|
const inDialog = () => inFindingDialog()
|
|
const modify = (user: User) =>
|
|
user.click(inDialog().getByRole('button', { name: 'Modifier' }))
|
|
const nameField = () => inDialog().getByLabelText('Nom (FR)')
|
|
const closed = () => waitFor(() => expect(findingDialog()).toBeNull())
|
|
|
|
describe('opening', () => {
|
|
it('a click, Enter and Space each select the row and open its dialog; the tree stays', async () => {
|
|
const user = await mount()
|
|
const before = screen.getAllByRole('treeitem').length
|
|
await openFinding(user, /Cat A/)
|
|
expect(treeRow(/Cat A/)).toHaveAttribute('aria-selected', 'true')
|
|
expect(inDialog().getByRole('heading', { name: 'Cat A' })).toBeVisible()
|
|
await user.click(inDialog().getByRole('button', { name: 'Fermer' }))
|
|
await closed()
|
|
treeRow(/Cat A/).focus()
|
|
await user.keyboard('{Enter}')
|
|
await waitFor(() => expect(findingDialog()).not.toBeNull())
|
|
await user.keyboard('{Escape}')
|
|
await closed()
|
|
await user.keyboard(' ')
|
|
await waitFor(() => expect(findingDialog()).not.toBeNull())
|
|
expect(screen.getAllByRole('treeitem')).toHaveLength(before)
|
|
})
|
|
|
|
it('writes one entry on open; Back closes it and the row holds the focus', async () => {
|
|
const user = await mount()
|
|
const first = written()!.id
|
|
await openFinding(user, /Cat A/)
|
|
await waitFor(() => expect(written()?.id).not.toBe(first))
|
|
expect(written()?.view.finding).toMatchObject({ citations: false })
|
|
expect(written()?.below?.finding).toBeNull()
|
|
await back()
|
|
await closed()
|
|
await waitFor(() => expect(document.activeElement).toBe(treeRow(/Cat A/)))
|
|
expect(written()?.id).toBe(first)
|
|
})
|
|
|
|
it('the close button asks nothing of a pristine view and pops its entry', async () => {
|
|
const user = await mount()
|
|
const first = written()!.id
|
|
await openFinding(user, /Cat A/)
|
|
await waitFor(() => expect(written()?.id).not.toBe(first))
|
|
await user.click(inDialog().getByRole('button', { name: 'Fermer' }))
|
|
await closed()
|
|
await waitFor(() => expect(written()?.id).toBe(first))
|
|
expect(document.activeElement).toBe(treeRow(/Cat A/))
|
|
})
|
|
})
|
|
|
|
describe('edit mode', () => {
|
|
it('a pristine « Enregistrer » is no command and no entry; a changed one is one', async () => {
|
|
const user = await mount()
|
|
await openFinding(user, /Cat A/)
|
|
const before = entries()
|
|
await modify(user)
|
|
await user.click(inDialog().getByRole('button', { name: 'Enregistrer' }))
|
|
expect(entries()).toBe(before)
|
|
expect(inDialog().getByRole('button', { name: 'Modifier' })).toHaveFocus()
|
|
await modify(user)
|
|
await user.clear(nameField())
|
|
await user.type(nameField(), 'Renommée')
|
|
await user.click(inDialog().getByRole('button', { name: 'Enregistrer' }))
|
|
expect(entries()).toBe(before + 1)
|
|
expect(doc().finding_zones[0].categories[0].name.fr).toBe('Renommée')
|
|
expect(inDialog().getByRole('status')).toHaveTextContent(
|
|
'Modifications enregistrées',
|
|
)
|
|
})
|
|
|
|
it('a blank name is refused on its field, nothing is written', async () => {
|
|
const user = await mount()
|
|
await openFinding(user, /Cat A/)
|
|
await modify(user)
|
|
const before = entries()
|
|
await user.clear(nameField())
|
|
await user.click(inDialog().getByRole('button', { name: 'Enregistrer' }))
|
|
expect(entries()).toBe(before)
|
|
expect(nameField()).toHaveFocus()
|
|
expect(nameField()).toHaveAccessibleDescription('Ce champ est obligatoire.')
|
|
})
|
|
|
|
it('« Annuler » with changes asks first; « Abandonner » commits nothing', async () => {
|
|
const user = await mount()
|
|
await openFinding(user, /Cat A/)
|
|
await modify(user)
|
|
const before = entries()
|
|
await user.type(nameField(), ' bis')
|
|
await user.click(inDialog().getByRole('button', { name: 'Annuler' }))
|
|
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
|
|
expect(
|
|
inDialog().getByRole('button', { name: 'Continuer la saisie' }),
|
|
).toHaveFocus()
|
|
await user.click(
|
|
inDialog().getByRole('button', { name: 'Continuer la saisie' }),
|
|
)
|
|
expect(nameField()).toHaveValue('Cat A bis')
|
|
await user.click(inDialog().getByRole('button', { name: 'Annuler' }))
|
|
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
|
|
expect(entries()).toBe(before)
|
|
expect(doc().finding_zones[0].categories[0].name.fr).toBe('Cat A')
|
|
expect(inDialog().getByRole('button', { name: 'Modifier' })).toBeVisible()
|
|
})
|
|
|
|
it('a dirty close request (Escape, ✕, a native cancel) asks and keeps the dialog', async () => {
|
|
const user = await mount()
|
|
await openFinding(user, /Cat A/)
|
|
await modify(user)
|
|
await user.type(nameField(), ' bis')
|
|
await user.keyboard('{Escape}')
|
|
expect(findingDialog()).not.toBeNull()
|
|
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
|
|
await user.click(
|
|
inDialog().getByRole('button', { name: 'Continuer la saisie' }),
|
|
)
|
|
await user.click(inDialog().getByRole('button', { name: 'Fermer' }))
|
|
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
|
|
fireEvent(findingDialog()!, new Event('cancel', { cancelable: true }))
|
|
expect(findingDialog()).not.toBeNull()
|
|
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
|
|
await closed()
|
|
expect(doc().finding_zones[0].categories[0].name.fr).toBe('Cat A')
|
|
})
|
|
|
|
it('a Back with changes asks, keeps the page and the draft, then Abandonner pops once', async () => {
|
|
const user = await mount()
|
|
const first = written()!.id
|
|
await openFinding(user, /Cat A/)
|
|
await waitFor(() => expect(written()?.id).not.toBe(first))
|
|
await modify(user)
|
|
await user.type(nameField(), ' bis')
|
|
await back()
|
|
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
|
|
expect(findingDialog()).not.toBeNull()
|
|
fireEvent.click(
|
|
inDialog().getByRole('button', { name: 'Continuer la saisie' }),
|
|
)
|
|
expect(nameField()).toHaveValue('Cat A bis')
|
|
fireEvent(findingDialog()!, new Event('cancel', { cancelable: true }))
|
|
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
|
|
await closed()
|
|
await waitFor(() => expect(written()?.view.finding).toBeNull())
|
|
expect(entries()).toBe(0)
|
|
// The refused Back pushes the view again from an effect: the test router's low-priority
|
|
// transition lands up to 5 s later (a test-DOM artifact, a browser applies it at once).
|
|
}, 20_000)
|
|
})
|
|
|
|
describe('creation', () => {
|
|
it('is a layer of its own, replaced by the element after « Créer » (no extra entry)', async () => {
|
|
const user = await mount()
|
|
const first = written()!.id
|
|
await user.click(screen.getByRole('button', { name: 'Ajouter une zone' }))
|
|
await waitFor(() => expect(written()?.id).not.toBe(first))
|
|
const creation = written()!.id
|
|
expect(written()?.view.finding).toEqual({
|
|
create: { kind: 'zone', parent: null },
|
|
})
|
|
await user.type(document.getElementById('finding-new-name-fr')!, 'Tête')
|
|
await user.type(document.getElementById('finding-new-name-en')!, 'Head')
|
|
await user.click(
|
|
within(findingDialog()!).getByRole('button', { name: 'Créer' }),
|
|
)
|
|
await waitFor(() => expect(written()?.view.finding).toHaveProperty('key'))
|
|
expect(written()?.id).toBe(creation)
|
|
expect(doc().finding_zones).toHaveLength(2)
|
|
await back()
|
|
await closed()
|
|
expect(written()?.id).toBe(first)
|
|
})
|
|
|
|
it('a dirty creation asks before it closes and writes nothing', async () => {
|
|
const user = await mount()
|
|
await user.click(screen.getByRole('button', { name: 'Ajouter une zone' }))
|
|
await user.type(document.getElementById('finding-new-name-fr')!, 'Tête')
|
|
await user.keyboard('{Escape}')
|
|
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
|
|
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
|
|
await closed()
|
|
expect(doc().finding_zones).toHaveLength(1)
|
|
await waitFor(() =>
|
|
expect(document.activeElement).toBe(
|
|
screen.getByRole('button', { name: 'Ajouter une zone' }),
|
|
),
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('read-only flip and the gone rule', () => {
|
|
it('1280 to 900 to 1280 keeps the draft and the focus, « Enregistrer » off meanwhile', async () => {
|
|
const user = await mount()
|
|
await openFinding(user, /Cat A/)
|
|
await modify(user)
|
|
await user.type(nameField(), ' bis')
|
|
const field = nameField()
|
|
act(() => {
|
|
setWidth(900)
|
|
window.dispatchEvent(new Event('resize'))
|
|
})
|
|
expect(nameField()).toBe(field)
|
|
expect(field).toHaveAttribute('readonly')
|
|
expect(field).toHaveFocus()
|
|
expect(inDialog().getByText(/Élargissez la fenêtre/)).toBeVisible()
|
|
expect(
|
|
inDialog().getByRole('button', { name: 'Enregistrer' }),
|
|
).toBeDisabled()
|
|
expect(doc().finding_zones[0].categories[0].name.fr).toBe('Cat A')
|
|
act(() => {
|
|
setWidth(1280)
|
|
window.dispatchEvent(new Event('resize'))
|
|
})
|
|
expect(field).not.toHaveAttribute('readonly')
|
|
expect(field).toHaveValue('Cat A bis')
|
|
expect(field).toHaveFocus()
|
|
})
|
|
|
|
it('a flip to read-only while « Modifier » holds the focus keeps it in the dialog (the tab row is inert)', async () => {
|
|
const user = await mount()
|
|
await openFinding(user, /Cat A/)
|
|
const button = inDialog().getByRole('button', { name: 'Modifier' })
|
|
button.focus()
|
|
expect(button).toHaveFocus()
|
|
act(() => {
|
|
setWidth(900)
|
|
window.dispatchEvent(new Event('resize'))
|
|
})
|
|
// The edit-only actions left the page: the focus moved to the dialog's first control.
|
|
expect(button.isConnected).toBe(false)
|
|
expect(findingDialog()!.contains(document.activeElement)).toBe(true)
|
|
})
|
|
|
|
it('a forced close of a dirty form says the dropped draft once, with the gone text', async () => {
|
|
const user = await mount()
|
|
await openFinding(user, /Node A/)
|
|
await modify(user)
|
|
await user.type(nameField(), ' bis')
|
|
act(() => {
|
|
store.getState().commit({
|
|
op: 'remove',
|
|
key: {
|
|
kind: 'finding',
|
|
zone_id: 'zone-a',
|
|
path: ['fcat-a', 'fnode-a'],
|
|
},
|
|
})
|
|
})
|
|
await closed()
|
|
const live = () => document.querySelector('p.sr-only[aria-live="polite"]')!
|
|
await waitFor(() =>
|
|
expect(live().textContent).toBe(
|
|
"Saisie abandonnée. Ce constat n'existe plus",
|
|
),
|
|
)
|
|
})
|
|
|
|
it('an element that vanishes closes its dialog by replace and says so', async () => {
|
|
const user = await mount()
|
|
await openFinding(user, /Node A/)
|
|
const open = written()!
|
|
act(() => {
|
|
store.getState().commit({
|
|
op: 'remove',
|
|
key: {
|
|
kind: 'finding',
|
|
zone_id: 'zone-a',
|
|
path: ['fcat-a', 'fnode-a'],
|
|
},
|
|
})
|
|
})
|
|
await closed()
|
|
await waitFor(() =>
|
|
expect(
|
|
document.querySelector('p.sr-only[aria-live="polite"]'),
|
|
).toHaveTextContent("Ce constat n'existe plus"),
|
|
)
|
|
expect(written()?.id).toBe(open.id)
|
|
expect(written()?.view.finding).toBeNull()
|
|
})
|
|
|
|
it('a reading dialog restored for a key the sheet lacks is dropped, with its announcement', async () => {
|
|
serverHas(sheetText(), 3)
|
|
const gone = {
|
|
kind: 'finding',
|
|
zone_id: 'zone-a',
|
|
path: ['fcat-a', 'gone'],
|
|
}
|
|
const view = {
|
|
...defaultView('findings'),
|
|
finding: { key: gone, citations: false },
|
|
}
|
|
const state = {
|
|
[ENTRY_KEY]: {
|
|
id: 'old',
|
|
view,
|
|
below: null,
|
|
sheetId: SHEET,
|
|
userId: USER,
|
|
},
|
|
}
|
|
mountDraft({ probe, entries: [{ pathname: path, state }] })
|
|
await screen.findByRole('heading', {
|
|
level: 1,
|
|
name: 'Brouillon, version 4',
|
|
})
|
|
await waitFor(() => expect(written()?.view.finding).toBeNull())
|
|
expect(findingDialog()).toBeNull()
|
|
expect(written()?.id).toBe('old')
|
|
await waitFor(() =>
|
|
expect(
|
|
document.querySelector('p.sr-only[aria-live="polite"]'),
|
|
).toHaveTextContent("Ce constat n'existe plus"),
|
|
)
|
|
})
|
|
|
|
it('a creation form in the entry is never restored by a reload', async () => {
|
|
serverHas(sheetText(), 3)
|
|
const view = {
|
|
...defaultView('findings'),
|
|
finding: { create: { kind: 'zone', parent: null } },
|
|
}
|
|
const state = {
|
|
[ENTRY_KEY]: {
|
|
id: 'old',
|
|
view,
|
|
below: null,
|
|
sheetId: SHEET,
|
|
userId: USER,
|
|
},
|
|
}
|
|
mountDraft({ probe, entries: [{ pathname: path, state }] })
|
|
await screen.findByRole('heading', {
|
|
level: 1,
|
|
name: 'Brouillon, version 4',
|
|
})
|
|
await waitFor(() => expect(written()?.view.finding).toBeNull())
|
|
expect(findingDialog()).toBeNull()
|
|
})
|
|
})
|