Files
faunosteo/apps/app/tests/unit/editor-history.test.tsx
T
bchanot 792394d3b2 feat(editor): browser history for tabs, sections and Aller a
EditorView held by the navigation reducer (restore_view), a history
adapter hook mounted in EditorWorkspace (push, replace, back, mount
rule, ownership, write budget, scroll map, refused POP), a guard stack
by layer, an announcer queue joined after open dialogs.
2026-11-13 17:57:52 +01:00

194 lines
6.6 KiB
TypeScript

/** R2F-1: the browser history follows tab, section and « Aller à » moves (MemoryRouter). */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
act,
cleanup,
fireEvent,
screen,
waitFor,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { Location, NavigateFunction } from 'react-router-dom'
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
import { defaultView } from '@/editor/navigation'
import { useLocaleStore } from '@/state/localeStore'
import { emptyText } from '../helpers/editorFixtures'
import { resetStore, serverHas, SHEET, USER } from '../helpers/editorHarness'
import { mountDraft, 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(),
}))
let navigate: NavigateFunction
let location: Location
const probe = (n: NavigateFunction, l: Location) => {
navigate = n
location = l
}
const written = () =>
(location.state as Record<string, HistoryEntry> | null)?.[ENTRY_KEY]
const ready = () =>
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
const tab = (name: string) => screen.getByRole('tab', { name })
const selected = () =>
screen.getByRole('tab', { selected: true }).textContent ?? ''
const back = (delta = -1) => act(() => navigate(delta))
const path = `/app/admin/sheets/${SHEET}/draft`
const foreign = (patch: Partial<HistoryEntry>) => ({
[ENTRY_KEY]: {
id: 'old',
view: defaultView('findings'),
below: null,
sheetId: SHEET,
userId: USER,
...patch,
},
})
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
serverHas(emptyText(), 3, false)
})
afterEach(() => {
cleanup()
resetStore()
setWidth(1024)
})
describe('mount', () => {
it('writes the default view as the first entry', async () => {
mountDraft({ probe })
await ready()
await waitFor(() => expect(written()).toBeTruthy())
const tabs: Record<string, string> = {
Général: 'general',
Annotations: 'annotations',
}
expect(written()?.view.tab).toBe(tabs[selected()])
expect(written()).toMatchObject({
sheetId: SHEET,
userId: USER,
below: null,
})
})
it('restores the view of an entry of this sheet and account', async () => {
const entries = [{ pathname: path, state: foreign({}) }]
mountDraft({ probe, entries })
await ready()
expect(selected()).toBe('Constats')
expect(written()?.id).toBe('old')
})
it('replaces an entry of another sheet or account by the default view', async () => {
const entries = [{ pathname: path, state: foreign({ sheetId: 'other' }) }]
mountDraft({ probe, entries })
await ready()
await waitFor(() => expect(written()?.sheetId).toBe(SHEET))
expect(selected()).not.toBe('Constats')
expect(written()?.id).not.toBe('old')
})
})
describe('tab and section moves', () => {
it('a tab click pushes, Back and Forward restore it, the tab button holds the focus', async () => {
const user = userEvent.setup()
mountDraft({ probe })
await ready()
await waitFor(() => expect(written()).toBeTruthy())
const first = selected()
await user.click(tab('Constats'))
await waitFor(() => expect(written()?.view.tab).toBe('findings'))
expect(written()?.below?.tab).not.toBe('findings')
back()
await waitFor(() => expect(selected()).toBe(first))
await waitFor(() => expect(document.activeElement).toBe(tab(first)))
back(1)
await waitFor(() => expect(selected()).toBe('Constats'))
await waitFor(() => expect(document.activeElement).toBe(tab('Constats')))
})
it('arrow keys along the row replace the entry instead of adding one', async () => {
const user = userEvent.setup()
mountDraft({ probe, entries: ['/app/admin', path] })
await ready()
await user.click(tab('Constats'))
await waitFor(() => expect(written()?.view.tab).toBe('findings'))
tab('Constats').focus()
fireEvent.keyDown(tab('Constats'), { key: 'ArrowRight' })
await waitFor(() => expect(selected()).toBe('Références'))
expect(written()?.view.tab).toBe('references')
// one Back leaves the replaced entry for the one below it, not for « Constats »
back()
await waitFor(() => expect(selected()).not.toBe('Références'))
expect(selected()).not.toBe('Constats')
})
it('an Annotations section pushes, « Retour au dessin » too, Back returns to the section', async () => {
const user = userEvent.setup()
mountDraft({ probe })
await ready()
await user.click(tab('Annotations'))
await user.click(screen.getByRole('button', { name: 'Vues' }))
await waitFor(() => expect(written()?.view.section).toBe('views'))
await user.click(screen.getByRole('button', { name: 'Retour au dessin' }))
await waitFor(() => expect(written()?.view.section).toBe('drawing'))
back()
await waitFor(() =>
expect(
screen.getByRole('button', { name: 'Retour au dessin' }),
).toBeVisible(),
)
expect(written()?.view.section).toBe('views')
})
it('a move that changes nothing writes nothing', async () => {
const user = userEvent.setup()
mountDraft({ probe })
await ready()
await waitFor(() => expect(written()).toBeTruthy())
const before = location.key
await user.click(screen.getByRole('tab', { selected: true }))
expect(location.key).toBe(before)
})
})
describe('a Back that must not unmount what is on screen', () => {
it('keeps the view and pushes it again while a dialog is open', async () => {
const user = userEvent.setup()
mountDraft({ probe })
await ready()
await user.click(tab('Constats'))
await waitFor(() => expect(written()?.view.tab).toBe('findings'))
const dialog = document.createElement('dialog')
dialog.setAttribute('open', '')
document.body.append(dialog)
back()
await waitFor(() => expect(written()?.view.tab).toBe('findings'))
expect(selected()).toBe('Constats')
// the re-pushed entry sits above the one landed on
expect(written()?.below?.tab).not.toBe('findings')
dialog.remove()
})
})
describe('StrictMode', () => {
it('writes the first entry once', async () => {
mountDraft({ probe, strict: true })
await ready()
await waitFor(() => expect(written()).toBeTruthy())
const id = written()?.id
expect(id).toBeTruthy()
await new Promise((resolve) => setTimeout(resolve, 20))
expect(written()?.id).toBe(id)
})
})