Tests: note ends on another tile's Modifier, on Ajouter une reference, and is replaced (announced again) by a newer drop; long titles shortened. Read-only toggle deletes the key from openKeys explicitly. Drop slot and tile wrappers moved to useTileActions. pinEdited pins nothing for an absent key.
381 lines
13 KiB
TypeScript
381 lines
13 KiB
TypeScript
/** C1: a read-only flip never drops a draft silently, writes nothing, and keeps the tile. */
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { act, cleanup, screen, waitFor, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { edit, insert } from '../helpers/editorFixtures'
|
|
import {
|
|
baseText,
|
|
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 = '') =>
|
|
({
|
|
kind: 'reference',
|
|
value: { id, type: 'book', citation, url: url || null, doi: null },
|
|
}) as const
|
|
|
|
/** Three references (ref-b has a URL) and Node A citing ref-a and ref-b. */
|
|
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'))
|
|
text = insert(text, {
|
|
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: ['ref-a', 'ref-b'],
|
|
review_status: 'to_confirm',
|
|
children: [],
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
type User = ReturnType<typeof userEvent.setup>
|
|
const NOTE = 'Saisie non appliquée'
|
|
const live = () => document.querySelector('p.sr-only[aria-live="polite"]')!
|
|
const doc = () => JSON.parse(store.getState().documentText)
|
|
const history = () => ({
|
|
undo: store.getState().undo.length,
|
|
redo: store.getState().redo.length,
|
|
})
|
|
const cross = (width: number) =>
|
|
act(() => {
|
|
setWidth(width)
|
|
window.dispatchEvent(new Event('resize'))
|
|
})
|
|
|
|
const flush = () => act(async () => undefined)
|
|
|
|
/** Counts the live region's updates that carry the drop note. */
|
|
function watchNotes() {
|
|
const seen: string[] = []
|
|
const observer = new MutationObserver(() => {
|
|
if (live().textContent?.includes(NOTE)) seen.push(live().textContent!)
|
|
})
|
|
observer.observe(live(), {
|
|
childList: true,
|
|
characterData: true,
|
|
subtree: true,
|
|
})
|
|
return { seen, stop: () => observer.disconnect() }
|
|
}
|
|
|
|
async function mount(width = 1280) {
|
|
serverHas(sheetText(), 3)
|
|
const user = userEvent.setup()
|
|
mountDraft({ width, strict: true })
|
|
await screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
|
|
return user
|
|
}
|
|
|
|
const tabPanel = () => screen.getByRole('tabpanel', { name: 'Références' })
|
|
const header = (id: string, host = 'tab') =>
|
|
document.querySelector<HTMLElement>(
|
|
`button[id^="${host}-reference-"][id$="-${id}-header"]`,
|
|
)!
|
|
const tile = (id: string, host = 'tab') => header(id, host).closest('li')!
|
|
const field = (id: string, name: string, host = 'tab') =>
|
|
document.querySelector<HTMLInputElement>(
|
|
`#${host}-reference-${id === 'ref-a' ? 0 : id === 'ref-b' ? 1 : 2}-${id}-${name}`,
|
|
)!
|
|
|
|
/** « Références », the tile open and in edit; the focus on its Citation field. */
|
|
async function editTile(user: User, id: string, label: RegExp) {
|
|
await user.click(screen.getByRole('tab', { name: 'Références' }))
|
|
await user.click(within(tabPanel()).getByRole('button', { name: label }))
|
|
await user.click(within(tile(id)).getByRole('button', { name: 'Modifier' }))
|
|
}
|
|
|
|
/** Types a schemeless URL (refused) in the focused-then-kept URL field of `id`. */
|
|
async function typeBadUrl(user: User, id: string) {
|
|
const url = field(id, 'url')
|
|
await user.clear(url)
|
|
await user.type(url, 'exemple.org')
|
|
return url
|
|
}
|
|
|
|
describe('a read-only flip with a refused draft (tab tile)', () => {
|
|
it('shows the note, says it once, writes nothing, focuses the tile', async () => {
|
|
const user = await mount()
|
|
await editTile(user, 'ref-b', /^Denoix/)
|
|
await typeBadUrl(user, 'ref-b')
|
|
const before = { references: doc().references, ...history() }
|
|
const watch = watchNotes()
|
|
cross(800)
|
|
expect(
|
|
within(tile('ref-b')).getByText(`${NOTE} : Adresse web (URL)`),
|
|
).toBeVisible()
|
|
expect(within(tile('ref-b')).queryByRole('textbox')).toBeNull()
|
|
expect(
|
|
within(tile('ref-b')).getByText('https://exemple.org/Épaule'),
|
|
).toBeVisible()
|
|
await flush()
|
|
expect(watch.seen).toHaveLength(1)
|
|
expect(live().textContent).toBe(`${NOTE} : Adresse web (URL)`)
|
|
expect({ references: doc().references, ...history() }).toEqual(before)
|
|
expect(document.activeElement).toBe(header('ref-b'))
|
|
watch.stop()
|
|
})
|
|
|
|
it('keeps the note across the flip back; Terminer removes it', async () => {
|
|
const user = await mount()
|
|
await editTile(user, 'ref-b', /^Denoix/)
|
|
await typeBadUrl(user, 'ref-b')
|
|
cross(800)
|
|
cross(1280)
|
|
const item = tile('ref-b')
|
|
expect(within(item).getByText(`${NOTE} : Adresse web (URL)`)).toBeVisible()
|
|
expect(field('ref-b', 'url')).toHaveValue('https://exemple.org/Épaule')
|
|
expect(history()).toEqual({ undo: 0, redo: 0 })
|
|
await user.click(within(item).getByRole('button', { name: 'Terminer' }))
|
|
expect(within(item).queryByText(new RegExp(NOTE))).toBeNull()
|
|
})
|
|
|
|
it('the read-only toggle ends the edit state, collapses and clears the note', async () => {
|
|
const user = await mount()
|
|
await editTile(user, 'ref-b', /^Denoix/)
|
|
await typeBadUrl(user, 'ref-b')
|
|
cross(800)
|
|
await user.click(header('ref-b'))
|
|
expect(header('ref-b')).toHaveAttribute('aria-expanded', 'false')
|
|
expect(within(tile('ref-b')).queryByText(new RegExp(NOTE))).toBeNull()
|
|
cross(1280)
|
|
expect(header('ref-b')).toHaveAttribute('aria-expanded', 'false')
|
|
expect(within(tile('ref-b')).queryByRole('textbox')).toBeNull()
|
|
})
|
|
|
|
it('names both refused fields, in field order', async () => {
|
|
const user = await mount()
|
|
await editTile(user, 'ref-b', /^Denoix/)
|
|
await user.clear(field('ref-b', 'citation'))
|
|
await typeBadUrl(user, 'ref-b')
|
|
cross(800)
|
|
expect(
|
|
within(tile('ref-b')).getByText(`${NOTE} : Citation, Adresse web (URL)`),
|
|
).toBeVisible()
|
|
expect(doc().references[1].citation).toBe('Denoix')
|
|
})
|
|
|
|
it('keeps the tile in edit shown when the search does not match it', async () => {
|
|
const user = await mount()
|
|
await editTile(user, 'ref-b', /^Denoix/)
|
|
await user.type(
|
|
within(tabPanel()).getByRole('searchbox', {
|
|
name: 'Rechercher une référence',
|
|
}),
|
|
'rural',
|
|
)
|
|
expect(header('ref-b')).not.toBeNull()
|
|
await typeBadUrl(user, 'ref-b')
|
|
cross(800)
|
|
expect(
|
|
within(tile('ref-b')).getByText(`${NOTE} : Adresse web (URL)`),
|
|
).toBeVisible()
|
|
expect(header('ref-c')).not.toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('the note ends on the next action', () => {
|
|
/** ref-b flipped with a refused URL and back: in edit again, note shown. */
|
|
async function flippedBack() {
|
|
const user = await mount()
|
|
await editTile(user, 'ref-b', /^Denoix/)
|
|
await typeBadUrl(user, 'ref-b')
|
|
cross(800)
|
|
cross(1280)
|
|
expect(
|
|
within(tile('ref-b')).getByText(/Saisie non appliquée/),
|
|
).toBeVisible()
|
|
return user
|
|
}
|
|
const noted = () => within(tabPanel()).queryAllByText(/Saisie non appliquée/)
|
|
|
|
it("ends on another tile's « Modifier » (this tile leaves edit)", async () => {
|
|
const user = await flippedBack()
|
|
await user.click(
|
|
within(tabPanel()).getByRole('button', { name: /^Barone/ }),
|
|
)
|
|
await user.click(
|
|
within(tile('ref-a')).getByRole('button', { name: 'Modifier' }),
|
|
)
|
|
expect(noted()).toHaveLength(0)
|
|
})
|
|
|
|
it('ends on « Ajouter une référence »', async () => {
|
|
const user = await flippedBack()
|
|
await user.click(
|
|
within(tabPanel()).getByRole('button', { name: 'Ajouter une référence' }),
|
|
)
|
|
expect(noted()).toHaveLength(0)
|
|
})
|
|
|
|
it('is replaced by a newer drop of the same tile, announced again', async () => {
|
|
const user = await flippedBack()
|
|
const watch = watchNotes()
|
|
await user.clear(field('ref-b', 'citation'))
|
|
await typeBadUrl(user, 'ref-b')
|
|
cross(800)
|
|
await flush()
|
|
const items = noted()
|
|
expect(items).toHaveLength(1)
|
|
expect(items[0]).toHaveTextContent(`${NOTE} : Citation, Adresse web (URL)`)
|
|
expect(watch.seen).toHaveLength(1)
|
|
watch.stop()
|
|
})
|
|
})
|
|
|
|
describe('a read-only flip without a refused draft', () => {
|
|
it('adds only the typed change when a valid draft has the focus', async () => {
|
|
const user = await mount()
|
|
await editTile(user, 'ref-a', /^Barone/)
|
|
const citation = field('ref-a', 'citation')
|
|
await user.clear(citation)
|
|
await user.type(citation, 'Neuf')
|
|
const watch = watchNotes()
|
|
cross(800)
|
|
expect(history()).toEqual({ undo: 1, redo: 0 })
|
|
expect(doc().references[0].citation).toBe('Neuf')
|
|
expect(within(tile('ref-a')).queryByText(new RegExp(NOTE))).toBeNull()
|
|
expect(document.activeElement).toBe(header('ref-a'))
|
|
cross(1280)
|
|
await flush()
|
|
expect(history()).toEqual({ undo: 1, redo: 0 })
|
|
expect(watch.seen).toHaveLength(0)
|
|
watch.stop()
|
|
})
|
|
|
|
it('writes nothing, says nothing and shows no note when nothing is pending', async () => {
|
|
const user = await mount()
|
|
await editTile(user, 'ref-a', /^Barone/)
|
|
const watch = watchNotes()
|
|
cross(800)
|
|
cross(1280)
|
|
await flush()
|
|
expect(history()).toEqual({ undo: 0, redo: 0 })
|
|
expect(watch.seen).toHaveLength(0)
|
|
expect(screen.queryByText(new RegExp(NOTE))).toBeNull()
|
|
watch.stop()
|
|
})
|
|
})
|
|
|
|
describe('a read-only flip in the citations panel', () => {
|
|
async function openPanel(user: User) {
|
|
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
|
await user.click(screen.getByRole('treeitem', { name: /Cat A/ }))
|
|
await user.keyboard('{ArrowRight}')
|
|
await user.click(screen.getByRole('treeitem', { name: /Node A/ }))
|
|
await user.click(
|
|
within(screen.getByRole('tabpanel', { name: 'Constats' })).getByRole(
|
|
'button',
|
|
{ name: '2 références' },
|
|
),
|
|
)
|
|
await waitFor(() =>
|
|
expect(document.activeElement).toBe(
|
|
screen.getByRole('heading', { name: 'Références citées' }),
|
|
),
|
|
)
|
|
const panel = screen.getByTestId('editor-citations-panel')
|
|
await user.click(within(panel).getByRole('button', { name: /^Denoix/ }))
|
|
await user.click(within(panel).getByRole('button', { name: 'Modifier' }))
|
|
return panel
|
|
}
|
|
|
|
it('shows the note in the panel tile, says it once, focuses the tile header', async () => {
|
|
const user = await mount()
|
|
await openPanel(user)
|
|
await typeBadUrlIn(user)
|
|
const watch = watchNotes()
|
|
cross(800)
|
|
const item = tile('ref-b', 'panel')
|
|
expect(within(item).getByText(`${NOTE} : Adresse web (URL)`)).toBeVisible()
|
|
await flush()
|
|
expect(watch.seen).toHaveLength(1)
|
|
expect(document.activeElement).toBe(header('ref-b', 'panel'))
|
|
expect(history()).toEqual({ undo: 0, redo: 0 })
|
|
watch.stop()
|
|
})
|
|
|
|
it('a forced close names every refused field and says it once', async () => {
|
|
const user = await mount()
|
|
await openPanel(user)
|
|
await user.clear(field('ref-b', 'citation', 'panel'))
|
|
await typeBadUrlIn(user)
|
|
const watch = watchNotes()
|
|
act(() => {
|
|
store.getState().commit({
|
|
op: 'remove',
|
|
key: {
|
|
kind: 'finding',
|
|
zone_id: 'zone-a',
|
|
path: ['fcat-a', 'fnode-a'],
|
|
},
|
|
})
|
|
})
|
|
await waitFor(() =>
|
|
expect(screen.queryByTestId('editor-citations-panel')).toBeNull(),
|
|
)
|
|
expect(watch.seen).toHaveLength(1)
|
|
expect(live().textContent).toContain(
|
|
`${NOTE} : Citation, Adresse web (URL)`,
|
|
)
|
|
watch.stop()
|
|
})
|
|
})
|
|
|
|
async function typeBadUrlIn(user: User) {
|
|
const url = field('ref-b', 'url', 'panel')
|
|
await user.clear(url)
|
|
await user.type(url, 'exemple.org')
|
|
}
|