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( {}} />, ) 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() }) }) 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( {}} />, ) // 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( {}} />, ) 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( {}} />, ) 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( {}} />, ) 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( {}} />, ) // 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() }) })