fix(app): a focus request is consumed once, not replayed on tab return

This commit is contained in:
bchanot committed 2026-11-04 14:09:30 +01:00
1 parent 4f223e04cd
commit 3d589e1556
14 files changed
+99 -30

No files matched your search

@@ -1,6 +1,8 @@
/** Lot J fix: « Aller à … » seeds each tab's own selection once. */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { useState } from 'react'
import { cleanup, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type {
AnatomicalSheetDocument,
ElementKey,
@@ -9,6 +11,7 @@ import FindingsTab from '@/components/editor/tabs/FindingsTab'
import ReferencesTab from '@/components/editor/tabs/ReferencesTab'
import VertebraeTab from '@/components/editor/tabs/VertebraeTab'
import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab'
import { FocusConsumedContext, type FocusRequest } from '@/editor/focusRequest'
import { issueTarget } from '@/editor/verification'
import { useLocaleStore } from '@/state/localeStore'
import { edit, insert, point } from '../helpers/editorFixtures'
@@ -192,3 +195,51 @@ describe('issueTarget', () => {
expect(issueTarget(d, 'nothing', 'fr')).toBeNull()
})
})
/** The route's contract: one tab mounted at a time, the request cleared once consumed. */
function Harness() {
const [tab, setTab] = useState<'references' | 'vertebrae'>('references')
const [focus, setFocus] = useState<FocusRequest | undefined>({
key: { kind: 'reference', id: 'ref-a' },
serial: 1,
})
const clear = (serial: number) =>
setFocus((prev) => (prev?.serial === serial ? undefined : prev))
const Body = tab === 'references' ? ReferencesTab : VertebraeTab
return (
<FocusConsumedContext value={clear}>
<button
onClick={() =>
setTab(tab === 'references' ? 'vertebrae' : 'references')
}
>
switch
</button>
<Body {...base} focus={focus} />
</FocusConsumedContext>
)
}
describe('a consumed request', () => {
it('is not replayed when the tab is left and reopened', async () => {
const text = insert(baseText(), {
kind: 'reference',
value: {
id: 'ref-a',
type: 'book',
citation: 'Alpha',
url: null,
doi: null,
},
})
await openReady({ text })
const user = userEvent.setup()
render(<Harness />)
expect(document.activeElement?.getAttribute('data-focus-key')).toContain(
'ref-a',
)
await user.click(screen.getByText('switch'))
await user.click(screen.getByText('switch'))
expect(document.activeElement?.getAttribute('data-focus-key')).toBeNull()
})
})