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 type { Finding } from '@faunosteo/domain' import FindingsTreePicker from '@/components/observation/FindingsTreePicker' 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) describe('FindingsTreePicker on a sheet finding zone', () => { it('drills down the hip zone and selects a leaf as a v3 finding with snapshots and side', async () => { const user = userEvent.setup() const onChange = vi.fn() render( , ) expect(screen.getByText('Hanche (coxo-fémorale)')).toBeInTheDocument() await user.click( screen.getByRole('button', { name: 'Ouvrir Dysfonctions ostéopathiques', }), ) await user.click( screen.getByRole('button', { name: 'Ouvrir Restrictions de mobilité articulaire', }), ) await user.click( screen.getByRole('checkbox', { name: 'Restriction en flexion' }), ) expect(onChange).toHaveBeenCalledWith([ { findingZoneId: 'hip', path: ['hip-dysfunctions', 'articular', 'restriction-flexion'], labelFrSnapshot: 'Restriction en flexion', labelEnSnapshot: 'Flexion restriction', side: 'left', }, ]) }) it('shows descriptions in the active locale, the vet marker, counts, and unselects', async () => { const user = userEvent.setup() useLocaleStore.setState({ locale: 'en' }) const selected: Finding[] = [ { findingZoneId: 'hip', path: ['hip-dysfunctions', 'articular', 'hypermobility'], labelFrSnapshot: 'x', labelEnSnapshot: 'x', requiresVetReferral: true, }, ] const onChange = vi.fn() render( , ) expect(screen.getByLabelText('1 finding selected')).toBeInTheDocument() await user.click( screen.getByRole('button', { name: 'Open Osteopathic dysfunctions' }), ) await user.click( screen.getByRole('button', { name: 'Open Articular mobility restrictions', }), ) expect( screen.getByText(/Perceived laxity may indicate a ligament injury/), ).toBeInTheDocument() expect( screen.getAllByLabelText('Refer to a veterinarian').length, ).toBeGreaterThan(0) const box = screen.getByRole('checkbox', { name: 'Hypermobility / perceived laxity', }) expect(box).toBeChecked() await user.click(box) expect(onChange).toHaveBeenCalledWith([]) await user.click( screen.getByRole('button', { name: 'Back to previous level' }), ) expect( screen.getByRole('button', { name: 'Open Articular mobility restrictions', }), ).toBeInTheDocument() }) it('flags a vet-referral leaf and omits the key on other leaves', async () => { const user = userEvent.setup() const onChange = vi.fn() render( , ) await user.click( screen.getByRole('button', { name: 'Ouvrir Dysfonctions ostéopathiques', }), ) await user.click( screen.getByRole('button', { name: 'Ouvrir Restrictions de mobilité articulaire', }), ) await user.click( screen.getByRole('checkbox', { name: 'Hypermobilité / laxité perçue' }), ) expect(onChange.mock.calls[0]![0][0].requiresVetReferral).toBe(true) onChange.mockClear() await user.click( screen.getByRole('checkbox', { name: 'Hypomobilité globale' }), ) expect(onChange.mock.calls[0]![0][0]).not.toHaveProperty( 'requiresVetReferral', ) }) describe('count badges and scoping', () => { const finding = ( path: string[], extra: Partial = {}, ): Finding => ({ findingZoneId: 'hip', path, labelFrSnapshot: 'x', labelEnSnapshot: 'x', side: 'left', ...extra, }) const base = [ finding(['hip-dysfunctions', 'articular', 'hypomobility']), finding(['hip-dysfunctions', 'articular', 'hypermobility']), finding(['hip-dysfunctions', 'muscular', 'trigger-points']), ] function renderPicker(selected: Finding[]) { render( , ) } it('counts selected findings per level, none on a clean sibling', async () => { const user = userEvent.setup() renderPicker(base) expect( screen.getByLabelText('3 constats sélectionnés'), ).toBeInTheDocument() expect(screen.getAllByLabelText(/constats? sélectionnés?$/)).toHaveLength( 1, ) await user.click( screen.getByRole('button', { name: 'Ouvrir Dysfonctions ostéopathiques', }), ) expect( screen.getByLabelText('2 constats sélectionnés'), ).toBeInTheDocument() expect(screen.getByLabelText('1 constat sélectionné')).toBeInTheDocument() expect(screen.getAllByLabelText(/constats? sélectionnés?$/)).toHaveLength( 2, ) }) it('ignores findings of another zone or another side', async () => { const user = userEvent.setup() renderPicker([ finding(['hip-dysfunctions', 'articular', 'hypomobility'], { findingZoneId: 'stifle', }), finding(['hip-dysfunctions', 'articular', 'hypomobility'], { side: 'right', }), ]) expect(screen.queryByLabelText(/sélectionnés?$/)).toBeNull() await user.click( screen.getByRole('button', { name: 'Ouvrir Dysfonctions ostéopathiques', }), ) await user.click( screen.getByRole('button', { name: 'Ouvrir Restrictions de mobilité articulaire', }), ) expect( screen.getByRole('checkbox', { name: 'Hypomobilité globale' }), ).not.toBeChecked() }) }) it('the findings list carries no permanent compositing hint (text stays crisp)', () => { render( {}} />, ) const ul = document.querySelector('ul') as HTMLElement expect(ul.getAttribute('style') ?? '').not.toContain('will-change') expect(ul.style.willChange).toBe('') }) })