Files
faunosteo/tests/unit/components.VertebraPicker.test.tsx
T
Bastien ChanotandClaude b3bd75d2e2 feat(editor): VertebraPicker preselects region tab inferred from zone label
When opened from a spine zone, the picker now lands on the matching
region tab instead of defaulting to Cervicales every time. Inference is
substring-based on the FR label first, EN as fallback (cervic→C,
thorac/dors→T, lombair/lumbar→L, sacral/sacre→S, caudal/coccyg→Cd),
case- and accent-insensitive. An existing selection still wins over
the inferred region so re-opening lands where the user left off.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-05-19 05:09:57 +02:00

101 lines
3.1 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import VertebraPicker, {
regionFromLabel,
} from '@/components/observation/VertebraPicker'
import { EQUINE_VERTEBRAL_FORMULA } from '@/domain/species/equine/vertebrae'
describe('VertebraPicker', () => {
it('renders region tabs based on the formula', () => {
render(
<VertebraPicker
open
onClose={() => {}}
onConfirm={() => {}}
initial={[]}
formula={EQUINE_VERTEBRAL_FORMULA}
title="Vertèbres cervicales"
/>,
)
expect(screen.getByRole('tab', { name: /Cervicales/ })).toBeInTheDocument()
expect(screen.getByRole('tab', { name: /Thoraciques/ })).toBeInTheDocument()
})
it('toggles a chip and confirms', async () => {
const onConfirm = vi.fn()
render(
<VertebraPicker
open
onClose={() => {}}
onConfirm={onConfirm}
initial={[]}
formula={EQUINE_VERTEBRAL_FORMULA}
title="Vertèbres thoraciques"
/>,
)
await userEvent.click(screen.getByRole('tab', { name: /Thoraciques/ }))
await userEvent.click(screen.getByRole('button', { name: 'T12' }))
await userEvent.click(screen.getByRole('button', { name: 'Valider' }))
expect(onConfirm).toHaveBeenCalledWith([{ region: 'T', index: 12 }])
})
it('preselects the tab matching initialRegion', () => {
render(
<VertebraPicker
open
onClose={() => {}}
onConfirm={() => {}}
initial={[]}
formula={EQUINE_VERTEBRAL_FORMULA}
title="Vertèbres lombaires"
initialRegion="L"
/>,
)
expect(screen.getByRole('tab', { name: /Lombaires/ })).toHaveAttribute(
'aria-selected',
'true',
)
})
it('region of first existing selection wins over initialRegion', () => {
render(
<VertebraPicker
open
onClose={() => {}}
onConfirm={() => {}}
initial={[{ region: 'S', index: 2 }]}
formula={EQUINE_VERTEBRAL_FORMULA}
title="Vertèbres sacrales"
initialRegion="C"
/>,
)
expect(screen.getByRole('tab', { name: /Sacrales/ })).toHaveAttribute(
'aria-selected',
'true',
)
})
})
describe('regionFromLabel', () => {
it('matches FR roots (accent-insensitive)', () => {
expect(regionFromLabel('Vertèbres cervicales')).toBe('C')
expect(regionFromLabel('Vertèbres thoraciques')).toBe('T')
expect(regionFromLabel('Vertèbres lombaires')).toBe('L')
expect(regionFromLabel('Vertèbres sacrales')).toBe('S')
expect(regionFromLabel('Vertèbres caudales')).toBe('Cd')
})
it('matches EN roots', () => {
expect(regionFromLabel('Cervical Vertebrae')).toBe('C')
expect(regionFromLabel('Thoracic Vertebrae')).toBe('T')
expect(regionFromLabel('Lumbar Vertebrae')).toBe('L')
expect(regionFromLabel('Coccygeal Vertebrae')).toBe('Cd')
})
it('returns undefined when no root matches', () => {
expect(regionFromLabel('Épaule')).toBeUndefined()
expect(regionFromLabel('')).toBeUndefined()
})
})