import { beforeAll, describe, expect, it, vi, afterEach, beforeEach, } from 'vitest' import { cleanup, render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import VertebraPicker from '@/components/observation/VertebraPicker' import { useLocaleStore } from '@/state/localeStore' import type { SheetIndex } from '@/services/sheetIndex' import { loadTestSheet } from '../helpers/sheet' let sheet: SheetIndex beforeAll(async () => { sheet = await loadTestSheet() }) beforeEach(() => useLocaleStore.setState({ locale: 'fr' })) afterEach(cleanup) function mount(initialRegion?: string, onConfirm = vi.fn()) { render( undefined} onConfirm={onConfirm} initial={[{ region: 'C', index: 3 }]} regions={sheet.regions()} title="Vertèbres" initialRegion={initialRegion} />, ) return onConfirm } describe('VertebraPicker on the sheet regions', () => { it('shows one tab per region of the sheet, named from the document, opening on the asked region', () => { mount('L') const tabs = screen.getAllByRole('tab').map((t) => t.textContent) expect(tabs).toEqual([ 'Cervicales', 'Thoraciques', 'Lombaires', 'Sacrales', 'Caudales', ]) expect(screen.getByRole('tab', { name: 'Lombaires' })).toHaveAttribute( 'aria-selected', 'true', ) expect(screen.getByText('L6')).toBeInTheDocument() expect(screen.queryByText('L7')).toBeNull() }) it('shows the usual count and extends to the documented maximum (D6)', async () => { const user = userEvent.setup() mount('Cd') expect(screen.getByText('Cd18')).toBeInTheDocument() expect(screen.queryByText('Cd21')).toBeNull() await user.click( screen.getByRole('button', { name: "Afficher jusqu'à 21" }), ) expect(screen.getByText('Cd21')).toBeInTheDocument() expect( screen.getByText(/Nombre usuel : 18 ; variation documentée : 15 à 21/), ).toBeInTheDocument() await user.click(screen.getByRole('tab', { name: 'Cervicales' })) expect(screen.queryByRole('button', { name: /Afficher jusqu/ })).toBeNull() }) it('toggles vertebrae and confirms the selection across regions', async () => { const user = userEvent.setup() const onConfirm = mount('C') await user.click(screen.getByText('C5')) await user.click(screen.getByRole('tab', { name: 'Thoraciques' })) await user.click(screen.getByText('T2')) await user.click(screen.getByRole('button', { name: 'Valider' })) expect(onConfirm).toHaveBeenCalledWith([ { region: 'C', index: 3 }, { region: 'C', index: 5 }, { region: 'T', index: 2 }, ]) }) })