fix(editor): sign-out and shell links ask the drawing guard first

With a started move, sign-out and an in-app link now ask « Abandonner
le déplacement ? » (R-12) through the page's own question, registered by
the draft route in leaveGuardStore (`ask`; the shell imports no editor
module). The generic leave and sign-out dialogs cover only sheet-level
unsaved work; with both pending the drawing asks first, then the sheet
question, as useLeaveConfirm orders them. A move with nothing started
arms nothing (it ends); a started one still guards a reload.

Tests: sign-out and link with a started move (alone and with sheet
changes), the history jump with only a move and after dirty layers, a
section entry and « Tout afficher ».
This commit is contained in:
bchanot committed 2026-11-17 19:25:04 +01:00
1 parent 44de465c6d
commit e01d8519ea
8 files changed
+311 -26

No files matched your search

@@ -10,6 +10,14 @@ import { useAuthStore } from '@/state/authStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import { fakeUser } from '../helpers/fakeSupabase'
import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog'
import { guardDrawing } from '@/editor/annotations/drawingGuard'
import {
resetWork,
setModePreview,
startMove,
} from '@/editor/annotations/workStore'
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
const user = fakeUser()
const OTHER = 'bbbbbbbb-0000-0000-0000-000000000002'
@@ -101,3 +109,54 @@ describe('SignOutButton', () => {
expect(screen.queryByRole('dialog')).toBeNull()
})
})
describe('SignOutButton and a started move on the drawing (R-12)', () => {
const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 }
beforeEach(() => {
useLeaveGuardStore.getState().setAsk(guardDrawing)
startMove('a', 'doc')
setModePreview({ ...STORED, cx: 9 }, STORED)
})
afterEach(() => {
useLeaveGuardStore.getState().setAsk(null)
resetWork()
})
function mountWithGuard() {
render(
<MemoryRouter>
<SignOutButton />
<DrawingGuardDialog />
</MemoryRouter>,
)
}
const question = () =>
screen.queryByRole('dialog', { name: 'Abandonner le déplacement ?' })
it('asks the drawing question, never the sign-out text; « Continuer » stays', async () => {
mountWithGuard()
await click()
expect(question()).not.toBeNull()
expect(screen.queryByText(fr['shell.sign_out_loss_title'])).toBeNull()
await userEvent.click(screen.getByRole('button', { name: 'Continuer' }))
expect(signOut).not.toHaveBeenCalled()
})
it('« Abandonner » signs out when nothing else is at stake', async () => {
mountWithGuard()
await click()
await userEvent.click(screen.getByRole('button', { name: 'Abandonner' }))
await waitFor(() => expect(signOut).toHaveBeenCalledTimes(1))
})
it('with unsaved sheet changes too: the drawing first, then the sheet question', async () => {
useEditorStore.setState({ unsaved: true })
mountWithGuard()
await click()
expect(screen.queryByText(fr['shell.sign_out_loss_title'])).toBeNull()
await userEvent.click(screen.getByRole('button', { name: 'Abandonner' }))
expect(
await screen.findByText(fr['shell.sign_out_loss_title']),
).toBeTruthy()
expect(signOut).not.toHaveBeenCalled()
})
})
@@ -67,6 +67,7 @@ function Page({ guard }: { guard?: () => { allowed: boolean } }) {
<button onClick={() => nav.closeFinding()}>close</button>
<button onClick={() => nav.showAllReferences(finding)}>show all</button>
<button onClick={() => nav.navigate(targetOfKey(other))}>go to</button>
<button onClick={() => nav.openSection('views')}>section</button>
<button onClick={() => nav.guards.register('finding', guard ?? null)}>
guard
</button>
@@ -423,6 +424,22 @@ describe('the drawing guard (R-12): a move mode and the navigations', () => {
expect(useAnnotationWork.getState().mode).toBeNull()
})
it('a section entry and « Tout afficher » ask the same way', async () => {
useLocaleStore.setState({ locale: 'fr' })
const user = userEvent.setup()
mount()
started()
await user.click(screen.getByRole('button', { name: 'section' }))
expect(useAnnotationWork.getState().question?.work).toBe('move')
expect(tabShown()).toHaveTextContent('Général')
await user.click(screen.getByRole('button', { name: 'Continuer' }))
await user.click(screen.getByRole('button', { name: 'show all' }))
expect(useAnnotationWork.getState().question?.work).toBe('move')
expect(tabShown()).toHaveTextContent('Général')
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
expect(tabShown()).toHaveTextContent('Références')
})
it('« Aller à » asks the same way', async () => {
useLocaleStore.setState({ locale: 'fr' })
const user = userEvent.setup()
@@ -13,6 +13,13 @@ import {
type LayerName,
} from '@/editor/useEditorNavigation'
import { useLocaleStore } from '@/state/localeStore'
import { answerDrawing } from '@/editor/annotations/drawingGuard'
import {
resetWork,
setModePreview,
startMove,
useAnnotationWork,
} from '@/editor/annotations/workStore'
const path = '/app/admin/sheets/S/draft'
@@ -132,3 +139,54 @@ describe('reference, citations and finding dialogs, all dirty', () => {
expect(api.traverseTo).not.toHaveBeenCalled()
})
})
describe('the drawing guard on a history jump out of the editor (R-12)', () => {
const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 }
afterEach(resetWork)
it('with only a started move: the jump waits for its question, then goes', async () => {
cleanup()
render(
<MemoryRouter initialEntries={[path]}>
<DrawingOnly />
</MemoryRouter>,
)
startMove('a', 'doc')
setModePreview({ ...STORED, cx: 9 }, STORED)
expect(jump().defaultPrevented).toBe(true)
expect(useAnnotationWork.getState().question?.work).toBe('move')
expect(api.traverseTo).not.toHaveBeenCalled()
act(() => answerDrawing(true))
expect(api.traverseTo).toHaveBeenCalledWith('list')
})
it('with nothing started: the jump is not held and the mode ends', () => {
cleanup()
render(
<MemoryRouter initialEntries={[path]}>
<DrawingOnly />
</MemoryRouter>,
)
startMove('a', 'doc')
expect(jump().defaultPrevented).toBe(false)
})
it('dirty layers ask first, then the drawing, then the jump goes', async () => {
const user = userEvent.setup()
startMove('a', 'doc')
setModePreview({ ...STORED, cx: 9 }, STORED)
jump()
await user.click(button('reference', 'Abandonner'))
await user.click(button('citations', 'Abandonner'))
await user.click(button('finding', 'Abandonner'))
expect(api.traverseTo).not.toHaveBeenCalled()
expect(useAnnotationWork.getState().question?.work).toBe('move')
act(() => answerDrawing(true))
expect(api.traverseTo).toHaveBeenCalledWith('list')
})
})
function DrawingOnly() {
useLeaveConfirm(useEditorNavigation('findings'))
return null
}
@@ -28,6 +28,14 @@ import { useAuthStore } from '@/state/authStore'
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog'
import { guardDrawing } from '@/editor/annotations/drawingGuard'
import {
resetWork,
setModePreview,
startMove,
useAnnotationWork,
} from '@/editor/annotations/workStore'
let fake: FakeSupabase
@@ -271,6 +279,78 @@ describe('AppLayout, editor sidebar from 1 280 px', () => {
})
})
describe('AppLayout and a started move on the drawing (R-12)', () => {
const STORED = { type: 'circle' as const, cx: 1, cy: 2, r: 3 }
afterEach(() => {
useLeaveGuardStore.getState().setAsk(null)
resetWork()
})
function mountDraft() {
render(
<MemoryRouter initialEntries={['/app/admin/sheets/s-1/draft']}>
<Routes>
<Route path="/app" element={<AppLayout />}>
<Route path="account" element={<p>account page</p>} />
<Route
path="admin/sheets/:sheetId/draft"
element={
<>
<p>draft page</p>
<DrawingGuardDialog />
</>
}
/>
</Route>
</Routes>
</MemoryRouter>,
)
useLeaveGuardStore.getState().setAsk(guardDrawing)
}
const account = () =>
screen.getAllByRole('link', {
name: new RegExp(fr['shell.nav.account']),
})[0]
const started = () => {
startMove('a', 'doc')
setModePreview({ ...STORED, cx: 9 }, STORED)
}
it('a link asks the drawing question, not the form one; « Abandonner » follows it', async () => {
const user = userEvent.setup()
mountDraft()
started()
await user.click(account())
expect(
screen.getByRole('dialog', { name: 'Abandonner le déplacement ?' }),
).toBeInTheDocument()
expect(screen.queryByText(fr['leave.title'])).toBeNull()
expect(screen.queryByText('account page')).toBeNull()
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
expect(await screen.findByText('account page')).toBeInTheDocument()
})
it('with sheet changes too: the drawing question, then the leave question', async () => {
const user = userEvent.setup()
mountDraft()
started()
useLeaveGuardStore.getState().arm(true)
await user.click(account())
expect(screen.queryByText(fr['leave.title'])).toBeNull()
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
expect(await screen.findByText(fr['leave.title'])).toBeInTheDocument()
expect(screen.queryByText('account page')).toBeNull()
})
it('a move with nothing started leaves at once and ends', async () => {
const user = userEvent.setup()
mountDraft()
startMove('a', 'doc')
await user.click(account())
expect(await screen.findByText('account page')).toBeInTheDocument()
expect(useAnnotationWork.getState().mode).toBeNull()
})
})
describe('monogram', () => {
it('takes initials, else two letters, else ?', () => {
expect(monogram('Alice Local', undefined)).toBe('AL')