feat(app): Aller à selects the element in its tab, reload after submit

This commit is contained in:
bchanot committed 2026-11-04 14:06:09 +01:00
1 parent 2a54fa4c78
commit 4f223e04cd
24 files changed
+473 -66

No files matched your search

@@ -0,0 +1,194 @@
/** Lot J fix: « Aller à … » seeds each tab's own selection once. */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, render, screen } from '@testing-library/react'
import type {
AnatomicalSheetDocument,
ElementKey,
} from '@faunosteo/core-wasm/types'
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 { issueTarget } from '@/editor/verification'
import { useLocaleStore } from '@/state/localeStore'
import { edit, insert, point } from '../helpers/editorFixtures'
import {
baseText,
openReady,
resetStore,
store,
} from '../helpers/editorHarness'
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
vi.mock('@faunosteo/supabase', async (orig) => ({
...(await orig<typeof import('@faunosteo/supabase')>()),
getSheetVersion: vi.fn(),
downloadSheetDrawing: vi.fn(),
saveSheetDraft: vi.fn(),
getOfficialSheetAdmin: vi.fn(),
submitSheetReview: vi.fn(),
}))
const base = { readOnly: false, onSelectTab: vi.fn(), reloadSheet: vi.fn() }
const doc = (): AnatomicalSheetDocument =>
JSON.parse(store.getState().documentText)
const name = (n: string) => ({ fr: n, en: n })
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
afterEach(() => {
cleanup()
resetStore()
})
function findingsText(): string {
let text = insert(baseText(), {
kind: 'zone',
value: { id: 'zone-a', name: name('Zone A'), categories: [] },
})
text = edit(text, {
op: 'insert',
parent: { kind: 'zone', id: 'zone-a' },
element: {
kind: 'finding_category',
value: {
id: 'fcat-a',
kind: 'dysfunction',
name: name('Cat A'),
description: {},
reference_ids: [],
review_status: 'to_confirm',
children: [],
},
},
})
return edit(text, {
op: 'insert',
parent: { kind: 'finding', zone_id: 'zone-a', path: ['fcat-a'] },
element: {
kind: 'finding_node',
value: {
id: 'node-a',
name: name('Nœud A'),
description: {},
requires_vet_referral: false,
reference_ids: [],
review_status: 'to_confirm',
children: [],
},
},
})
}
describe('focus requests', () => {
it('Constats: expands to the finding, selects it and shows its detail', async () => {
await openReady({ text: findingsText() })
const key: ElementKey = {
kind: 'finding',
zone_id: 'zone-a',
path: ['fcat-a', 'node-a'],
}
render(<FindingsTab {...base} focus={{ key, serial: 1 }} />)
const item = await screen.findByRole('treeitem', { name: /Nœud A/ })
expect(item.getAttribute('aria-selected')).toBe('true')
})
it('Annotations: selects the annotation of the request', async () => {
const text = insert(baseText(), point('anno-b', 20, 20, 3))
await openReady({ text })
render(
<AnnotationsTab
{...base}
focus={{ key: { kind: 'annotation', id: 'anno-b' }, serial: 1 }}
/>,
)
expect(await screen.findAllByText(/anno-b|Sélection/)).not.toHaveLength(0)
})
it('Références and Vertèbres: focus the named row', async () => {
let text = insert(baseText(), {
kind: 'reference',
value: {
id: 'ref-a',
type: 'book',
citation: 'Alpha',
url: null,
doi: null,
},
})
text = insert(text, {
kind: 'region',
value: {
code: 'C',
name: name('Cervicales'),
count: 7,
count_range: null,
},
})
await openReady({ text })
render(
<ReferencesTab
{...base}
focus={{ key: { kind: 'reference', id: 'ref-a' }, serial: 1 }}
/>,
)
expect(document.activeElement?.getAttribute('data-focus-key')).toContain(
'ref-a',
)
cleanup()
render(
<VertebraeTab
{...base}
focus={{ key: { kind: 'region', code: 'C' }, serial: 1 }}
/>,
)
expect(document.activeElement?.getAttribute('data-focus-key')).toContain(
'"C"',
)
})
it('consumes a serial once', async () => {
const text = insert(baseText(), {
kind: 'reference',
value: {
id: 'ref-a',
type: 'book',
citation: 'Alpha',
url: null,
doi: null,
},
})
await openReady({ text })
const focus = {
key: { kind: 'reference', id: 'ref-a' } as ElementKey,
serial: 1,
}
const view = render(<ReferencesTab {...base} focus={focus} />)
;(document.activeElement as HTMLElement).blur()
view.rerender(<ReferencesTab {...base} focus={focus} />)
expect(document.activeElement?.getAttribute('data-focus-key')).toBeNull()
})
})
describe('issueTarget', () => {
it('resolves nested finding paths, regions and references', async () => {
await openReady({ text: findingsText() })
const d = doc()
const path = 'finding_zones[0].categories[0].children[0].reference_ids[1]'
expect(issueTarget(d, path, 'fr')).toMatchObject({
tab: 'findings',
key: { kind: 'finding', zone_id: 'zone-a', path: ['fcat-a', 'node-a'] },
name: 'Nœud A',
})
expect(issueTarget(d, 'finding_zones[0]', 'fr')?.key).toEqual({
kind: 'zone',
id: 'zone-a',
})
expect(
issueTarget(d, 'vertebral_config.regions[3].count_range', 'fr'),
).toMatchObject({
tab: 'vertebrae',
key: null,
})
expect(issueTarget(d, 'nothing', 'fr')).toBeNull()
})
})
@@ -29,7 +29,8 @@ vi.mock('@faunosteo/supabase', async (orig) => ({
const onSelectTab = vi.fn()
const reloadSheet = vi.fn()
const props = { readOnly: false, onSelectTab, reloadSheet }
const onGoTo = vi.fn()
const props = { readOnly: false, onSelectTab, onGoTo, reloadSheet }
const SUBMIT = 'Soumettre à la revue'
/** An annotation without geometry: a blocking error of the core. */
@@ -48,6 +49,7 @@ afterEach(() => {
cleanup()
resetStore()
onSelectTab.mockReset()
onGoTo.mockReset()
reloadSheet.mockReset()
})
@@ -77,9 +79,12 @@ describe('Vérification panel', () => {
expect(screen.getAllByText('Sans tracé')).toHaveLength(2)
expect(screen.getByText(/n'a pas de géométrie/)).toBeTruthy()
await user.click(
screen.getAllByRole('button', { name: 'Aller à Annotations' })[0],
screen.getAllByRole('button', { name: 'Aller à Sans tracé' })[0],
)
expect(onSelectTab).toHaveBeenCalledWith('annotations')
expect(onGoTo).toHaveBeenCalledWith('annotations', {
kind: 'annotation',
id: 'anno-bare',
})
expect(screen.queryByText('Aucun problème détecté')).toBeNull()
})
@@ -101,9 +106,9 @@ describe('Vérification panel', () => {
render(<VerificationTab {...props} />)
expect(screen.getByText('1 blocking errors')).toBeTruthy()
expect(screen.getByText(/This annotation has no geometry/)).toBeTruthy()
expect(
screen.getAllByRole('button', { name: 'Go to Annotations' }),
).toHaveLength(3)
expect(screen.getAllByRole('button', { name: 'Go to Bare' })).toHaveLength(
2,
)
})
it('hides the submission when read-only', async () => {
@@ -214,3 +219,21 @@ describe('Issue messages', () => {
expect(getDict('fr')[key]).toBeTruthy()
})
})
describe('Submission after the tab is gone', () => {
it('still reloads the sheet when the tab unmounted during the await', async () => {
await openReady()
let resolve: (id: never) => void = () => undefined
sb.submitSheetReview.mockReturnValue(new Promise((r) => (resolve = r)))
const user = userEvent.setup()
const view = render(<VerificationTab {...props} />)
await user.type(
screen.getByLabelText('Note de soumission (obligatoire)'),
'Prête',
)
await user.click(screen.getByRole('button', { name: SUBMIT }))
view.unmount()
await act(async () => resolve('review-1' as never))
await waitFor(() => expect(reloadSheet).toHaveBeenCalled())
})
})