/** Lot H: the Annotations tab (creation, corrected position, gestures, read-only, repair). */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { act, cleanup, fireEvent, 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 { ASSET, drawnText, insert, point, svgBytes, } from '../helpers/editorFixtures' import { USER, VERSION, commitMove, openReady, resetStore, sb, store, versionAnswer, } 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, cx: number, cy: number, r: number) => point(id, cx, cy, r) const annotations = () => store.getState().previewState!.document.annotations const geometryOf = (id: string) => annotations().find((a: Annotation) => a.id === id)?.geometry /** Screen = drawing: the overlay's inverse CTM is the identity in the page under test. */ function stubCtm(): void { const identity = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 } Object.defineProperty(window.SVGElement.prototype, 'getScreenCTM', { configurable: true, value: () => ({ ...identity, inverse: () => identity }), }) } beforeEach(() => { useLocaleStore.setState({ locale: 'fr' }) stubCtm() }) afterEach(() => { cleanup() resetStore() }) const text = () => insert(drawnText(), named('anno-a', 50, 50, 5)) async function mount(options: { text?: string; readOnly?: boolean } = {}) { await openReady({ text: options.text ?? text() }) render() const drawing = options.readOnly ? 'catalogue-canvas-drawing' : 'annotation-canvas-drawing' await waitFor(() => expect(screen.getByTestId(drawing).querySelector('svg')).not.toBeNull(), ) } async function selectFromList(name: RegExp | string = /N/) { const list = await screen.findByRole('list', { name: 'Annotations de la vue', }) await userEvent.setup().click(within(list).getAllByRole('button')[0]) void name return screen.findByTestId('annotation-properties') } describe('selection and the list', () => { it('lists the view annotations, selects one and shows its exact stored numbers', async () => { await mount({ text: insert(drawnText(), named('anno-a', 743.0451049804688, 50, 5)), }) const panel = await selectFromList() expect(within(panel).getByLabelText('Centre x')).toHaveValue( '743.0451049804688', ) expect(screen.getByTestId('annotation-selection-tag')).toHaveTextContent( 'Sélection : point', ) }) }) describe('a numeric edit', () => { it('commits only when the typed number differs from the stored one', async () => { await mount() const panel = await selectFromList() const user = userEvent.setup() const version = store.getState().handleVersion const cx = within(panel).getByLabelText('Centre x') await user.click(cx) await user.keyboard('{Enter}') expect(store.getState().handleVersion).toBe(version) await user.clear(cx) await user.type(cx, '60{Enter}') expect(geometryOf('anno-a')).toEqual({ type: 'circle', cx: 60, cy: 50, r: 5, }) expect(store.getState().handleVersion).toBe(version + 1) }) it('keeps a refused value as « non appliqué » with the document unchanged, then applies a coherent set', async () => { await mount({ text: insert(drawnText(), named('anno-a', 150, 150, 5)) }) const panel = await selectFromList() const user = userEvent.setup() const before = store.getState().documentText expect( within(panel).getByTestId('annotation-geometry-issues'), ).toBeInTheDocument() const cx = within(panel).getByLabelText('Centre x') await user.clear(cx) await user.type(cx, '50{Enter}') expect(store.getState().documentText).toBe(before) expect(within(panel).getByText('non appliqué')).toBeInTheDocument() expect( within(panel).getByText(/Valeurs non appliquées/), ).toBeInTheDocument() expect( within(panel).getByRole('button', { name: 'Revenir aux valeurs enregistrées', }), ).toBeInTheDocument() expect(within(panel).getByLabelText('Centre x')).toHaveValue('50') const cy = within(panel).getByLabelText('Centre y') await user.clear(cy) await user.type(cy, '50{Enter}') expect(geometryOf('anno-a')).toEqual({ type: 'circle', cx: 50, cy: 50, r: 5, }) expect(screen.queryByTestId('annotation-geometry-issues')).toBeNull() }) it('reverts the group to the saved values', async () => { await mount({ text: insert(drawnText(), named('anno-a', 150, 150, 5)) }) const panel = await selectFromList() const user = userEvent.setup() const cx = within(panel).getByLabelText('Centre x') await user.clear(cx) await user.type(cx, '50{Enter}') await user.click( within(panel).getByRole('button', { name: 'Revenir aux valeurs enregistrées', }), ) expect(within(panel).getByLabelText('Centre x')).toHaveValue('150') }) }) async function fillCreation(user: ReturnType) { const panel = await screen.findByTestId('annotation-creation') await user.type(within(panel).getByLabelText('Nom (FR)'), 'Nuque') await user.type(within(panel).getByLabelText('Nom (EN)'), 'Poll') await user.selectOptions( within(panel).getByLabelText('Latéralité'), 'unpaired', ) return panel } describe('the keyboard creation path', () => { it('holds a point at the frame centre, asks the required fields, then inserts once', async () => { await mount({ text: drawnText() }) const user = userEvent.setup() await waitFor(() => expect( screen.getByRole('button', { name: 'Ajouter un point' }), ).toBeEnabled(), ) await user.click(screen.getByRole('button', { name: 'Ajouter un point' })) const panel = await fillCreation(user) expect(within(panel).getByLabelText('Centre x')).toHaveValue('50') expect(within(panel).getByLabelText('Latéralité')).toHaveValue('unpaired') expect(annotations()).toHaveLength(0) await user.click(within(panel).getByRole('button', { name: 'Créer' })) expect(annotations()).toHaveLength(1) expect(annotations()[0]).toMatchObject({ kind: 'point', name: { fr: 'Nuque', en: 'Poll' }, laterality: 'unpaired', geometry: { type: 'circle', cx: 50, cy: 50 }, }) expect( await screen.findByTestId('annotation-properties'), ).toBeInTheDocument() }) it('refuses a creation without laterality and creates nothing', async () => { await mount({ text: drawnText() }) const user = userEvent.setup() await waitFor(() => expect( screen.getByRole('button', { name: 'Ajouter une zone' }), ).toBeEnabled(), ) await user.click(screen.getByRole('button', { name: 'Ajouter une zone' })) const panel = await screen.findByTestId('annotation-creation') await user.type(within(panel).getByLabelText('Nom (FR)'), 'A') await user.type(within(panel).getByLabelText('Nom (EN)'), 'B') await user.click(within(panel).getByRole('button', { name: 'Créer' })) expect(within(panel).getByRole('alert')).toHaveTextContent( 'Choisissez la catégorie et la latéralité', ) expect(annotations()).toHaveLength(0) }) it('applies the geometry group to a pending zone, the core deciding at « Créer »', async () => { await mount({ text: drawnText() }) const user = userEvent.setup() await waitFor(() => expect( screen.getByRole('button', { name: 'Ajouter une zone' }), ).toBeEnabled(), ) await user.click(screen.getByRole('button', { name: 'Ajouter une zone' })) const panel = await fillCreation(user) const x = within(panel).getByLabelText('x du sommet 1') await user.clear(x) await user.type(x, '500{Enter}') expect(within(panel).getByLabelText('x du sommet 1')).toHaveValue('500') await user.click(within(panel).getByRole('button', { name: 'Créer' })) expect(annotations()).toHaveLength(0) expect(within(panel).getByRole('alert')).toBeInTheDocument() expect(within(panel).getByLabelText('x du sommet 1')).toHaveValue('500') }) }) describe('the point tool and the corrected position (C26)', () => { async function clickCorner() { const user = userEvent.setup() await user.click(screen.getByRole('button', { name: 'Point' })) fireEvent.click(screen.getByTestId('annotation-canvas'), { clientX: 0, clientY: 0, }) return user } it('proposes C with the click marked apart, inserts it only on confirmation', async () => { await mount({ text: drawnText() }) await waitFor(() => expect(screen.getByRole('button', { name: 'Point' })).toBeEnabled(), ) const user = await clickCorner() const notice = await screen.findByTestId('correction-notice') expect(notice).toHaveTextContent( 'Position corrigée proposée : x = 15, y = 15.', ) expect(screen.getByTestId('overlay-click-cross')).toBeInTheDocument() const panel = await fillCreation(user) expect(annotations()).toHaveLength(0) await user.click( within(notice).getByRole('button', { name: 'Utiliser cette position' }), ) expect(geometryOf(annotations()[0].id)).toEqual({ type: 'circle', cx: 15, cy: 15, r: 15, }) expect(panel).not.toBeInTheDocument() expect(screen.queryByTestId('overlay-click-cross')).toBeNull() const props = await screen.findByTestId('annotation-properties') expect(within(props).getByLabelText('Centre x')).toHaveValue('15') }) it('keeps the pending point at the click when the correction is cancelled', async () => { await mount({ text: drawnText() }) await waitFor(() => expect(screen.getByRole('button', { name: 'Point' })).toBeEnabled(), ) const user = await clickCorner() const notice = await screen.findByTestId('correction-notice') await user.click(within(notice).getByRole('button', { name: 'Annuler' })) expect(screen.queryByTestId('correction-notice')).toBeNull() expect(screen.queryByTestId('overlay-click-cross')).toBeNull() const panel = screen.getByTestId('annotation-creation') expect(within(panel).getByLabelText('Centre x')).toHaveValue('0') expect(annotations()).toHaveLength(0) }) it('offers the correction when the core refuses a pending point', async () => { await mount({ text: drawnText() }) const user = userEvent.setup() await waitFor(() => expect( screen.getByRole('button', { name: 'Ajouter un point' }), ).toBeEnabled(), ) await user.click(screen.getByRole('button', { name: 'Ajouter un point' })) const panel = await fillCreation(user) const cx = within(panel).getByLabelText('Centre x') await user.clear(cx) await user.type(cx, '98{Enter}') await user.click(within(panel).getByRole('button', { name: 'Créer' })) expect(await screen.findByTestId('correction-notice')).toHaveTextContent( 'x = 85', ) expect(annotations()).toHaveLength(0) }) it('says to click inside the drawing, and proposes nothing, outside the frame', async () => { await mount({ text: drawnText() }) await waitFor(() => expect(screen.getByRole('button', { name: 'Point' })).toBeEnabled(), ) const user = userEvent.setup() await user.click(screen.getByRole('button', { name: 'Point' })) fireEvent.click(screen.getByTestId('annotation-canvas'), { clientX: -5, clientY: 50, }) expect( await screen.findByText("Cliquez à l'intérieur du dessin."), ).toBeInTheDocument() expect(screen.queryByTestId('annotation-creation')).toBeNull() }) }) function marker(id: string): Element { const found = screen .getByTestId('annotation-canvas-drawing') .querySelector(`[data-anno-id="${id}"]`) if (!found) throw new Error(`no marker ${id}`) return found } describe('select and move gestures (C34)', () => { const down = (el: Element, x: number, y: number) => fireEvent.pointerDown(el, { clientX: x, clientY: y, button: 0, pointerId: 1, }) const moveTo = (el: Element, x: number, y: number) => fireEvent.pointerMove(el, { clientX: x, clientY: y, pointerId: 1 }) const up = (el: Element, x: number, y: number) => fireEvent.pointerUp(el, { clientX: x, clientY: y, pointerId: 1 }) async function selected() { await mount() await selectFromList() return marker('anno-a') } it('treats a press and release under 4 px as a click: nothing is committed', async () => { const hit = await selected() const version = store.getState().handleVersion down(hit, 50, 50) moveTo(hit, 52, 51) up(hit, 52, 51) expect(store.getState().handleVersion).toBe(version) expect(geometryOf('anno-a')).toEqual({ type: 'circle', cx: 50, cy: 50, r: 5, }) }) it('moves the selected circle as a block after 4 px, in one update rounded to 2 decimals', async () => { const hit = await selected() const version = store.getState().handleVersion down(hit, 50, 50) moveTo(hit, 58, 50) expect(screen.getByTestId('annotation-overlay')).toBeInTheDocument() up(hit, 60.004, 40) expect(geometryOf('anno-a')).toEqual({ type: 'circle', cx: 60, cy: 40, r: 5, }) expect(store.getState().handleVersion).toBe(version + 1) }) it('rolls a drop half out of the frame back, with the core message', async () => { const hit = await selected() const before = store.getState().documentText down(hit, 50, 50) moveTo(hit, 90, 50) up(hit, 98, 50) expect(store.getState().documentText).toBe(before) expect(await screen.findByRole('alert')).toBeInTheDocument() }) it('drops the drag on pointer cancel with no command', async () => { const hit = await selected() const version = store.getState().handleVersion down(hit, 50, 50) moveTo(hit, 70, 50) fireEvent.pointerCancel(hit, { pointerId: 1 }) up(hit, 70, 50) expect(store.getState().handleVersion).toBe(version) }) it('does not move an annotation that is not selected', async () => { await mount() const hit = marker('anno-a') const version = store.getState().handleVersion down(hit, 50, 50) moveTo(hit, 70, 50) up(hit, 70, 50) expect(store.getState().handleVersion).toBe(version) }) }) describe('an unmeasured view (C32)', () => { async function mountMissing() { const drawing = svgBytes() sb.getSheetVersion.mockResolvedValue({ ...versionAnswer(text(), 3), drawings: [], } as never) sb.downloadSheetDrawing.mockResolvedValue(drawing.bytes) store.getState().open(VERSION, USER, { canEdit: true }) await waitFor(() => expect(store.getState().status).toBe('ready')) render() void ASSET } it('disables every tool and add button, shows the hint, keeps the annotation selectable read-only', async () => { vi.spyOn(console, 'warn').mockImplementation(() => undefined) await mountMissing() for (const name of [ 'Point', 'Zone', 'Accès à une vue détaillée', 'Ajouter un point', 'Ajouter une zone', 'Ajouter un accès', ]) { expect(screen.getByRole('button', { name })).toBeDisabled() } expect( screen.getAllByText(/Ce dessin n'est pas mesurable/).length, ).toBeGreaterThan(0) const panel = await selectFromList() expect( within(panel).queryByRole('textbox', { name: 'Centre x' }), ).toBeNull() expect( within(panel).getByText('Centre x', { exact: false }), ).toBeInTheDocument() expect( within(panel).getByText(/Ce dessin n'est pas mesurable/), ).toBeInTheDocument() }) }) describe('tools and categories', () => { it('disables the drawing tools until an annotation category exists', async () => { const bare = JSON.parse(drawnText()) bare.categories = [] await mount({ text: JSON.stringify(bare) }) expect(screen.getByRole('button', { name: 'Point' })).toBeDisabled() expect( screen.getByText( "Créez d'abord une catégorie d'annotation (Annotations, section Catégories).", ), ).toBeInTheDocument() }) it('enables the link tool only when the view has a direct detail child', async () => { await mount({ text: drawnText() }) expect( screen.getByRole('button', { name: 'Accès à une vue détaillée' }), ).toBeDisabled() expect( screen.getByRole('button', { name: 'Ajouter un accès' }), ).toBeDisabled() }) it('keeps no undo or redo in the tool column (they sit in the header)', async () => { await mount() const tools = screen.getByRole('toolbar', { name: 'Outils' }) expect( within(tools).queryByRole('button', { name: /Annuler|Rétablir/ }), ).toBeNull() }) }) describe('read-only', () => { it('browses the annotations with no tool, no add button and the fields as text', async () => { await mount({ readOnly: true }) expect(screen.queryByRole('button', { name: 'Point' })).toBeNull() expect( screen.queryByRole('button', { name: 'Ajouter un point' }), ).toBeNull() const panel = await selectFromList() expect(within(panel).queryByRole('textbox')).toBeNull() expect( within(panel).queryByRole('button', { name: 'Supprimer' }), ).toBeNull() }) }) const triangle = [ [20, 20], [80, 20], [50, 80], ] const zoneAnnotation = (geometry: object): Annotation => ({ ...(point('zone-a', 0, 0, 1).value as Annotation), kind: 'zone', geometry, }) as Annotation describe('tracing a zone or a link', () => { const clickAt = (x: number, y: number) => fireEvent.click(screen.getByTestId('annotation-canvas'), { clientX: x, clientY: y, }) it('closes on Enter, holds the polygon pending, inserts it on « Créer »', async () => { await mount({ text: drawnText() }) const user = userEvent.setup() await waitFor(() => expect(screen.getByRole('button', { name: 'Zone' })).toBeEnabled(), ) await user.click(screen.getByRole('button', { name: 'Zone' })) triangle.forEach(([x, y]) => clickAt(x, y)) expect( screen.getByRole('button', { name: 'Fermer le tracé' }), ).toBeEnabled() fireEvent.keyDown(screen.getByTestId('annotation-canvas'), { key: 'Enter' }) const panel = await fillCreation(user) expect(within(panel).getByLabelText('x du sommet 3')).toHaveValue('50') await user.click(within(panel).getByRole('button', { name: 'Créer' })) expect(annotations()[0]).toMatchObject({ kind: 'zone', geometry: { type: 'polygon', points: triangle }, }) }) it('drops the tracing on Escape with nothing created', async () => { await mount({ text: drawnText() }) await waitFor(() => expect(screen.getByRole('button', { name: 'Zone' })).toBeEnabled(), ) await userEvent.setup().click(screen.getByRole('button', { name: 'Zone' })) clickAt(20, 20) expect(screen.getByText('1 sommet posé.')).toBeInTheDocument() fireEvent.keyDown(screen.getByTestId('annotation-canvas'), { key: 'Escape', }) expect(screen.queryByText('1 sommet posé.')).toBeNull() expect(annotations()).toHaveLength(0) }) it('closes on a click at the first vertex once three are placed', async () => { await mount({ text: drawnText() }) await waitFor(() => expect(screen.getByRole('button', { name: 'Zone' })).toBeEnabled(), ) await userEvent.setup().click(screen.getByRole('button', { name: 'Zone' })) triangle.forEach(([x, y]) => clickAt(x, y)) clickAt(20.5, 20) expect(await screen.findByTestId('annotation-creation')).toBeInTheDocument() }) it('links only to a detail view directly under this one', async () => { const withView = insert(drawnText(), { 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', }, }) await mount({ text: withView }) const user = userEvent.setup() await waitFor(() => expect( screen.getByRole('button', { name: 'Accès à une vue détaillée' }), ).toBeEnabled(), ) await user.click(screen.getByRole('button', { name: 'Ajouter un accès' })) const panel = await fillCreation(user) const opens = within(panel).getByLabelText('Vue ouverte') expect( within(opens) .getAllByRole('option') .map((o) => o.textContent), ).toEqual(['Choisir…', 'Tête']) await user.selectOptions(opens, 'view-det') await user.click(within(panel).getByRole('button', { name: 'Créer' })) expect(annotations()[0]).toMatchObject({ kind: 'detail_trigger', opens_view_id: 'view-det', }) }) }) describe('polygons and paths', () => { const zoneText = (geometry: object) => insert(drawnText(), { kind: 'annotation', value: zoneAnnotation(geometry) }) it('adds and removes a vertex as part of the whole group', async () => { await mount({ text: zoneText({ type: 'polygon', points: triangle }) }) const panel = await selectFromList() const user = userEvent.setup() await user.click(within(panel).getByLabelText('x du sommet 1')) await user.click( within(panel).getByRole('button', { name: 'Ajouter un sommet' }), ) expect(geometryOf('zone-a')).toMatchObject({ points: [ [20, 20], [50, 20], [80, 20], [50, 80], ], }) await user.click(within(panel).getByRole('button', { name: 'Retirer' })) expect(geometryOf('zone-a')).toMatchObject({ points: triangle.slice(0, 3).map((p, i) => (i === 0 ? [50, 20] : p)), }) }) it('never offers to remove a vertex below three', async () => { await mount({ text: zoneText({ type: 'polygon', points: triangle }) }) const panel = await selectFromList() expect( within(panel).getByRole('button', { name: 'Retirer' }), ).toBeDisabled() }) it('moves a selected polygon as a block', async () => { await mount({ text: zoneText({ type: 'polygon', points: triangle }) }) await selectFromList() const hit = screen .getByTestId('annotation-canvas-drawing') .querySelector('[data-anno-id="zone-a"]')! fireEvent.pointerDown(hit, { clientX: 50, clientY: 40, button: 0, pointerId: 1, }) fireEvent.pointerMove(hit, { clientX: 55, clientY: 40, pointerId: 1 }) fireEvent.pointerUp(hit, { clientX: 60, clientY: 45, pointerId: 1 }) expect(geometryOf('zone-a')).toMatchObject({ points: [ [30, 25], [90, 25], [60, 85], ], }) }) it('replaces a path by a traced polygon and never shows its data', async () => { await mount({ text: zoneText({ type: 'path', d: 'M 20 20 L 80 20 L 50 80 Z' }), }) const panel = await selectFromList() expect(within(panel).queryByText(/M 20 20/)).toBeNull() await userEvent.setup().click( within(panel).getByRole('button', { name: 'Remplacer par un polygone', }), ) const canvas = screen.getByTestId('annotation-canvas') triangle.forEach(([x, y]) => fireEvent.click(canvas, { clientX: x, clientY: y }), ) fireEvent.keyDown(canvas, { key: 'Enter' }) await waitFor(() => expect(geometryOf('zone-a')).toEqual({ type: 'polygon', points: triangle, }), ) }) }) describe('keys while tracing (H3)', () => { const clickAt = (x: number, y: number) => fireEvent.click(screen.getByTestId('annotation-canvas'), { clientX: x, clientY: y, }) async function traced() { await mount({ text: drawnText() }) await waitFor(() => expect(screen.getByRole('button', { name: 'Zone' })).toBeEnabled(), ) const user = userEvent.setup() await user.click(screen.getByRole('button', { name: 'Zone' })) triangle.forEach(([x, y]) => clickAt(x, y)) return user } it('Enter on « Annuler le tracé » cancels the tracing instead of closing it', async () => { const user = await traced() screen.getByRole('button', { name: 'Annuler le tracé' }).focus() await user.keyboard('{Enter}') expect( screen.queryByRole('button', { name: 'Annuler le tracé' }), ).toBeNull() expect(screen.queryByTestId('annotation-creation')).toBeNull() }) it('Enter on a view chip or a list row never closes the tracing', async () => { await traced() const views = screen.getByRole('group', { name: 'Vues' }) fireEvent.keyDown(within(views).getAllByRole('button')[0], { key: 'Enter', }) expect(screen.queryByTestId('annotation-creation')).toBeNull() expect( screen.getByRole('button', { name: 'Annuler le tracé' }), ).toBeInTheDocument() }) }) describe('a second click with the point tool (H6)', () => { it('moves the pending point and keeps what was typed', async () => { await mount({ text: drawnText() }) await waitFor(() => expect(screen.getByRole('button', { name: 'Point' })).toBeEnabled(), ) const user = userEvent.setup() await user.click(screen.getByRole('button', { name: 'Point' })) fireEvent.click(screen.getByTestId('annotation-canvas'), { clientX: 50, clientY: 50, }) const panel = await fillCreation(user) fireEvent.click(screen.getByTestId('annotation-canvas'), { clientX: 60, clientY: 40, }) await waitFor(() => expect(within(panel).getByLabelText('Centre x')).toHaveValue('60'), ) expect(within(panel).getByLabelText('Nom (FR)')).toHaveValue('Nuque') expect(within(panel).getByLabelText('Latéralité')).toHaveValue('unpaired') expect(screen.getByTestId('annotation-creation')).toBe(panel) }) }) describe('a typed radius (H5)', () => { it('shows the core issue, not a corrected position', async () => { const drawing = svgBytes('0 0 5000 5000') sb.getSheetVersion.mockResolvedValue({ ...versionAnswer(drawnText(), 3), drawings: [{ asset_id: ASSET, sha256: drawing.sha256 }], } as never) sb.downloadSheetDrawing.mockResolvedValue(drawing.bytes) store.getState().open(VERSION, USER, { canEdit: true }) await waitFor(() => expect(store.getState().status).toBe('ready')) render() await waitFor(() => expect( screen.getByRole('button', { name: 'Ajouter un point' }), ).toBeEnabled(), ) const user = userEvent.setup() await user.click(screen.getByRole('button', { name: 'Ajouter un point' })) const panel = await fillCreation(user) const r = within(panel).getByLabelText('Rayon') await user.clear(r) await user.type(r, '1{Enter}') await user.click(within(panel).getByRole('button', { name: 'Créer' })) expect(screen.queryByTestId('correction-notice')).toBeNull() expect(within(panel).getByRole('alert')).toBeInTheDocument() expect(annotations()).toHaveLength(0) }) }) describe('the frame follows the view (H7)', () => { it('holds the keyboard point at the centre of the view shown, after a switch', async () => { const withView = insert(drawnText(), { 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: 20, width: 40, height: 60 }, parent_view_id: 'view-main', }, }) await mount({ text: withView }) const user = userEvent.setup() await user.click(screen.getByRole('button', { name: 'Tête' })) await waitFor(() => expect( screen.getByRole('button', { name: 'Ajouter un point' }), ).toBeEnabled(), ) await user.click(screen.getByRole('button', { name: 'Ajouter un point' })) const panel = await screen.findByTestId('annotation-creation') expect(within(panel).getByLabelText('Centre x')).toHaveValue('30') expect(within(panel).getByLabelText('Centre y')).toHaveValue('50') }) }) describe('refusals through the tab (H8)', () => { const zoneText = (geometry: object) => insert(drawnText(), { kind: 'annotation', value: zoneAnnotation(geometry) }) it('a refused « Remplacer par un polygone » drops the tracing, shows the core message', async () => { const path = { type: 'path', d: 'M 20 20 L 80 20 L 50 80 Z' } await mount({ text: zoneText(path) }) const panel = await selectFromList() await userEvent.setup().click( within(panel).getByRole('button', { name: 'Remplacer par un polygone', }), ) const canvas = screen.getByTestId('annotation-canvas') ;[ [20, 20], [500, 20], [50, 80], ].forEach(([x, y]) => fireEvent.click(canvas, { clientX: x, clientY: y })) fireEvent.keyDown(canvas, { key: 'Enter' }) expect(await screen.findByRole('alert')).toBeInTheDocument() expect( screen.queryByRole('button', { name: 'Annuler le tracé' }), ).toBeNull() expect(geometryOf('zone-a')).toEqual(path) }) it('a refused vertex add and a refused remove stay « non appliqué », document unchanged', async () => { await mount({ text: zoneText({ type: 'polygon', points: [...triangle, [20, 60]] }), }) const panel = await selectFromList() const user = userEvent.setup() const before = store.getState().documentText const x = within(panel).getByLabelText('x du sommet 1') await user.clear(x) await user.type(x, '500{Enter}') await user.click( within(panel).getByRole('button', { name: 'Ajouter un sommet' }), ) expect(store.getState().documentText).toBe(before) expect(within(panel).getAllByLabelText(/^x du sommet/)).toHaveLength(5) expect( within(panel).getByText(/Valeurs non appliquées/), ).toBeInTheDocument() await user.click(within(panel).getByRole('button', { name: 'Retirer' })) expect(store.getState().documentText).toBe(before) expect( within(panel).getByText(/Valeurs non appliquées/), ).toBeInTheDocument() }) }) describe('the drag and the canvas follow the handle (H8)', () => { const down = (el: Element, x: number, y: number) => fireEvent.pointerDown(el, { clientX: x, clientY: y, button: 0, pointerId: 1, }) it('lostpointercapture drops the drag with no command', async () => { await mount() await selectFromList() const hit = marker('anno-a') const version = store.getState().handleVersion down(hit, 50, 50) fireEvent.pointerMove(hit, { clientX: 70, clientY: 50, pointerId: 1 }) fireEvent.lostPointerCapture(hit, { pointerId: 1 }) fireEvent.pointerUp(hit, { clientX: 70, clientY: 50, pointerId: 1 }) expect(store.getState().handleVersion).toBe(version) }) it('a switch to read-only mid-drag drops it', async () => { await openReady({ text: text() }) const view = render() await waitFor(() => expect( screen.getByTestId('annotation-canvas-drawing').querySelector('svg'), ).not.toBeNull(), ) await selectFromList() const hit = marker('anno-a') const version = store.getState().handleVersion down(hit, 50, 50) fireEvent.pointerMove(hit, { clientX: 70, clientY: 50, pointerId: 1 }) view.rerender() fireEvent.pointerUp(hit, { clientX: 70, clientY: 50, pointerId: 1 }) expect(store.getState().handleVersion).toBe(version) expect(geometryOf('anno-a')).toEqual({ type: 'circle', cx: 50, cy: 50, r: 5, }) }) it('rebuilds the drawing when the handle changes under it', async () => { await mount() const first = marker('anno-a') act(() => void commitMove(60)) await waitFor(() => expect(marker('anno-a')).not.toBe(first)) }) })