Header Annuler/Retablir on every tab (aria-disabled, commit on pointerdown, refused blur commit skips the undo). Save line: unsaved count selector (savedDistance), saveLineState forms, server save time via readSaveTime (lastSave in the store, never the browser clock). Discard relabelled and names the stored copy. Tool column pair removed, glyphs in EditorGlyph. Rewritten or removed assertions (R-24): - editor-route.test: Revision 3 / unsaved text / Annuler les modifications -> save-line testid, Abandonner label, copy sentence in the dialog - editor-route-conflict, editor-route-recovery: Revision n and copy_cap/ copy_quota text queries -> save-line testid - editor-no-loss: discard button label - editor-annotations-tab: tool column undo/redo test -> no pair left (behaviour moved to editor-history-buttons.test) - e2e journey/annotations/recovery/structures/support saveDraft/lan: undo/redo and revision/unsaved text -> historyButton, saveLine helpers; structures toHaveCount(0) on old text -> exact Revision n line - editor-shell clickability: header Annuler/Retablir added
435 lines
16 KiB
TypeScript
435 lines
16 KiB
TypeScript
/** Lot E: the draft route, header, tabs, modes and permission states, leave guard. */
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { act, cleanup, screen, waitFor, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { SheetFailure } from '@faunosteo/supabase'
|
|
import { useAuthStore } from '@/state/authStore'
|
|
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
|
|
import { fr } from '@/i18n/fr'
|
|
import {
|
|
VERSION,
|
|
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 ready = () =>
|
|
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
|
|
const TABS = ['Général', 'Annotations', 'Constats', 'Vérification']
|
|
|
|
describe('edit mode', () => {
|
|
it('header, badges, the four tabs, nothing read-only', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
expect(screen.getByText('Brouillon')).toBeInTheDocument()
|
|
expect(screen.getByText('Copie de travail')).toBeInTheDocument()
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Révision 3$/)
|
|
const tabs = within(screen.getByRole('tablist')).getAllByRole('tab')
|
|
expect(tabs.map((tab) => tab.textContent)).toEqual(TABS)
|
|
expect(screen.getByRole('tab', { name: 'Annotations' })).toHaveAttribute(
|
|
'aria-selected',
|
|
'true',
|
|
)
|
|
expect(screen.queryByText('Lecture seule')).toBeNull()
|
|
expect(screen.getByRole('button', { name: 'Enregistrer' })).toBeDisabled()
|
|
expect(store.getState()).toMatchObject({
|
|
canEdit: true,
|
|
versionId: VERSION,
|
|
})
|
|
})
|
|
|
|
it('opens the draft read for this version only and closes the session on leaving', async () => {
|
|
const view = mountDraft()
|
|
await ready()
|
|
expect(sb.getSheetVersion).toHaveBeenCalledWith(expect.anything(), VERSION)
|
|
view.unmount()
|
|
expect(store.getState().status).toBe('idle')
|
|
})
|
|
|
|
it('a commit shows the unsaved line; Enregistrer saves against the loaded revision', async () => {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await waitFor(() =>
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(/^1 modification non enregistrée$/),
|
|
)
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
await waitFor(() =>
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Enregistré · révision 4$/),
|
|
)
|
|
expect(sb.saveSheetDraft).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.objectContaining({ version_id: VERSION, expected_revision: 3 }),
|
|
)
|
|
expect(storedCopy()).toBeNull()
|
|
})
|
|
|
|
it('« Abandonner les modifications » asks first, then reloads the server draft', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await user.click(
|
|
await screen.findByRole('button', { name: 'Abandonner les modifications' }),
|
|
)
|
|
const asked = await screen.findByRole('dialog')
|
|
expect(asked).toHaveTextContent(fr['editor.draft.discard_ask'])
|
|
expect(asked).toHaveTextContent(fr['editor.draft.discard_copy'])
|
|
const dialog = within(asked)
|
|
await user.click(
|
|
dialog.getByRole('button', { name: 'Abandonner les modifications' }),
|
|
)
|
|
await waitFor(() => expect(store.getState().unsaved).toBe(false))
|
|
await waitFor(() =>
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Révision 3$/),
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('tabs', () => {
|
|
it('arrow keys move between the shown tabs, only the active body is mounted', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
expect(screen.getByTestId('editor-tab-annotations')).toBeInTheDocument()
|
|
await user.click(screen.getByRole('tab', { name: 'Annotations' }))
|
|
await user.keyboard('{ArrowRight}')
|
|
expect(screen.getByRole('tab', { name: 'Constats' })).toHaveAttribute(
|
|
'aria-selected',
|
|
'true',
|
|
)
|
|
expect(screen.getByTestId('editor-tab-findings')).toBeInTheDocument()
|
|
expect(screen.getByTestId('editor-tab-annotations')).not.toBeVisible()
|
|
expect(screen.getByRole('tabpanel')).toHaveAccessibleName('Constats')
|
|
})
|
|
|
|
it('640 to 1 023 px: the four tabs, no « Plus »', async () => {
|
|
mountDraft({ width: 800 })
|
|
await ready()
|
|
const shown = within(screen.getByRole('tablist')).getAllByRole('tab')
|
|
expect(shown.map((tab) => tab.textContent)).toEqual([
|
|
'Général',
|
|
'Annotations',
|
|
'Constats',
|
|
'Vérification',
|
|
])
|
|
expect(screen.queryByRole('button', { name: 'Plus' })).toBeNull()
|
|
})
|
|
|
|
it('below 640 px: two tabs and « Plus » listing Général and Constats', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft({ width: 375 })
|
|
await ready()
|
|
const shown = within(screen.getByRole('tablist')).getAllByRole('tab')
|
|
expect(shown.map((tab) => tab.textContent)).toEqual([
|
|
'Annotations',
|
|
'Vérification',
|
|
])
|
|
await user.click(screen.getByRole('button', { name: 'Plus' }))
|
|
const more = document.getElementById('editor-tabs-more') as HTMLElement
|
|
const rest = within(more).getAllByRole('button')
|
|
expect(rest.map((b) => b.textContent)).toEqual(['Général', 'Constats'])
|
|
await user.click(rest[1])
|
|
expect(screen.getByTestId('editor-tab-findings')).toBeInTheDocument()
|
|
expect(document.getElementById('editor-tabs-more')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('references panel', () => {
|
|
const button = () => screen.getByRole('button', { name: 'Références' })
|
|
|
|
it('the header button opens it on its heading and ✕ returns the focus', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
expect(button()).toHaveAttribute('aria-expanded', 'false')
|
|
expect(screen.queryByTestId('editor-references-panel')).toBeNull()
|
|
await user.click(button())
|
|
const panel = screen.getByTestId('editor-references-panel')
|
|
expect(panel).toBeVisible()
|
|
expect(button()).toHaveAttribute('aria-expanded', 'true')
|
|
expect(document.activeElement).toBe(
|
|
within(panel).getByRole('heading', { name: 'Références' }),
|
|
)
|
|
expect(screen.getByRole('tab', { name: 'Annotations' })).toHaveAttribute(
|
|
'aria-selected',
|
|
'true',
|
|
)
|
|
await user.click(
|
|
within(panel).getByRole('button', { name: 'Fermer les références' }),
|
|
)
|
|
expect(panel).not.toBeVisible()
|
|
expect(document.activeElement).toBe(button())
|
|
})
|
|
|
|
it('Escape inside the panel closes it, the content is kept for the next opening', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft()
|
|
await ready()
|
|
await user.click(button())
|
|
const panel = screen.getByTestId('editor-references-panel')
|
|
await user.keyboard('{Escape}')
|
|
expect(panel).not.toBeVisible()
|
|
await user.click(button())
|
|
expect(screen.getByTestId('editor-references-panel')).toBe(panel)
|
|
expect(panel).toBeVisible()
|
|
})
|
|
|
|
it('read-only: the panel lists the references with no edit control', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft({ width: 800 })
|
|
await ready()
|
|
await user.click(button())
|
|
const panel = screen.getByTestId('editor-references-panel')
|
|
expect(within(panel).queryByRole('textbox')).toBeNull()
|
|
expect(
|
|
within(panel).queryByRole('button', { name: 'Ajouter la référence' }),
|
|
).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('read-only modes', () => {
|
|
it('narrow: the L15 notice alone, the lock badge, no save, tabs readable, copy offered', async () => {
|
|
mountDraft({ width: 800 })
|
|
await ready()
|
|
expect(
|
|
screen.getByText(
|
|
"L'édition nécessite une largeur d'écran d'au moins 1 024 px.",
|
|
),
|
|
).toBeInTheDocument()
|
|
expect(screen.getByText('Lecture seule')).toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Enregistrer' })).toBeNull()
|
|
expect(screen.queryByText(/Ici, consultation seule/)).toBeNull()
|
|
})
|
|
|
|
it('a width switch keeps the unsaved work and the leave guard', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
act(() => {
|
|
setWidth(800)
|
|
window.dispatchEvent(new Event('resize'))
|
|
})
|
|
expect(await screen.findByText('Lecture seule')).toBeInTheDocument()
|
|
expect(store.getState().unsaved).toBe(true)
|
|
expect(useLeaveGuardStore.getState().armed).toBe(true)
|
|
act(() => {
|
|
setWidth(1280)
|
|
window.dispatchEvent(new Event('resize'))
|
|
})
|
|
expect(
|
|
await screen.findByRole('button', { name: 'Enregistrer' }),
|
|
).toBeEnabled()
|
|
expect(store.getState().unsaved).toBe(true)
|
|
})
|
|
|
|
it('rights: a reviewer without the editor capability reads the draft', async () => {
|
|
mountDraft({ grants: [grant('sheet_reviewer')] })
|
|
await ready()
|
|
expect(screen.getByText(fr['editor.draft.mode.rights'])).toBeInTheDocument()
|
|
expect(screen.getByText('Lecture seule')).toBeInTheDocument()
|
|
expect(store.getState().canEdit).toBe(false)
|
|
})
|
|
|
|
it('rights: the editor capability without the admin or owner role is read-only', async () => {
|
|
mountDraft({ roles: [] })
|
|
await ready()
|
|
expect(screen.getByText(fr['editor.draft.mode.rights'])).toBeInTheDocument()
|
|
})
|
|
|
|
it('frozen: the round, its label and the link to the overview', async () => {
|
|
mountDraft({ answer: adminAnswer({ rounds: [round('verifying')] }) })
|
|
await ready()
|
|
expect(
|
|
screen.getByText('Revue n° 1 · vérification en cours'),
|
|
).toBeInTheDocument()
|
|
expect(screen.getByRole('link', { name: 'Voir la fiche' })).toHaveAttribute(
|
|
'href',
|
|
expect.stringMatching(/\/app\/admin\/sheets\/[^/]+$/),
|
|
)
|
|
})
|
|
|
|
it('frozen by an open round: the round status label', async () => {
|
|
mountDraft({
|
|
answer: adminAnswer({ rounds: [round('open', { round: 2 })] }),
|
|
})
|
|
await ready()
|
|
expect(screen.getByText('Revue n° 2 · À relire')).toBeInTheDocument()
|
|
})
|
|
|
|
it('a round that no longer freezes (changes requested) leaves the draft editable', async () => {
|
|
mountDraft({
|
|
answer: adminAnswer({ rounds: [round('changes_requested')] }),
|
|
})
|
|
await ready()
|
|
expect(screen.queryByText('Lecture seule')).toBeNull()
|
|
})
|
|
|
|
it('historical and inactive sheets', async () => {
|
|
mountDraft({ answer: adminAnswer({ historical: true }) })
|
|
await ready()
|
|
expect(
|
|
screen.getByText(fr['editor.draft.mode.historical']),
|
|
).toBeInTheDocument()
|
|
cleanup()
|
|
resetStore()
|
|
serverHas(baseText(), 3)
|
|
mountDraft({ answer: adminAnswer({ status: 'archived' }) })
|
|
await ready()
|
|
expect(
|
|
screen.getByText(fr['editor.draft.mode.inactive']),
|
|
).toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('permission states', () => {
|
|
it('own grants loading: waits, reads nothing of the draft', async () => {
|
|
mountDraft({ grantsStatus: 'loading' })
|
|
expect(screen.getByText(fr['shell.loading_session'])).toHaveAttribute(
|
|
'role',
|
|
'status',
|
|
)
|
|
await act(async () => {})
|
|
expect(sb.getSheetVersion).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('a rights refusal of the admin read: « Accès non autorisé »', async () => {
|
|
mountDraft({ adminFailure: 'rights.capability' })
|
|
expect(
|
|
await screen.findByRole('heading', { name: fr['access_denied.title'] }),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
it('own grants unreadable: the unavailable notice with its retry', async () => {
|
|
const reloadAccess = vi.fn(async () =>
|
|
useAuthStore.setState({ catalogGrantsStatus: 'ready' }),
|
|
)
|
|
mountDraft({ grantsStatus: 'failed' })
|
|
act(() => useAuthStore.setState({ reloadAccess }))
|
|
await act(async () => {})
|
|
const user = userEvent.setup()
|
|
await user.click(await screen.findByRole('button', { name: 'Réessayer' }))
|
|
expect(reloadAccess).toHaveBeenCalled()
|
|
await ready()
|
|
})
|
|
|
|
it('an unknown sheet: the not found page', async () => {
|
|
mountDraft({ adminFailure: 'not_found' })
|
|
expect(
|
|
await screen.findByRole('heading', {
|
|
name: fr['admin.overview.not_found_title'],
|
|
}),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
it('no draft: a notice and the link to the overview', async () => {
|
|
mountDraft({ answer: adminAnswer({ draft_version: null }) })
|
|
expect(await screen.findByText(fr['editor.draft.none'])).toBeInTheDocument()
|
|
expect(
|
|
screen.getByRole('link', { name: 'Voir la fiche' }),
|
|
).toBeInTheDocument()
|
|
expect(sb.getSheetVersion).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('leave guard', () => {
|
|
it('armed from the store flag while unsaved, disarmed once saved', async () => {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
|
mountDraft()
|
|
await ready()
|
|
expect(useLeaveGuardStore.getState().armed).toBe(false)
|
|
act(() => void commitMove(40))
|
|
await waitFor(() => expect(useLeaveGuardStore.getState().armed).toBe(true))
|
|
const event = new Event('beforeunload', { cancelable: true })
|
|
window.dispatchEvent(event)
|
|
expect(event.defaultPrevented).toBe(true)
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
await waitFor(() => expect(useLeaveGuardStore.getState().armed).toBe(false))
|
|
const after = new Event('beforeunload', { cancelable: true })
|
|
window.dispatchEvent(after)
|
|
expect(after.defaultPrevented).toBe(false)
|
|
})
|
|
|
|
it('leaving the page disarms it', async () => {
|
|
const view = mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await waitFor(() => expect(useLeaveGuardStore.getState().armed).toBe(true))
|
|
view.unmount()
|
|
expect(useLeaveGuardStore.getState().armed).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('permission lost during the session', () => {
|
|
async function refusedSave(
|
|
kind: ConstructorParameters<typeof SheetFailure>[0],
|
|
) {
|
|
const user = userEvent.setup()
|
|
sb.saveSheetDraft.mockRejectedValue(new SheetFailure(kind))
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await user.click(await screen.findByRole('button', { name: 'Enregistrer' }))
|
|
}
|
|
|
|
it('draft.frozen: the message promises a copy only because it was written; read-only follows', async () => {
|
|
await refusedSave('draft.frozen')
|
|
expect(
|
|
await screen.findByText(/Une revue est en cours sur ce brouillon/),
|
|
).toHaveTextContent(fr['editor.draft.copy_kept'])
|
|
expect(screen.getByText('Lecture seule')).toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Enregistrer' })).toBeNull()
|
|
expect(sb.saveSheetDraft).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('session lost with a failed copy write: the changes cannot be kept', async () => {
|
|
const user = userEvent.setup()
|
|
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
|
throw new DOMException('full', 'QuotaExceededError')
|
|
})
|
|
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('session_expired'))
|
|
mountDraft()
|
|
await ready()
|
|
act(() => void commitMove(40))
|
|
await waitFor(() =>
|
|
expect(screen.getByTestId('save-line')).toHaveTextContent(fr['editor.draft.status.copy_quota']),
|
|
)
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
const notice = await screen.findByText(/Votre session a expiré/)
|
|
expect(notice).toHaveTextContent(fr['editor.draft.copy_not_kept'])
|
|
expect(notice).not.toHaveTextContent(fr['editor.draft.copy_kept'])
|
|
})
|
|
})
|