fix(app): shell takes a link only with drawing work at stake

The draft route registers an at-stake predicate with its question;
AppLayout intercepts and replays a link through navigate(to) only when
it holds (or when unsaved changes arm the leave guard), so a Link keeps
its own replace and state. A download anchor is never taken.

Tests: the unstarted-move test no longer expects the mode ended by the
shell (the link is not intercepted; leaving the editor resets the work);
new: a Link's state reaches the page, a download link is not prevented.
This commit is contained in:
bchanot committed 2026-11-17 22:56:37 +01:00
1 parent 87528303a1
commit 2d58da7d01
4 files changed
+87 -21

No files matched your search

+63 -11
View File
@@ -8,7 +8,13 @@ import {
within,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import {
Link,
MemoryRouter,
Route,
Routes,
useLocation,
} from 'react-router-dom'
import {
createFakeSupabase,
fakeProfile,
@@ -31,6 +37,7 @@ import { fr } from '@/i18n/fr'
import DrawingGuardDialog from '@/components/editor/annotations/DrawingGuardDialog'
import { guardDrawing } from '@/editor/annotations/drawingGuard'
import {
guardWork,
resetWork,
markRedraw,
setModePreview,
@@ -292,12 +299,18 @@ describe('AppLayout and a started move on the drawing (R-12)', () => {
<MemoryRouter initialEntries={['/app/admin/sheets/s-1/draft']}>
<Routes>
<Route path="/app" element={<AppLayout />}>
<Route path="account" element={<p>account page</p>} />
<Route path="account" element={<AccountPage />} />
<Route
path="admin/sheets/:sheetId/draft"
element={
<>
<p>draft page</p>
<Link to="/app/account" replace state={{ from: 'draft' }}>
page link
</Link>
<a href="/export.json" download onClick={onDownload}>
download link
</a>
<DrawingGuardDialog />
</>
}
@@ -306,7 +319,28 @@ describe('AppLayout and a started move on the drawing (R-12)', () => {
</Routes>
</MemoryRouter>,
)
useLeaveGuardStore.getState().setAsk(guardDrawing)
// As the draft route registers it: the question, and whether anything is at stake.
useLeaveGuardStore
.getState()
.setAsk(
guardDrawing,
() => guardWork(useAnnotationWork.getState()) !== null,
)
}
/** The download anchor's own click: whether a handler before it took the click. */
const downloads: boolean[] = []
function onDownload(event: {
defaultPrevented: boolean
preventDefault(): void
}) {
downloads.push(event.defaultPrevented)
// happy-dom would try to follow it.
event.preventDefault()
}
function AccountPage() {
const location = useLocation()
const from = (location.state as { from?: string } | null)?.from ?? 'none'
return <p>account page from {from}</p>
}
const account = () =>
screen.getAllByRole('link', {
@@ -326,9 +360,9 @@ describe('AppLayout and a started move on the drawing (R-12)', () => {
screen.getByRole('dialog', { name: 'Abandonner le déplacement ?' }),
).toBeInTheDocument()
expect(screen.queryByText(fr['leave.title'])).toBeNull()
expect(screen.queryByText('account page')).toBeNull()
expect(screen.queryByText(/account page/)).toBeNull()
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
expect(await screen.findByText('account page')).toBeInTheDocument()
expect(await screen.findByText(/account page/)).toBeInTheDocument()
})
it('with sheet changes too: the drawing question, then the leave question', async () => {
@@ -340,7 +374,7 @@ describe('AppLayout and a started move on the drawing (R-12)', () => {
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()
expect(screen.queryByText(/account page/)).toBeNull()
})
it('a started redraw asks « Abandonner le nouveau tracé ? » before a link', async () => {
@@ -352,19 +386,37 @@ describe('AppLayout and a started move on the drawing (R-12)', () => {
expect(
screen.getByRole('dialog', { name: 'Abandonner le nouveau tracé ?' }),
).toBeInTheDocument()
expect(screen.queryByText('account page')).toBeNull()
expect(screen.queryByText(/account page/)).toBeNull()
await user.click(screen.getByRole('button', { name: 'Abandonner' }))
expect(await screen.findByText('account page')).toBeInTheDocument()
expect(await screen.findByText(/account page/)).toBeInTheDocument()
expect(useAnnotationWork.getState().mode).toBeNull()
})
it('a move with nothing started leaves at once and ends', async () => {
it('a move with nothing started is not at stake: the link goes at once', 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()
expect(await screen.findByText(/account page/)).toBeInTheDocument()
})
it("with nothing at stake a page link is the router's own: its state reaches the page", async () => {
const user = userEvent.setup()
mountDraft()
await user.click(screen.getByRole('link', { name: 'page link' }))
expect(
await screen.findByText('account page from draft'),
).toBeInTheDocument()
})
it('a download link is never taken, even with a started move', async () => {
const user = userEvent.setup()
mountDraft()
started()
downloads.length = 0
await user.click(screen.getByRole('link', { name: 'download link' }))
expect(downloads).toEqual([false])
expect(screen.queryByRole('dialog')).toBeNull()
})
})