/** Lot H fix H2: Ctrl+Z and Ctrl+Shift+Z are editor-wide, writable only, not in a dialog. */ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { act, cleanup, fireEvent, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { baseText, commitMove, 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()), getSheetVersion: vi.fn(), downloadSheetDrawing: vi.fn(), saveSheetDraft: vi.fn(), getOfficialSheetAdmin: vi.fn(), submitSheetReview: vi.fn(), })) beforeEach(() => serverHas(baseText(), 3)) afterEach(() => { cleanup() resetStore() setWidth(1024) }) const ready = () => screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' }) const cx = () => store.getState().previewState?.document.annotations[0].geometry const ctrlZ = (target: Element, shift = false) => fireEvent.keyDown(target, { key: 'z', ctrlKey: true, shiftKey: shift }) describe('editor-wide undo', () => { it('undoes and redoes from another tab', async () => { mountDraft() await ready() act(() => void commitMove(40)) await userEvent.setup().click(screen.getByRole('tab', { name: 'Général' })) ctrlZ(document.body) expect(cx()).toMatchObject({ cx: 50 }) ctrlZ(document.body, true) expect(cx()).toMatchObject({ cx: 40 }) }) it('does nothing in a text field', async () => { mountDraft() await ready() act(() => void commitMove(40)) await userEvent.setup().click(screen.getByRole('tab', { name: 'Général' })) ctrlZ(document.querySelector('input') as Element) expect(cx()).toMatchObject({ cx: 40 }) }) it('does nothing while a modal dialog is open', async () => { mountDraft() await ready() act(() => void commitMove(40)) const dialog = document.createElement('dialog') dialog.setAttribute('open', '') document.body.append(dialog) ctrlZ(document.body) expect(cx()).toMatchObject({ cx: 40 }) dialog.remove() }) it('is off in a read-only mode', async () => { mountDraft({ width: 800 }) await ready() store.getState().setCanEdit(true) act(() => void commitMove(40)) store.getState().setCanEdit(false) ctrlZ(document.body) expect(cx()).toMatchObject({ cx: 40 }) }) })