278 lines
10 KiB
TypeScript
278 lines
10 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 { resolveSessionTag } from '@/editor/history/sessionTag'
|
|
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,
|
|
tag: TAG,
|
|
...patch,
|
|
},
|
|
})
|
|
|
|
/** The session tag of USER in this tab (criterion 22). */
|
|
let TAG: string
|
|
beforeEach(async () => {
|
|
TAG = await resolveSessionTag(USER)
|
|
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,
|
|
tag: TAG,
|
|
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('a reload never reopens a form: the creation layer is replaced by the view without it', async () => {
|
|
const create = {
|
|
...defaultView('findings'),
|
|
finding: { create: { kind: 'zone', parent: null } },
|
|
}
|
|
const entries = [{ pathname: path, state: foreign({ view: create }) }]
|
|
mountDraft({ probe, entries })
|
|
await ready()
|
|
expect(selected()).toBe('Constats')
|
|
await waitFor(() => expect(written()?.view.finding).toBeNull())
|
|
expect(written()?.id).toBe('old')
|
|
expect(screen.queryByRole('dialog')).toBeNull()
|
|
})
|
|
|
|
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')
|
|
})
|
|
|
|
it('an entry of another session tag, or of the older shape, is foreign', async () => {
|
|
const older = foreign({}) as Record<string, Record<string, unknown>>
|
|
older[ENTRY_KEY] = { ...older[ENTRY_KEY], tag: undefined, userId: USER }
|
|
for (const state of [foreign({ tag: '0'.repeat(32) }), older]) {
|
|
mountDraft({ probe, entries: [{ pathname: path, state }] })
|
|
await ready()
|
|
await waitFor(() => expect(written()?.tag).toBe(TAG))
|
|
expect(selected()).not.toBe('Constats')
|
|
expect(written()?.id).not.toBe('old')
|
|
expect(JSON.stringify(written())).not.toContain(USER)
|
|
cleanup()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('tab and section moves', () => {
|
|
it('a tab click pushes, Back and Forward restore it, the tab holds 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')
|
|
})
|
|
|
|
// R-24: the first version of this case expected « Retour au dessin » to push and Back to
|
|
// return to the section; R2F-1 makes it a removal (a pop), so no duplicate entry remains.
|
|
it('a section is an entry; « Retour au dessin » pops it: no duplicate', async () => {
|
|
const user = userEvent.setup()
|
|
mountDraft({ probe, entries: ['/app/admin', path] })
|
|
await ready()
|
|
await user.click(tab('Annotations'))
|
|
await waitFor(() => expect(written()?.view.tab).toBe('annotations'))
|
|
const drawing = written()?.id
|
|
await user.click(screen.getByRole('button', { name: 'Vues' }))
|
|
await waitFor(() => expect(written()?.view.section).toBe('views'))
|
|
expect(written()?.below?.section).toBe('drawing')
|
|
await user.click(screen.getByRole('button', { name: 'Retour au dessin' }))
|
|
await waitFor(() => expect(written()?.id).toBe(drawing))
|
|
expect(written()?.view.section).toBe('drawing')
|
|
// one more Back leaves the drawing for the entry before it, not for the section
|
|
back()
|
|
await waitFor(() => expect(selected()).not.toBe('Annotations'))
|
|
})
|
|
|
|
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('scroll', () => {
|
|
const scrollAt = (y: number) =>
|
|
Object.defineProperty(window, 'scrollY', { configurable: true, value: y })
|
|
afterEach(() => scrollAt(0))
|
|
|
|
it('takes the offset in the move, restores it on Back', async () => {
|
|
const scrollTo = vi.spyOn(window, 'scrollTo').mockImplementation(() => {})
|
|
const user = userEvent.setup()
|
|
mountDraft({ probe })
|
|
await ready()
|
|
await waitFor(() => expect(written()).toBeTruthy())
|
|
scrollAt(300)
|
|
await user.click(tab('Constats'))
|
|
await waitFor(() => expect(written()?.view.tab).toBe('findings'))
|
|
scrollAt(40)
|
|
back()
|
|
await waitFor(() => expect(scrollTo).toHaveBeenCalledWith(0, 300))
|
|
back(1)
|
|
await waitFor(() => expect(scrollTo).toHaveBeenCalledWith(0, 40))
|
|
})
|
|
|
|
it('is manual while the editor is mounted, as it was after', async () => {
|
|
window.history.scrollRestoration = 'auto'
|
|
const view = mountDraft({ probe })
|
|
await ready()
|
|
expect(window.history.scrollRestoration).toBe('manual')
|
|
view.unmount()
|
|
expect(window.history.scrollRestoration).toBe('auto')
|
|
})
|
|
})
|
|
|
|
describe('a Back that must not unmount what is on screen', () => {
|
|
it('also refuses to replace a foreign entry while a dialog is open', async () => {
|
|
const entries = [{ pathname: path, state: foreign({ sheetId: 'x' }) }, path]
|
|
mountDraft({ probe, entries })
|
|
await ready()
|
|
await waitFor(() => expect(written()?.sheetId).toBe(SHEET))
|
|
const before = selected()
|
|
const dialog = document.createElement('dialog')
|
|
dialog.setAttribute('open', '')
|
|
document.body.append(dialog)
|
|
const id = written()?.id
|
|
back()
|
|
await waitFor(() => expect(written()?.id).not.toBe(id))
|
|
expect(selected()).toBe(before)
|
|
expect(written()?.sheetId).toBe(SHEET)
|
|
// pushed again above the landed entry, not replaced by a first entry
|
|
expect(written()?.below).not.toBeNull()
|
|
dialog.remove()
|
|
})
|
|
|
|
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)
|
|
})
|
|
})
|