917 lines
32 KiB
TypeScript
917 lines
32 KiB
TypeScript
/** Lot E: the header « Défaire » / « Refaire », the save line's announcements, discard. */
|
|
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 DeleteDialog from '@/components/editor/DeleteDialog'
|
|
import { en } from '@/i18n/en'
|
|
import { fr } from '@/i18n/fr'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { emptyForm } from '@/editor/annotations/creation'
|
|
import { traceVertex } from '@/editor/annotations/creationWork'
|
|
import { useAnnotationWork, resetWork } from '@/editor/annotations/workStore'
|
|
import {
|
|
baseText,
|
|
commitMove,
|
|
resetStore,
|
|
sb,
|
|
serverHas,
|
|
storedCopy,
|
|
store,
|
|
} from '../helpers/editorHarness'
|
|
import {
|
|
adminAnswer,
|
|
grant,
|
|
mountDraft,
|
|
round,
|
|
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(() => serverHas(baseText(), 3))
|
|
afterEach(() => {
|
|
cleanup()
|
|
resetStore()
|
|
setWidth(1024)
|
|
})
|
|
|
|
const UNDO = 'Défaire la dernière modification'
|
|
const REDO = 'Refaire la dernière modification défaite'
|
|
const ready = () =>
|
|
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
|
|
const undoButton = () => screen.getByRole('button', { name: UNDO })
|
|
const redoButton = () => screen.getByRole('button', { name: REDO })
|
|
const disabled = (button: HTMLElement) =>
|
|
button.getAttribute('aria-disabled') === 'true'
|
|
const geometry = () =>
|
|
store.getState().previewState?.document.annotations[0].geometry
|
|
const nameFr = () => store.getState().previewState?.document.name.fr
|
|
|
|
/**
|
|
* A committed field (it writes on its blur): the sheet's French name on Général. U2 Task 6
|
|
* took the annotation's committed fields out of the drawing (one dialog save).
|
|
*/
|
|
async function selectAnnotation(user: ReturnType<typeof userEvent.setup>) {
|
|
await user.click(screen.getByRole('tab', { name: 'Général' }))
|
|
return (await screen.findByLabelText('Nom (FR)')) as HTMLInputElement
|
|
}
|
|
|
|
describe('the buttons', () => {
|
|
it('have the T15 names, titles, shortcuts and a visible label', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
const row = within(screen.getByTestId('history-buttons'))
|
|
expect(undoButton()).toHaveAttribute('title', `${UNDO} (Ctrl+Z ou ⌘Z)`)
|
|
expect(redoButton()).toHaveAttribute('title', `${REDO} (Ctrl+Maj+Z ou ⇧⌘Z)`)
|
|
expect(undoButton()).toHaveAttribute(
|
|
'aria-keyshortcuts',
|
|
'Control+Z Meta+Z',
|
|
)
|
|
expect(redoButton()).toHaveAttribute(
|
|
'aria-keyshortcuts',
|
|
'Control+Shift+Z Meta+Shift+Z',
|
|
)
|
|
expect(row.getAllByRole('button').map((b) => b.textContent)).toEqual([
|
|
'Défaire',
|
|
'Refaire',
|
|
])
|
|
expect(undoButton()).not.toHaveAttribute('disabled')
|
|
})
|
|
|
|
it('keep their English names and carry the English shortcut in the title', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
act(() => useLocaleStore.setState({ locale: 'en' }))
|
|
const undo = screen.getByRole('button', {
|
|
name: en['editor.history.undo_title'],
|
|
})
|
|
const redo = screen.getByRole('button', {
|
|
name: en['editor.history.redo_title'],
|
|
})
|
|
expect(undo).toHaveAttribute('title', 'Undo the last change (Ctrl+Z or ⌘Z)')
|
|
expect(redo).toHaveAttribute(
|
|
'title',
|
|
'Redo the last undone change (Ctrl+Shift+Z or ⇧⌘Z)',
|
|
)
|
|
})
|
|
|
|
it('French prose says « Défaire » while the dialog cancel stays « Annuler »', () => {
|
|
expect(fr['editor.views.drawing.use_parent_body']).toContain('(Défaire)')
|
|
expect(fr['editor.views.repair.body_one']).toContain('un seul Défaire')
|
|
expect(fr['editor.views.repair.body_other']).toContain('un seul Défaire')
|
|
expect(fr['editor.views.drawing.use_parent_cancel']).toBe('Annuler')
|
|
})
|
|
|
|
it('are enabled exactly when the store can step, and step', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
expect([disabled(undoButton()), disabled(redoButton())]).toEqual([
|
|
true,
|
|
true,
|
|
])
|
|
act(() => void commitMove(40))
|
|
expect([disabled(undoButton()), disabled(redoButton())]).toEqual([
|
|
false,
|
|
true,
|
|
])
|
|
await user.click(undoButton())
|
|
expect(geometry()).toMatchObject({ cx: 50 })
|
|
expect([disabled(undoButton()), disabled(redoButton())]).toEqual([
|
|
true,
|
|
false,
|
|
])
|
|
await user.click(redoButton())
|
|
expect(geometry()).toMatchObject({ cx: 40 })
|
|
})
|
|
|
|
it('stay focused when the last undo empties the stack', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await user.click(undoButton())
|
|
expect(disabled(undoButton())).toBe(true)
|
|
expect(undoButton()).toHaveFocus()
|
|
})
|
|
|
|
it('are disabled in every mode that cannot edit', async () => {
|
|
const modes: Array<[string, Parameters<typeof mountDraft>[0]]> = [
|
|
['narrow', { width: 800 }],
|
|
['rights', { grants: [grant('sheet_reviewer')], roles: [] }],
|
|
['frozen', { answer: adminAnswer({ rounds: [round('open')] }) }],
|
|
['historical', { answer: adminAnswer({ historical: true }) }],
|
|
['inactive', { answer: adminAnswer({ status: 'archived' }) }],
|
|
]
|
|
for (const [, options] of modes) {
|
|
serverHas(baseText(), 3)
|
|
const view = mountDraft(options)
|
|
await ready()
|
|
store.setState({ undo: [{ text: baseText(), bytes: 1 }] })
|
|
expect(disabled(undoButton())).toBe(true)
|
|
await userEvent.setup().click(undoButton())
|
|
expect(store.getState().undo).toHaveLength(1)
|
|
view.unmount()
|
|
cleanup()
|
|
resetStore()
|
|
}
|
|
})
|
|
|
|
it('are disabled while a recovery decision is pending', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
act(() => store.setState({ recovery: 'pending' }))
|
|
expect(disabled(undoButton())).toBe(true)
|
|
})
|
|
|
|
it('stay enabled while saving, in step with Ctrl+Z', async () => {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockReturnValue(new Promise(() => undefined) as never)
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
await waitFor(() => expect(store.getState().saving).toBe(true))
|
|
expect(disabled(undoButton())).toBe(false)
|
|
fireEvent.keyDown(document.body, { key: 'z', ctrlKey: true })
|
|
expect(geometry()).toMatchObject({ cx: 50 })
|
|
expect(store.getState().redo).toHaveLength(1)
|
|
await user.click(redoButton())
|
|
expect(geometry()).toMatchObject({ cx: 40 })
|
|
})
|
|
|
|
it('undo from Général and from Constats reverts a canvas change', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await user.click(screen.getByRole('tab', { name: 'Général' }))
|
|
await user.click(undoButton())
|
|
expect(geometry()).toMatchObject({ cx: 50 })
|
|
await user.click(redoButton())
|
|
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
|
await user.click(undoButton())
|
|
expect(geometry()).toMatchObject({ cx: 50 })
|
|
})
|
|
|
|
it('undo from Annotations reverts a Général change', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
const original = store.getState().previewState?.document.name?.fr
|
|
await user.click(screen.getByRole('tab', { name: 'Général' }))
|
|
const place = document.querySelector(
|
|
'[data-place="general"]',
|
|
) as HTMLElement
|
|
const field = within(place).getByLabelText('Nom (FR)')
|
|
await user.clear(field)
|
|
await user.type(field, 'Renommée')
|
|
await user.click(screen.getByRole('tab', { name: 'Annotations' }))
|
|
expect(store.getState().previewState?.document.name?.fr).toBe('Renommée')
|
|
await user.click(undoButton())
|
|
expect(store.getState().previewState?.document.name?.fr).toBe(original)
|
|
})
|
|
|
|
it('leave no undo or redo pair in the tool column', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
const tools = screen.getByRole('toolbar', {
|
|
name: fr['editor.annotations.tools'],
|
|
})
|
|
expect(
|
|
within(tools).queryByRole('button', { name: /Défaire|Refaire/ }),
|
|
).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('typed text and the press', () => {
|
|
it('undo reverts exactly the typed text, with and without earlier history', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
const field = await selectAnnotation(user)
|
|
const original = nameFr()
|
|
await user.clear(field)
|
|
await user.type(field, 'Nouveau')
|
|
await user.pointer({ keys: '[MouseLeft]', target: undoButton() })
|
|
expect(nameFr()).toBe(original)
|
|
expect(store.getState().redo).toHaveLength(1)
|
|
act(() => void commitMove(40))
|
|
await user.clear(field)
|
|
await user.type(field, 'Encore')
|
|
await user.pointer({ keys: '[MouseLeft]', target: undoButton() })
|
|
expect(nameFr()).toBe(original)
|
|
expect(geometry()).toMatchObject({ cx: 40 })
|
|
})
|
|
|
|
it('the pointer press blurs the focused field', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
const field = await selectAnnotation(user)
|
|
await user.clear(field)
|
|
await user.type(field, 'Saisie')
|
|
fireEvent.pointerDown(undoButton())
|
|
expect(field).not.toHaveFocus()
|
|
expect(nameFr()).toBe('Saisie')
|
|
})
|
|
|
|
it('a refused blur commit skips the undo and returns focus to the field', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
const field = await selectAnnotation(user)
|
|
await user.clear(field)
|
|
await user.pointer({ keys: '[MouseLeft]', target: undoButton() })
|
|
expect(field).toHaveAttribute('aria-invalid', 'true')
|
|
expect(field).toHaveFocus()
|
|
expect(geometry()).toMatchObject({ cx: 40 })
|
|
})
|
|
|
|
async function refusedField() {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
const field = await selectAnnotation(user)
|
|
await user.clear(field)
|
|
return field
|
|
}
|
|
|
|
/** A press as a touch sends it: down, up, the leave it fires, then the click. */
|
|
function touchPress() {
|
|
const init = { pointerType: 'touch' }
|
|
fireEvent.pointerDown(undoButton(), init)
|
|
fireEvent.pointerUp(undoButton(), init)
|
|
fireEvent.pointerLeave(undoButton(), init)
|
|
fireEvent.click(undoButton())
|
|
}
|
|
|
|
it('a touch press keeps the refused field across pointer leave and click', async () => {
|
|
const field = await refusedField()
|
|
touchPress()
|
|
expect(field).toHaveAttribute('aria-invalid', 'true')
|
|
expect(field).toHaveFocus()
|
|
expect(geometry()).toMatchObject({ cx: 40 })
|
|
expect(store.getState().undo).toHaveLength(1)
|
|
})
|
|
|
|
it('a touch press cancelled by the browser forgets the field', async () => {
|
|
const field = await refusedField()
|
|
fireEvent.pointerDown(undoButton(), { pointerType: 'touch' })
|
|
fireEvent.pointerCancel(undoButton(), { pointerType: 'touch' })
|
|
fireEvent.click(undoButton())
|
|
expect(field).not.toHaveFocus()
|
|
expect(geometry()).toMatchObject({ cx: 50 })
|
|
})
|
|
|
|
it('a mouse leaving the button forgets the field', async () => {
|
|
const field = await refusedField()
|
|
fireEvent.pointerDown(undoButton(), { pointerType: 'mouse' })
|
|
fireEvent.pointerLeave(undoButton(), { pointerType: 'mouse' })
|
|
fireEvent.click(undoButton())
|
|
expect(field).not.toHaveFocus()
|
|
expect(geometry()).toMatchObject({ cx: 50 })
|
|
})
|
|
|
|
it('a keyboard activation after a refused blur commit skips the undo too', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
const field = await selectAnnotation(user)
|
|
await user.clear(field)
|
|
field.blur()
|
|
act(() => undoButton().focus())
|
|
fireEvent.focus(undoButton(), { relatedTarget: field })
|
|
await user.keyboard('{Enter}')
|
|
expect(field).toHaveAttribute('aria-invalid', 'true')
|
|
expect(field).toHaveFocus()
|
|
expect(geometry()).toMatchObject({ cx: 40 })
|
|
expect(store.getState().undo).toHaveLength(1)
|
|
})
|
|
|
|
it('a field left through « Refaire » to « Défaire » still skips the undo', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
const field = await selectAnnotation(user)
|
|
await user.clear(field)
|
|
field.blur()
|
|
const redo = screen.getByRole('button', {
|
|
name: REDO,
|
|
})
|
|
act(() => redo.focus())
|
|
fireEvent.focus(redo, { relatedTarget: field })
|
|
act(() => undoButton().focus())
|
|
fireEvent.focus(undoButton(), { relatedTarget: redo })
|
|
await user.keyboard('{Enter}')
|
|
expect(field).toHaveFocus()
|
|
expect(store.getState().undo).toHaveLength(1)
|
|
})
|
|
|
|
it('Ctrl+Z is ignored while a DeleteDialog is open', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
render(
|
|
<DeleteDialog
|
|
target={{ key: { kind: 'annotation', id: 'anno-a' }, label: 'N' }}
|
|
onClose={vi.fn()}
|
|
/>,
|
|
)
|
|
await screen.findByRole('dialog')
|
|
fireEvent.keyDown(document.body, { key: 'z', ctrlKey: true })
|
|
expect(geometry()).toMatchObject({ cx: 40 })
|
|
})
|
|
|
|
it('Ctrl+Z inside a field leaves the store untouched', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
const before = store.getState().documentText
|
|
fireEvent.keyDown(document.querySelector('input') as Element, {
|
|
key: 'z',
|
|
ctrlKey: true,
|
|
})
|
|
expect(store.getState().documentText).toBe(before)
|
|
})
|
|
})
|
|
|
|
describe('the save status', () => {
|
|
it('announces state transitions, never each count', async () => {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
|
mountDraft()
|
|
await ready()
|
|
const status = screen.getByTestId('save-status')
|
|
expect(status).toHaveTextContent(/^$/)
|
|
act(() => void commitMove(40))
|
|
expect(status).toHaveTextContent(/^Modifications non enregistrées$/)
|
|
act(() => void commitMove(41))
|
|
expect(status).toHaveTextContent(/^Modifications non enregistrées$/)
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(
|
|
/^2 modifications non enregistrées$/,
|
|
)
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
await waitFor(() =>
|
|
expect(status).toHaveTextContent(/^Enregistré · révision 4$/),
|
|
)
|
|
})
|
|
|
|
it('shows the server time once read, « Enregistré · révision n » otherwise', async () => {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
sb.getOfficialSheetAdmin.mockResolvedValue(
|
|
adminAnswer({
|
|
draft_version: { version_id: 'ver-1', version_number: 4, revision: 4 },
|
|
draft_last_save: {
|
|
revision: 4,
|
|
saved_at: '2026-11-04T13:32:00Z',
|
|
saved_by: null,
|
|
},
|
|
}) as never,
|
|
)
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
await waitFor(() =>
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(
|
|
/^Enregistré à 14:32 · révision 4$/,
|
|
),
|
|
)
|
|
})
|
|
|
|
it('shows « Enregistré · révision n » when the read fails, editor still mounted', async () => {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
sb.getOfficialSheetAdmin.mockRejectedValue(new Error('offline'))
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
await waitFor(() =>
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(
|
|
/^Enregistré · révision 4$/,
|
|
),
|
|
)
|
|
expect(screen.getByRole('heading', { level: 1 })).toBeInTheDocument()
|
|
})
|
|
|
|
it('shows no saved line while the save is in flight', async () => {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockReturnValue(new Promise(() => undefined) as never)
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(
|
|
/^Enregistrement…$/,
|
|
)
|
|
})
|
|
|
|
it('reserves its widest form so the row does not move', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
const sizers = screen
|
|
.getByTestId('save-line')
|
|
.parentElement!.querySelectorAll('[aria-hidden]')
|
|
expect([...sizers].map((el) => el.textContent)).toEqual([
|
|
'Enregistré à 00:00 · révision 999',
|
|
'999 modifications non enregistrées',
|
|
])
|
|
})
|
|
})
|
|
|
|
describe('« Abandonner les modifications »', () => {
|
|
const open = async (user: ReturnType<typeof userEvent.setup>) => {
|
|
await user.click(
|
|
screen.getByRole('button', { name: 'Abandonner les modifications' }),
|
|
)
|
|
return within(await screen.findByRole('dialog'))
|
|
}
|
|
|
|
it('names the stored copy, and keeps changes and copy on cancel', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
const dialog = await open(user)
|
|
expect(
|
|
dialog.getByText(new RegExp(fr['editor.draft.discard_copy'])),
|
|
).toBeInTheDocument()
|
|
await user.click(dialog.getByRole('button', { name: 'Annuler' }))
|
|
expect(store.getState().unsaved).toBe(true)
|
|
expect(storedCopy()).not.toBeNull()
|
|
})
|
|
|
|
it('names a copy left by an earlier write after a quota refusal', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
expect(storedCopy()).not.toBeNull()
|
|
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
|
throw new DOMException('full', 'QuotaExceededError')
|
|
})
|
|
act(() => void commitMove(41))
|
|
await waitFor(() => expect(store.getState().copyIssue).toBe('quota'))
|
|
const dialog = await open(user)
|
|
expect(
|
|
dialog.getByText(new RegExp(fr['editor.draft.discard_copy'])),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
it("keeps the session's save in the save line, and the save line says it", async () => {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
|
sb.getOfficialSheetAdmin.mockRejectedValue(new Error('offline'))
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
const line = screen.getByTestId('save-line')
|
|
await waitFor(() =>
|
|
expect(line).toHaveTextContent(/^Enregistré · révision 4$/),
|
|
)
|
|
act(() => void commitMove(41))
|
|
expect(line).toHaveTextContent(/^1 modification non enregistrée$/)
|
|
const dialog = await open(user)
|
|
await user.click(
|
|
dialog.getByRole('button', { name: fr['editor.draft.discard_confirm'] }),
|
|
)
|
|
expect(store.getState().unsaved).toBe(false)
|
|
expect(geometry()).toMatchObject({ cx: 40 })
|
|
expect(line).toHaveTextContent(/^Enregistré · révision 4$/)
|
|
})
|
|
|
|
it('does not mention a copy when none is stored', async () => {
|
|
const user = userEvent.setup()
|
|
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
|
throw new DOMException('full', 'QuotaExceededError')
|
|
})
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
const dialog = await open(user)
|
|
expect(dialog.getByText(fr['editor.draft.discard_ask'])).toBeInTheDocument()
|
|
expect(
|
|
dialog.queryByText(new RegExp(fr['editor.draft.discard_copy'])),
|
|
).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('why a button is held (point 6)', () => {
|
|
const CREATION =
|
|
"Indisponible pendant une création : créez l'annotation ou abandonnez-la."
|
|
const REDO_CREATION =
|
|
'Refaire est suspendu pendant une création : abandonnez-la pour pouvoir refaire.'
|
|
const PENDING =
|
|
'Refaire est suspendu pendant la modification en cours : annulez-la avec Défaire pour pouvoir refaire.'
|
|
const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 }
|
|
afterEach(() => act(() => resetWork()))
|
|
const bubble = () => screen.getByTestId('history-hold-bubble')
|
|
const reason = (button: HTMLElement) => {
|
|
const id = button.getAttribute('aria-describedby')
|
|
return id ? document.getElementById(id)?.textContent : null
|
|
}
|
|
const noMessage = () =>
|
|
expect(screen.queryByTestId('history-hold-bubble')).toBeNull()
|
|
async function openAnnotations() {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
await user.click(screen.getByRole('tab', { name: 'Annotations' }))
|
|
await screen.findByTestId('gesture-line')
|
|
return user
|
|
}
|
|
// Something to undo and to redo: a reason shows only when its cause alone holds the button.
|
|
const stacks = () =>
|
|
act(() =>
|
|
store.setState({
|
|
undo: [{ text: baseText(), bytes: 1 }],
|
|
redo: [{ text: baseText(), bytes: 1 }],
|
|
}),
|
|
)
|
|
const create = () =>
|
|
act(() =>
|
|
traceVertex(
|
|
{
|
|
viewId: 'v',
|
|
frame: null,
|
|
origin: 'tool',
|
|
form: emptyForm({ categories: [] } as never),
|
|
},
|
|
'zone',
|
|
[1, 1],
|
|
),
|
|
)
|
|
// A slider change in progress: a size preview on the stored document (I-1).
|
|
const pend = () =>
|
|
act(() =>
|
|
useAnnotationWork.setState({
|
|
preview: {
|
|
annotationId: 'anno-a',
|
|
geometry: { ...STORED, r: 9 },
|
|
base: store.getState().documentText,
|
|
source: 'size',
|
|
},
|
|
}),
|
|
)
|
|
// A handle's blur commits its series (here: ends the preview), as the real one does.
|
|
function focusedHandle() {
|
|
const handle = document.createElement('button')
|
|
handle.addEventListener('blur', () =>
|
|
useAnnotationWork.setState({ preview: null }),
|
|
)
|
|
document.body.append(handle)
|
|
handle.focus()
|
|
return handle
|
|
}
|
|
|
|
it('a creation describes each button with its own reason and every press says why, once', async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
create()
|
|
expect(reason(undoButton())).toBe(CREATION)
|
|
expect(reason(redoButton())).toBe(REDO_CREATION)
|
|
expect(disabled(redoButton())).toBe(true)
|
|
await user.click(redoButton())
|
|
expect(bubble()).toHaveTextContent(REDO_CREATION)
|
|
const first = bubble()
|
|
redoButton().focus()
|
|
await user.keyboard('{Enter}')
|
|
expect(bubble()).toHaveTextContent(REDO_CREATION)
|
|
expect(bubble()).not.toBe(first)
|
|
await user.keyboard(' ')
|
|
expect(screen.getAllByTestId('history-hold-bubble')).toHaveLength(1)
|
|
undoButton().focus()
|
|
await user.keyboard('{Enter}')
|
|
expect(bubble()).toHaveTextContent(CREATION)
|
|
})
|
|
|
|
it('pending work holds redo only, with its own reason', async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
pend()
|
|
expect(reason(redoButton())).toBe(PENDING)
|
|
expect(reason(undoButton())).toBeNull()
|
|
await user.click(redoButton())
|
|
expect(bubble()).toHaveTextContent(PENDING)
|
|
})
|
|
|
|
it('a mouse press on a held « Refaire » leaves the focused pending work alone', async () => {
|
|
const user = await openAnnotations()
|
|
pend()
|
|
const handle = focusedHandle()
|
|
store.setState({ redo: [{ text: baseText(), bytes: 1 }] })
|
|
await user.pointer([
|
|
{ keys: '[MouseLeft>]', target: redoButton() },
|
|
{ keys: '[/MouseLeft]' },
|
|
])
|
|
expect(handle).toHaveFocus()
|
|
expect(useAnnotationWork.getState().preview).not.toBeNull()
|
|
expect(store.getState().redo).toHaveLength(1)
|
|
expect(bubble()).toHaveTextContent(PENDING)
|
|
handle.remove()
|
|
})
|
|
|
|
for (const [kind, keys] of [
|
|
['mouse', '[MouseLeft]'],
|
|
['touch', '[TouchA]'],
|
|
] as const) {
|
|
it(`a ${kind} press on « Défaire » during pending work only cancels it: focus and redo stay`, async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
pend()
|
|
let committed = false
|
|
const handle = focusedHandle()
|
|
handle.addEventListener('blur', () => {
|
|
committed = true
|
|
})
|
|
await user.pointer({ keys, target: undoButton() })
|
|
expect(committed).toBe(false)
|
|
expect(handle).toHaveFocus()
|
|
expect(useAnnotationWork.getState().preview).toBeNull()
|
|
expect(store.getState().undo).toHaveLength(1)
|
|
expect(store.getState().redo).toHaveLength(1)
|
|
handle.remove()
|
|
})
|
|
}
|
|
|
|
it('with nothing to redo, pending work gives « Refaire » no reason and a press commits it', async () => {
|
|
const user = await openAnnotations()
|
|
pend()
|
|
expect(reason(redoButton())).toBeNull()
|
|
expect(disabled(redoButton())).toBe(true)
|
|
const handle = focusedHandle()
|
|
await user.click(redoButton())
|
|
expect(handle).not.toHaveFocus()
|
|
expect(useAnnotationWork.getState().preview).toBeNull()
|
|
noMessage()
|
|
handle.remove()
|
|
})
|
|
|
|
it('a creation with nothing to undo or redo gives no reason, and a press says nothing', async () => {
|
|
const user = await openAnnotations()
|
|
create()
|
|
expect(reason(undoButton())).toBeNull()
|
|
expect(reason(redoButton())).toBeNull()
|
|
await user.click(redoButton())
|
|
await user.click(undoButton())
|
|
undoButton().focus()
|
|
await user.keyboard('{Enter}')
|
|
noMessage()
|
|
expect(useAnnotationWork.getState().creation).not.toBeNull()
|
|
})
|
|
|
|
it('Space on a held « Défaire » and Enter on a pending « Refaire » say why', async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
create()
|
|
undoButton().focus()
|
|
await user.keyboard(' ')
|
|
expect(bubble()).toHaveTextContent(CREATION)
|
|
act(() => resetWork())
|
|
pend()
|
|
redoButton().focus()
|
|
await user.keyboard('{Enter}')
|
|
expect(bubble()).toHaveTextContent(PENDING)
|
|
})
|
|
|
|
it('say it in English', async () => {
|
|
const user = await openAnnotations()
|
|
act(() => useLocaleStore.setState({ locale: 'en' }))
|
|
stacks()
|
|
create()
|
|
const redo = screen.getByRole('button', {
|
|
name: 'Redo the last undone change',
|
|
})
|
|
const undo = screen.getByRole('button', {
|
|
name: 'Undo the last change',
|
|
})
|
|
expect(reason(redo)).toBe(
|
|
'Redo is paused during a creation: discard it to redo.',
|
|
)
|
|
expect(reason(undo)).toBe(
|
|
'Unavailable during a creation: create the annotation or discard it.',
|
|
)
|
|
await user.click(redo)
|
|
expect(bubble()).toHaveTextContent('Redo is paused during a creation')
|
|
act(() => resetWork())
|
|
pend()
|
|
expect(reason(redo)).toBe(
|
|
'Redo is paused during the change in progress: cancel it with Undo to redo.',
|
|
)
|
|
})
|
|
|
|
it('on another tab a held « Refaire » says why in its bubble too, and nothing is committed', async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
await user.click(screen.getByRole('tab', { name: 'Général' }))
|
|
pend()
|
|
const before = store.getState().documentText
|
|
await user.click(redoButton())
|
|
expect(reason(redoButton())).toBe(PENDING)
|
|
expect(bubble()).toHaveTextContent(PENDING)
|
|
expect(useAnnotationWork.getState().preview).not.toBeNull()
|
|
expect(store.getState().documentText).toBe(before)
|
|
expect(store.getState().redo).toHaveLength(1)
|
|
// A tab change closes it, and a later press shows it again.
|
|
await user.click(screen.getByRole('tab', { name: 'Annotations' }))
|
|
noMessage()
|
|
})
|
|
|
|
describe('the bubble', () => {
|
|
async function heldRedo() {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
pend()
|
|
await user.click(redoButton())
|
|
expect(bubble()).toHaveAttribute('aria-hidden', 'true')
|
|
return user
|
|
}
|
|
|
|
it("is announced by the button's always-mounted status region, once per press", async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
pend()
|
|
const region = screen.getByTestId('history-status-redo')
|
|
expect(region).toHaveAttribute('role', 'status')
|
|
expect(region).toBeEmptyDOMElement()
|
|
await user.click(redoButton())
|
|
expect(region).toBe(screen.getByTestId('history-status-redo'))
|
|
expect(region).toHaveTextContent(PENDING)
|
|
const first = region.firstElementChild
|
|
await user.click(redoButton())
|
|
expect(region).toHaveTextContent(PENDING)
|
|
expect(region.children).toHaveLength(1)
|
|
expect(region.firstElementChild).not.toBe(first)
|
|
await user.pointer({ keys: '[MouseLeft]', target: document.body })
|
|
expect(region).toBeEmptyDOMElement()
|
|
})
|
|
|
|
it('a second Escape drops the pending work, as before', async () => {
|
|
const user = await openAnnotations()
|
|
const list = screen.getByRole('list', { name: 'Annotations de la vue' })
|
|
await user.click(within(list).getAllByRole('button')[0]!)
|
|
stacks()
|
|
pend()
|
|
redoButton().focus()
|
|
await user.keyboard('{Enter}')
|
|
await user.keyboard('{Escape}')
|
|
noMessage()
|
|
expect(useAnnotationWork.getState().preview).not.toBeNull()
|
|
await user.keyboard('{Escape}')
|
|
expect(useAnnotationWork.getState().preview).toBeNull()
|
|
})
|
|
|
|
it('is portalled out of the row, so nothing in the layout can move', async () => {
|
|
await heldRedo()
|
|
expect(screen.getByTestId('history-buttons')).not.toContainElement(
|
|
bubble(),
|
|
)
|
|
expect(bubble().parentElement).toBe(document.body)
|
|
expect(bubble().style.position || bubble().className).toMatch(/fixed/)
|
|
})
|
|
|
|
it('closes when the hold ends', async () => {
|
|
await heldRedo()
|
|
act(() => useAnnotationWork.setState({ preview: null }))
|
|
noMessage()
|
|
pend()
|
|
noMessage()
|
|
})
|
|
|
|
it('closes when the focus leaves the button', async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
pend()
|
|
redoButton().focus()
|
|
await user.keyboard('{Enter}')
|
|
expect(bubble()).toBeInTheDocument()
|
|
await user.tab()
|
|
noMessage()
|
|
})
|
|
|
|
for (const [kind, keys] of [
|
|
['mouse', '[MouseLeft]'],
|
|
['touch', '[TouchA]'],
|
|
] as const) {
|
|
it(`closes on a ${kind} press elsewhere, not on the button itself`, async () => {
|
|
const user = await heldRedo()
|
|
await user.pointer({ keys, target: redoButton() })
|
|
expect(bubble()).toBeInTheDocument()
|
|
await user.pointer({ keys, target: document.body })
|
|
noMessage()
|
|
})
|
|
}
|
|
|
|
it('Escape closes it first and alone, the pending work stays', async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
pend()
|
|
redoButton().focus()
|
|
await user.keyboard('{Enter}')
|
|
await user.keyboard('{Escape}')
|
|
noMessage()
|
|
expect(useAnnotationWork.getState().preview).not.toBeNull()
|
|
})
|
|
|
|
it('sits under the pressed button, its right edge on the button, inside the window', async () => {
|
|
const user = await openAnnotations()
|
|
stacks()
|
|
pend()
|
|
const rect = { top: 10, bottom: 50, left: 900, right: 960 } as DOMRect
|
|
vi.spyOn(redoButton(), 'getBoundingClientRect').mockReturnValue(rect)
|
|
Object.defineProperty(document.documentElement, 'clientWidth', {
|
|
configurable: true,
|
|
value: 1024,
|
|
})
|
|
await user.click(redoButton())
|
|
expect(bubble()).toHaveTextContent(PENDING)
|
|
expect(bubble().style.getPropertyValue('--bubble-top')).toBe('54px')
|
|
expect(bubble().style.right).toBe('64px')
|
|
expect(bubble().style.maxWidth).toBe('min(18rem, 952px)')
|
|
delete (document.documentElement as { clientWidth?: number }).clientWidth
|
|
})
|
|
})
|
|
|
|
it('give no reason when nothing is pending, nothing to redo or read-only', async () => {
|
|
const user = await openAnnotations()
|
|
expect(reason(redoButton())).toBeNull()
|
|
await user.click(redoButton())
|
|
noMessage()
|
|
cleanup()
|
|
resetStore()
|
|
serverHas(baseText(), 3)
|
|
mountDraft({ width: 800 })
|
|
await ready()
|
|
await user.click(undoButton())
|
|
expect(reason(undoButton())).toBeNull()
|
|
expect(reason(redoButton())).toBeNull()
|
|
})
|
|
})
|