396 lines
14 KiB
TypeScript
396 lines
14 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',
|
|
'Vues et illustrations',
|
|
'Annotations',
|
|
'Constats',
|
|
'Références',
|
|
'Vertèbres',
|
|
'Vérification',
|
|
]
|
|
|
|
describe('edit mode', () => {
|
|
it('header, badges, the seven tabs, nothing read-only', async () => {
|
|
mountDraft()
|
|
await ready()
|
|
expect(screen.getByText('Brouillon')).toBeInTheDocument()
|
|
expect(screen.getByText('Copie de travail')).toBeInTheDocument()
|
|
expect(screen.getByRole('status', { name: '' })).toBeDefined()
|
|
expect(screen.getByText('Révision 3')).toBeInTheDocument()
|
|
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))
|
|
expect(
|
|
await screen.findByText('Révision 3 · modifications non enregistrées'),
|
|
).toBeInTheDocument()
|
|
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
|
expect(await screen.findByText('Révision 4')).toBeInTheDocument()
|
|
expect(sb.saveSheetDraft).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.objectContaining({ version_id: VERSION, expected_revision: 3 }),
|
|
)
|
|
expect(storedCopy()).toBeNull()
|
|
})
|
|
|
|
it('« Annuler 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: 'Annuler les modifications' }),
|
|
)
|
|
const dialog = within(await screen.findByRole('dialog'))
|
|
expect(dialog.getByText(fr['editor.draft.discard_ask'])).toBeInTheDocument()
|
|
await user.click(
|
|
dialog.getByRole('button', { name: 'Abandonner les modifications' }),
|
|
)
|
|
await waitFor(() => expect(store.getState().unsaved).toBe(false))
|
|
expect(await screen.findByText('Révision 3')).toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
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.queryByTestId('editor-tab-annotations')).toBeNull()
|
|
expect(screen.getByRole('tabpanel')).toHaveAccessibleName('Constats')
|
|
})
|
|
|
|
it('640 to 1 023 px: four tabs and « Plus »; the others sit under the disclosure', async () => {
|
|
const user = userEvent.setup()
|
|
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',
|
|
])
|
|
const more = screen.getByRole('button', { name: 'Plus' })
|
|
expect(more).toHaveAttribute('aria-expanded', 'false')
|
|
expect(screen.queryByRole('button', { name: 'Références' })).toBeNull()
|
|
await user.click(more)
|
|
expect(more).toHaveAttribute('aria-expanded', 'true')
|
|
await user.click(screen.getByRole('button', { name: 'Références' }))
|
|
expect(screen.getByTestId('editor-tab-references')).toBeInTheDocument()
|
|
expect(more).toHaveAttribute('aria-expanded', 'false')
|
|
})
|
|
|
|
it('below 640 px: two tabs and « Plus » listing the five others', 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',
|
|
'Vues et illustrations',
|
|
'Constats',
|
|
'Références',
|
|
'Vertèbres',
|
|
])
|
|
})
|
|
})
|
|
|
|
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))
|
|
expect(
|
|
await screen.findByText(fr['editor.draft.status.copy_quota']),
|
|
).toBeInTheDocument()
|
|
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'])
|
|
})
|
|
})
|