Files
faunosteo/apps/app/tests/unit/editor-overview.test.tsx
T
bchanot 0467885527 fix(editor): final review fixes (touch undo guard, focus, save line)
Important 1: a touch press fires pointer leave between up and click; only a
mouse leave (or a pointer cancel) forgets the refused field, so Annuler no
longer undoes an unrelated change. Important 2: the LAN spec reads the
revision before the attach, through shownRevision, which throws instead of NaN
(spec stays not run). Minor 1: refocusIfHidden refocuses a still shown element
the commit blurred (references search on a 1 024 px crossing). Minor 2:
discardChanges keeps lastSave (reload path still drops it). Minor 3: the
overview filter term lives in UiState / the read-only twin, not the card.
Minor 4: categories section drops its inner h3 and the unused
editor.general.categories keys. AC8: badge test.

Assertions rewritten or added (R-24):
- editor-tabs-general « Général no longer holds the categories editor »:
  queryByText of the deleted heading (now vacuous) -> queryByLabelText of the
  category name fields.
- editor-focus-request « Annotations: selects the annotation »: findAllByText
  /anno-b|Sélection/ -> the properties panel holds Centre x = 20 (anno-b) and
  owns the focused heading.
- editor-shell-layout panel walk: added after.at(-1) === 'side'.
- editor-recovery title « Annuler les modifications » -> « Abandonner ».
- new: touch/cancel/mouse press, references search crossing, discard keeps
  lastSave, filter kept, one categories heading, badge 1/2/0, shownRevision.
2026-11-09 12:19:32 +01:00

201 lines
6.8 KiB
TypeScript

/** V1-2.6 U1 Lot C: board 01's « Annotations de la vue » card, in edit and read-only. */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
cleanup,
render,
screen,
waitFor,
within,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { Annotation } from '@faunosteo/core-wasm/types'
import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab'
import { useLocaleStore } from '@/state/localeStore'
import { drawnText, insert, point } from '../helpers/editorFixtures'
import { openReady, resetStore } from '../helpers/editorHarness'
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(),
}))
const props = {
readOnly: false,
navigate: vi.fn(),
reloadSheet: vi.fn(),
section: 'drawing' as const,
visited: ['drawing'] as const,
onOpenSection: vi.fn(),
onBack: vi.fn(),
}
const named = (
id: string,
fr: string,
category: string,
view = 'view-main',
) => {
const base = point(id, 50, 50, 5, view).value as Annotation
return {
kind: 'annotation',
value: { ...base, category_id: category, name: { fr, en: fr } },
} as const
}
/** Two categories (b declared after a); the first annotation sits in b. */
function sheet(): string {
let text = insert(drawnText(), {
kind: 'category',
value: { id: 'cat-b', name: { fr: 'Os', en: 'Bone' }, color: '#aa0000' },
})
text = insert(text, named('a1', 'Épaule', 'cat-b'))
text = insert(text, named('a2', 'Genou', 'cat-a'))
return insert(text, {
kind: 'view',
value: {
kind: 'detail',
id: 'view-det',
name: { fr: 'Tête', en: 'Head' },
svg_asset_id: null,
view_box: { min_x: 10, min_y: 10, width: 80, height: 80 },
parent_view_id: 'view-main',
},
})
}
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
afterEach(() => {
cleanup()
resetStore()
})
async function mount(readOnly = false) {
await openReady({ text: sheet() })
render(<AnnotationsTab {...props} readOnly={readOnly} />)
await screen.findByRole('list', { name: 'Annotations de la vue' })
}
const overview = () =>
screen.getByRole('region', { name: 'Aperçu de la fiche' })
const card = () =>
within(overview()).getByRole('list', { name: 'Annotations de la vue' })
/** The visible body: the read-only twin hides the kept edit one. */
const body = () =>
screen
.getAllByTestId('editor-tab-annotations')
.find((el) => !el.closest('[hidden]'))!
const canvas = (readOnly: boolean) =>
within(body()).getByTestId(
readOnly ? 'catalogue-canvas' : 'annotation-canvas',
)
const rows = () => within(card()).getAllByRole('button')
const chip = (name: string) =>
within(screen.getByRole('group', { name: 'Vues' })).getByRole('button', {
name,
})
describe.each([
['edit', false],
['read-only', true],
])('the annotation card (%s)', (_mode, readOnly) => {
it('groups the rows by category in document order, with a count per group', async () => {
await mount(readOnly)
const groups = within(card()).getAllByRole('list')
expect(groups).toHaveLength(2)
const [first, second] = groups
expect(first).toHaveAccessibleName('N 1')
expect(second).toHaveAccessibleName('Os 1')
expect(within(first).getByRole('button')).toHaveTextContent('Genou')
expect(within(second).getByRole('button')).toHaveTextContent('Épaule')
})
it('filters on the displayed name, ignoring case and accents', async () => {
await mount(readOnly)
const user = userEvent.setup()
const field = within(overview()).getByRole('searchbox', {
name: 'Filtrer par nom',
})
await user.type(field, 'EPAULE')
expect(rows()).toHaveLength(1)
expect(rows()[0]).toHaveTextContent('Épaule')
await user.type(field, 'zzz')
expect(
within(overview()).getByText('Aucune annotation ne correspond.'),
).toBeInTheDocument()
})
it('a row selects the annotation; « Toutes les annotations » returns with focus on its row', async () => {
await mount(readOnly)
const user = userEvent.setup()
await user.click(rows()[1])
expect(
await within(body()).findByTestId('annotation-properties'),
).toBeVisible()
await user.click(
screen.getByRole('button', { name: 'Toutes les annotations' }),
)
expect(overview()).toBeVisible()
await waitFor(() => expect(document.activeElement).toBe(rows()[1]))
})
it('keeps the « Filtrer par nom » term across a selection and back', async () => {
await mount(readOnly)
const user = userEvent.setup()
const field = () =>
within(overview()).getByRole('searchbox', { name: 'Filtrer par nom' })
await user.type(field(), 'epaule')
await user.click(rows()[0])
await user.click(
screen.getByRole('button', { name: 'Toutes les annotations' }),
)
expect(field()).toHaveValue('epaule')
expect(rows()).toHaveLength(1)
expect(rows()[0]).toHaveTextContent('Épaule')
})
it('a view row switches the canvas view', async () => {
await mount(readOnly)
const user = userEvent.setup()
const name = (view: string) => (readOnly ? `Dessin : ${view}` : view)
expect(canvas(readOnly)).not.toHaveAccessibleName(name('Tête'))
expect(chip('Tête')).toHaveAttribute('aria-pressed', 'false')
await user.click(within(overview()).getByRole('button', { name: /^Tête/ }))
expect(
within(overview()).getByRole('button', { name: /^Tête/ }),
).toHaveAttribute('aria-current', 'true')
expect(chip('Tête')).toHaveAttribute('aria-pressed', 'true')
expect(canvas(readOnly)).toHaveAccessibleName(name('Tête'))
expect(
within(overview()).getByText('Aucune annotation dans cette vue.'),
).toBeInTheDocument()
})
})
describe('« Sans catégorie »', () => {
it('a group appears only for an annotation whose category is not in the document', async () => {
let text = insert(drawnText(), named('stray', 'Perdue', 'ghost'))
text = insert(text, named('a2', 'Genou', 'cat-a'))
await openReady({ text })
render(<AnnotationsTab {...props} />)
await screen.findByRole('list', { name: 'Annotations de la vue' })
const groups = within(card()).getAllByRole('list')
expect(groups).toHaveLength(2)
expect(groups[1]).toHaveAccessibleName('Sans catégorie 1')
expect(within(groups[1]).getByRole('button')).toHaveTextContent('Perdue')
})
})
describe('the side column', () => {
it('sits beside the canvas from lg (1 024 px), not xl', async () => {
await mount()
const grid = overview().closest('.grid')
expect(grid?.className).toContain('lg:grid-cols-[minmax(0,1fr)_22rem]')
expect(grid?.className).not.toContain('xl:')
})
})