import { describe, it, expect, vi } 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' 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( {}} />, ) expect(screen.getByText('Sphénoïde')).toBeInTheDocument() }) it('drills into a category and shows leaves', async () => { render( {}} />, ) 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( , ) 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( , ) 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( , ) 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( {}} />, ) 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() }) })