feat(editor): session tag in history, confirm before leaving

This commit is contained in:
bchanot committed 2026-11-14 14:47:43 +01:00
1 parent 4018344ffc
commit 07bebbc855
19 files changed
+844 -76

No files matched your search

@@ -11,6 +11,10 @@ import {
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 {
forgetSessionTag,
resolveSessionTag,
} from '@/editor/history/sessionTag'
import { useCitationDraft } from '@/editor/findings/citationDraft'
import { useLocaleStore } from '@/state/localeStore'
import { edit, insert } from '../helpers/editorFixtures'
@@ -51,7 +55,12 @@ const written = () =>
(location.state as Record<string, HistoryEntry> | null)?.[ENTRY_KEY]
const back = () => act(async () => navigate(-1))
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
/** The session tag of USER in this tab: entries and flags carry it (criterion 22). */
let TAG: string
beforeEach(async () => {
useLocaleStore.setState({ locale: 'fr' })
TAG = await resolveSessionTag(USER)
})
afterEach(() => {
cleanup()
resetStore()
@@ -485,7 +494,7 @@ describe('read-only flip and the gone rule', () => {
view,
below: null,
sheetId: SHEET,
userId: USER,
tag: TAG,
},
}
mountDraft({ probe, entries: [{ pathname: path, state }] })
@@ -515,7 +524,7 @@ describe('read-only flip and the gone rule', () => {
view,
below: null,
sheetId: SHEET,
userId: USER,
tag: TAG,
},
}
mountDraft({ probe, entries: [{ pathname: path, state }] })
@@ -586,7 +595,7 @@ describe('final review: the zone of a new category is part of the draft (criteri
})
describe('final review: leaving the editor with a dirty dialog (criterion 17)', () => {
const flagKey = `faunosteo:editor:left-draft:${SHEET}:${USER}`
const flagKey = () => `faunosteo:editor:left-draft:${SHEET}:${TAG}`
const notice =
"Une saisie non enregistrée a été abandonnée en quittant l'éditeur"
@@ -596,7 +605,7 @@ describe('final review: leaving the editor with a dirty dialog (criterion 17)',
await modify(user)
await user.type(nameField(), ' bis')
cleanup()
const flag = sessionStorage.getItem(flagKey)
const flag = sessionStorage.getItem(flagKey())
expect(flag).not.toBeNull()
expect(flag).not.toContain('bis')
expect(JSON.parse(flag!)).toEqual({
@@ -605,17 +614,133 @@ describe('final review: leaving the editor with a dirty dialog (criterion 17)',
})
await mount()
expect(screen.getByText(notice)).toBeVisible()
expect(sessionStorage.getItem(flagKey)).toBeNull()
expect(sessionStorage.getItem(flagKey())).toBeNull()
cleanup()
await mount()
expect(screen.queryByText(notice)).toBeNull()
})
it('a sign-out leaves no flag under the tag it rotated', async () => {
const user = await mount()
await openFinding(user, /Node A/)
await modify(user)
await user.type(nameField(), ' bis')
act(() => forgetSessionTag())
await waitFor(() => expect(findingDialog()).toBeNull())
const flags = Object.keys(sessionStorage).filter((k) =>
k.startsWith('faunosteo:editor:left-draft:'),
)
expect(flags).toEqual([])
})
it('a clean dialog leaves no flag', async () => {
const user = await mount()
await openFinding(user, /Node A/)
cleanup()
expect(sessionStorage.getItem(flagKey)).toBeNull()
expect(sessionStorage.getItem(flagKey())).toBeNull()
})
})
/** The Navigation API as the guard reads it: `navigate` events and `traverseTo`. */
class NavigationStub extends EventTarget {
traverseTo = vi.fn((key: string) => {
void key
return { committed: Promise.resolve(), finished: Promise.resolve() }
})
}
describe('a jump of several entries out of the editor (criterion 23)', () => {
const flagKey = () => `faunosteo:editor:left-draft:${SHEET}:${TAG}`
const notice =
"Une saisie non enregistrée a été abandonnée en quittant l'éditeur"
let api: NavigationStub
beforeEach(() => {
api = new NavigationStub()
Object.defineProperty(window, 'navigation', {
value: api,
configurable: true,
})
})
afterEach(() => {
Reflect.deleteProperty(window, 'navigation')
})
/** A history jump to `pathname`, as Chromium dispatches it before it commits. */
function traverse(pathname: string, key: string, cancelable = true) {
const url = new URL(pathname, window.location.origin).href
const event = Object.assign(new Event('navigate', { cancelable }), {
navigationType: 'traverse',
destination: { url, key, sameDocument: true },
})
act(() => {
api.dispatchEvent(event)
})
return event
}
async function dirtyDialog() {
const user = await mount()
await openFinding(user, /Node A/)
await modify(user)
await user.type(nameField(), ' bis')
return user
}
it('is cancelled and asks first; « Continuer la saisie » keeps the draft', async () => {
const user = await dirtyDialog()
const jump = traverse('/app/admin/sheets', 'list')
expect(jump.defaultPrevented).toBe(true)
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
await user.click(
inDialog().getByRole('button', { name: 'Continuer la saisie' }),
)
expect(nameField()).toHaveValue('Node A bis')
expect(api.traverseTo).not.toHaveBeenCalled()
expect(sessionStorage.getItem(flagKey())).toBeNull()
})
it('« Abandonner » goes to the destination, with no notice afterwards', async () => {
const user = await dirtyDialog()
traverse('/app/admin/sheets', 'list')
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
expect(api.traverseTo).toHaveBeenCalledWith('list')
expect(traverse('/app/admin/sheets', 'list').defaultPrevented).toBe(false)
cleanup()
expect(sessionStorage.getItem(flagKey())).toBeNull()
await mount()
expect(screen.queryByText(notice)).toBeNull()
})
it('« Abandonner » after « Continuer la saisie » closes the dialog only', async () => {
const user = await dirtyDialog()
traverse('/app/admin/sheets', 'list')
await user.click(
inDialog().getByRole('button', { name: 'Continuer la saisie' }),
)
await user.click(inDialog().getByRole('button', { name: 'Annuler' }))
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
expect(api.traverseTo).not.toHaveBeenCalled()
expect(findingDialog()).not.toBeNull()
})
it('a jump that cannot be cancelled keeps the notice after the fact', async () => {
await dirtyDialog()
expect(traverse('/app/admin/sheets', 'list', false).defaultPrevented).toBe(
false,
)
expect(screen.queryByText('Abandonner les modifications ?')).toBeNull()
cleanup()
expect(sessionStorage.getItem(flagKey())).not.toBeNull()
})
it('a traversal inside the editor, or a clean dialog, is left alone', async () => {
const user = await mount()
await openFinding(user, /Node A/)
expect(traverse('/app/admin/sheets', 'list').defaultPrevented).toBe(false)
await modify(user)
await user.type(nameField(), ' bis')
expect(traverse(path, 'inside').defaultPrevented).toBe(false)
expect(screen.queryByText('Abandonner les modifications ?')).toBeNull()
})
})