Files
faunosteo/docs/faunosteo-audit/relevant-files/tests/unit/components.FindingsTreePicker.test.tsx
T
bastien e471d41c6b docs(docs): add anatomical audit of existing SVG, catalog and report systems
Read-only technical audit before the SaaS redesign: 13 reports,
a logical anatomical-sheet JSON built from real horse data, verbatim
copies of the 52 relevant source files, and a self-contained ZIP.
2026-10-06 23:51:35 +02:00

299 lines
8.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import FindingsTreePicker from '@/components/observation/FindingsTreePicker'
import type { ZoneFindings } from '@/domain/schema/findings'
import type { Finding } from '@/domain/schema/observation'
import { useLocaleStore } from '@/state/localeStore'
beforeEach(() => {
// Navigator-detected default may land on EN in happy-dom; force FR so the
// FR-label assertions in this file remain stable regardless of the runner's
// navigator.language.
useLocaleStore.setState({ locale: 'fr' })
})
const TREE: ZoneFindings = {
id: 'sphenoid',
name_fr: 'Sphénoïde',
name_en: 'Sphenoid',
categories: [
{
id: 'dysfunctions',
name_fr: 'Dysfonctions',
name_en: 'Dysfunctions',
children: [
{
id: 'flexion',
name_fr: 'Flexion',
name_en: 'Flexion',
description_fr: 'Description test',
},
{
id: 'extension',
name_fr: 'Extension',
name_en: 'Extension',
requires_vet_referral: true,
},
],
},
],
}
describe('FindingsTreePicker', () => {
it('renders the zone name in the initial breadcrumb', () => {
render(
<FindingsTreePicker
zoneId="sphenoid"
tree={TREE}
selected={[]}
onChange={() => {}}
/>,
)
expect(screen.getByText('Sphénoïde')).toBeInTheDocument()
})
it('drills into a category and shows leaves', async () => {
render(
<FindingsTreePicker
zoneId="sphenoid"
tree={TREE}
selected={[]}
onChange={() => {}}
/>,
)
await userEvent.click(screen.getByLabelText('Ouvrir Dysfonctions'))
expect(screen.getByLabelText('Flexion')).toBeInTheDocument()
expect(screen.getByLabelText('Extension')).toBeInTheDocument()
})
it('emits a Finding with snapshot labels on leaf select', async () => {
const onChange = vi.fn<(next: Finding[]) => void>()
render(
<FindingsTreePicker
zoneId="sphenoid"
tree={TREE}
selected={[]}
onChange={onChange}
/>,
)
await userEvent.click(screen.getByLabelText('Ouvrir Dysfonctions'))
await userEvent.click(screen.getByLabelText('Flexion'))
expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange.mock.calls[0][0]).toEqual([
{
zoneId: 'sphenoid',
path: ['dysfunctions', 'flexion'],
labelFrSnapshot: 'Flexion',
labelEnSnapshot: 'Flexion',
},
])
})
it('flags vet-referral leaves with requiresVetReferral on selection', async () => {
const onChange = vi.fn<(next: Finding[]) => void>()
render(
<FindingsTreePicker
zoneId="sphenoid"
tree={TREE}
selected={[]}
onChange={onChange}
/>,
)
await userEvent.click(screen.getByLabelText('Ouvrir Dysfonctions'))
await userEvent.click(screen.getByLabelText('Extension'))
expect(onChange.mock.calls[0][0][0].requiresVetReferral).toBe(true)
})
it('deselects a previously selected leaf', async () => {
const onChange = vi.fn<(next: Finding[]) => void>()
const selected: Finding[] = [
{
zoneId: 'sphenoid',
path: ['dysfunctions', 'flexion'],
labelFrSnapshot: 'Flexion',
labelEnSnapshot: 'Flexion',
},
]
render(
<FindingsTreePicker
zoneId="sphenoid"
tree={TREE}
selected={selected}
onChange={onChange}
/>,
)
await userEvent.click(screen.getByLabelText('Ouvrir Dysfonctions'))
await userEvent.click(screen.getByLabelText('Flexion'))
expect(onChange).toHaveBeenCalledWith([])
})
it('navigates back to parent via the Retour button', async () => {
render(
<FindingsTreePicker
zoneId="sphenoid"
tree={TREE}
selected={[]}
onChange={() => {}}
/>,
)
await userEvent.click(screen.getByLabelText('Ouvrir Dysfonctions'))
expect(screen.getByLabelText('Flexion')).toBeInTheDocument()
await userEvent.click(screen.getByLabelText('Retour au niveau précédent'))
expect(screen.getByLabelText('Ouvrir Dysfonctions')).toBeInTheDocument()
})
})
const DEEP_TREE: ZoneFindings = {
id: 'atm',
name_fr: 'ATM',
name_en: 'TMJ',
categories: [
{
id: 'dysf',
name_fr: 'Dysfonctions ostéopathiques de l\'ATM',
name_en: 'Osteopathic dysfunctions of the TMJ',
children: [
{
id: 'tensions',
name_fr: 'Tensions des muscles masticateurs',
name_en: 'Masticatory muscle tensions',
children: [
{
id: 'temporal',
name_fr: 'Tension du temporal',
name_en: 'Temporal tension',
},
{
id: 'pterygo',
name_fr: 'Tension des ptérygoïdiens',
name_en: 'Pterygoid tension',
},
{
id: 'masseter',
name_fr: 'Tension des masséters',
name_en: 'Masseter tension',
},
],
},
{
id: 'capsule',
name_fr: 'Dysfonction capsulaire',
name_en: 'Capsular dysfunction',
children: [
{ id: 'cap-ant', name_fr: 'Antérieure', name_en: 'Anterior' },
{ id: 'cap-post', name_fr: 'Postérieure', name_en: 'Posterior' },
],
},
],
},
],
}
describe('FindingsTreePicker selection counts on intermediate nodes', () => {
const selected: Finding[] = [
{
zoneId: 'atm',
path: ['dysf', 'tensions', 'temporal'],
labelFrSnapshot: 'Tension du temporal',
labelEnSnapshot: 'Temporal tension',
},
{
zoneId: 'atm',
path: ['dysf', 'tensions', 'pterygo'],
labelFrSnapshot: 'Tension des ptérygoïdiens',
labelEnSnapshot: 'Pterygoid tension',
},
]
it('shows a count badge of 2 on the top-level category when two descendant leaves are selected', () => {
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={selected}
onChange={() => {}}
/>,
)
// Root view: only the top-level "Dysfonctions..." category is visible.
expect(
screen.getByLabelText('2 constats sélectionnés'),
).toBeInTheDocument()
})
it('shows a count badge of 2 on the sub-category one level deeper', async () => {
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={selected}
onChange={() => {}}
/>,
)
await userEvent.click(
screen.getByLabelText('Ouvrir Dysfonctions ostéopathiques de l\'ATM'),
)
// Now we see "Tensions des muscles masticateurs" (2 leaves selected)
// and "Dysfonction capsulaire" (0 leaves selected — no badge).
expect(
screen.getByLabelText('2 constats sélectionnés'),
).toBeInTheDocument()
})
it('does not show a badge for siblings without selected descendants', async () => {
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={selected}
onChange={() => {}}
/>,
)
await userEvent.click(
screen.getByLabelText('Ouvrir Dysfonctions ostéopathiques de l\'ATM'),
)
// Capsular dysfunction has no selections — the button should not carry a badge label.
const capsular = screen.getByLabelText('Ouvrir Dysfonction capsulaire')
expect(capsular.textContent ?? '').not.toMatch(/\d/)
})
it('shows a singular badge label for exactly one selection', () => {
const oneSelected: Finding[] = [selected[0]]
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={oneSelected}
onChange={() => {}}
/>,
)
expect(
screen.getByLabelText('1 constat sélectionné'),
).toBeInTheDocument()
})
it('ignores selections from other zones when counting', () => {
const cross: Finding[] = [
{
zoneId: 'sphenoid',
path: ['dysf', 'tensions', 'temporal'],
labelFrSnapshot: 'Tension du temporal',
labelEnSnapshot: 'Temporal tension',
},
]
render(
<FindingsTreePicker
zoneId="atm"
tree={DEEP_TREE}
selected={cross}
onChange={() => {}}
/>,
)
// No badge should appear on the ATM tree — the lone selected finding
// belongs to a different zone.
expect(
screen.queryByLabelText(/constat sélectionné/),
).not.toBeInTheDocument()
})
})