/** 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()), 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() 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() 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:') }) })