fix(editor): ask only the layers a Back removes, clear the sheet first

This commit is contained in:
bchanot committed 2026-11-14 18:47:24 +01:00
1 parent 526aecb40e
commit 2ad720b34e
5 files changed
+193 -30

No files matched your search

@@ -979,3 +979,94 @@ describe('citing references lives in the edit form (owner item 1)', () => {
).toBeVisible()
})
})
describe('the citations sheet over a dirty edit form (final review rev 4, item 1)', () => {
const question = 'Abandonner les modifications ?'
const dialogs = () => [...document.querySelectorAll('dialog[open]')]
const refForm = () =>
dialogs().find((d) => d.querySelector('#reference-form-citation'))
let api: NavigationStub
beforeEach(() => {
api = new NavigationStub()
Object.defineProperty(window, 'navigation', {
value: api,
configurable: true,
})
})
afterEach(() => {
Reflect.deleteProperty(window, 'navigation')
})
/** Node A in edit with Beta cited in the draft, its sheet open over the form. */
async function sheetOverDirtyForm() {
const user = await mount(1280, citingText())
await openFinding(user, /Node A/)
await modify(user)
await cite(user, 'Beta')
await user.click(inDialog().getByRole('button', { name: '2 références' }))
await waitFor(() => expect(panelOpen()).not.toBeNull())
await waitFor(() =>
expect(written()?.view.finding).toMatchObject({ citations: true }),
)
return user
}
const draftKept = () =>
expect(
inDialog().getByRole('button', { name: '2 références' }),
).toBeVisible()
it('a Back closes the clean sheet and asks nothing', async () => {
await sheetOverDirtyForm()
await back()
await waitFor(() => expect(panelOpen()).toBeNull())
expect(screen.queryByText(question)).toBeNull()
expect(written()?.view.finding).toMatchObject({ citations: false })
draftKept()
})
it('a Back from a clean reference dialog over the sheet closes it only', async () => {
const user = await sheetOverDirtyForm()
const sheet = within(panelOpen()!)
const head = sheet.getByRole('button', { name: /^Alpha/ })
if (head.getAttribute('aria-expanded') === 'false') await user.click(head)
const tile = within(head.closest('li')!)
await user.click(tile.getByRole('button', { name: 'Modifier' }))
await waitFor(() => expect(refForm()).toBeDefined())
await back()
await waitFor(() => expect(refForm()).toBeUndefined())
expect(panelOpen()).not.toBeNull()
expect(screen.queryByText(question)).toBeNull()
})
it('a jump of two entries inside the editor closes the sheet, then the form asks', async () => {
await sheetOverDirtyForm()
await act(async () => navigate(-2))
await waitFor(() => expect(panelOpen()).toBeNull())
expect(inDialog().getByText(question)).toBeVisible()
await waitFor(() =>
expect(findingDialog()!.contains(document.activeElement)).toBe(true),
)
expect(written()?.view.finding).toMatchObject({ citations: false })
draftKept()
})
it('a jump out of the editor closes the sheet, then the form asks in view', async () => {
await sheetOverDirtyForm()
const url = new URL('/app/admin/sheets', window.location.origin).href
const event = Object.assign(new Event('navigate', { cancelable: true }), {
navigationType: 'traverse',
destination: { url, key: 'list', sameDocument: true },
})
act(() => {
api.dispatchEvent(event)
})
expect(event.defaultPrevented).toBe(true)
await waitFor(() => expect(panelOpen()).toBeNull())
expect(inDialog().getByText(question)).toBeVisible()
await waitFor(() =>
expect(findingDialog()!.contains(document.activeElement)).toBe(true),
)
draftKept()
})
})
@@ -109,13 +109,26 @@ describe('a Back over the finding layers', () => {
it('a dirty layer keeps the page where it is: asked, the view pushed again', async () => {
const guard = vi.fn(() => ({ allowed: false }))
await openSheet(guard)
// The sheet is no form: the Back that closes it alone asks nothing (rev 4, item 1).
act(() => navigate(-1))
expect(guard).not.toHaveBeenCalled()
expect(layer()).toMatchObject({ citations: false })
const before = entry().id
act(() => navigate(-1))
expect(guard).toHaveBeenCalledWith(false)
expect(layer()).toMatchObject({ citations: true })
expect(layer()).toMatchObject({ citations: false })
expect(entry().id).not.toBe(before)
expect(entry().view.finding).toMatchObject({ citations: true })
expect(entry().below?.finding).toMatchObject({ citations: false })
expect(entry().view.finding).toMatchObject({ citations: false })
expect(entry().below?.finding).toBeNull()
})
it('a jump that removes a dirty dialog under the sheet closes the sheet, then asks', async () => {
const guard = vi.fn(() => ({ allowed: false }))
await openSheet(guard)
act(() => navigate(-2))
expect(guard).toHaveBeenCalledWith(false)
expect(layer()).toMatchObject({ citations: false })
expect(entry().view.finding).toMatchObject({ citations: false })
})
it('a modal that is no layer refuses the Back too', async () => {