diff --git a/src/state/reportStore.ts b/src/state/reportStore.ts index 34a62ec9..ea197ea5 100644 --- a/src/state/reportStore.ts +++ b/src/state/reportStore.ts @@ -1,6 +1,8 @@ import { create } from 'zustand' import { ReportSchema, type Report } from '@/domain/schema/report' import type { Observation } from '@/domain/schema/observation' +import type { Practitioner } from '@/domain/schema/practitioner' +import type { Animal, Owner } from '@/domain/schema/animal' import { generateId } from '@/lib/id' import { nowIso } from '@/lib/dates' @@ -15,6 +17,9 @@ export interface ReportStore { moveObservationDown: (index: number) => void updateObservation: (index: number, obs: Observation) => void setActiveObservation: (index: number) => void + setPractitioner: (next: Practitioner) => void + setAnimal: (next: Animal) => void + setOwner: (next: Owner) => void setConclusion: (text: string) => void setRecommendations: (text: string) => void setNextAppointment: (next: Report['nextAppointment']) => void @@ -110,6 +115,15 @@ export const useReportStore = create((set) => ({ setActiveObservation: (index) => set({ activeObservationIndex: index }), + setPractitioner: (next) => + set((s) => (s.report ? { report: { ...s.report, practitioner: next } } : s)), + + setAnimal: (next) => + set((s) => (s.report ? { report: { ...s.report, animal: next } } : s)), + + setOwner: (next) => + set((s) => (s.report ? { report: { ...s.report, owner: next } } : s)), + setConclusion: (text) => set((s) => (s.report ? { report: { ...s.report, conclusion: text } } : s)), diff --git a/tests/unit/state.reportStore.setters.test.ts b/tests/unit/state.reportStore.setters.test.ts new file mode 100644 index 00000000..74fa37a1 --- /dev/null +++ b/tests/unit/state.reportStore.setters.test.ts @@ -0,0 +1,56 @@ +import { describe, it, expect, beforeEach } from 'vitest' +import { useReportStore } from '@/state/reportStore' + +const draft = { + date: '2026-05-18', + practitioner: { name: 'Dr. Dupont' }, + animal: { speciesId: 'equine', name: 'Mistral' }, + owner: { name: 'Jean Martin' }, + observations: [], + conclusion: '', + recommendations: '', +} + +beforeEach(() => { + useReportStore.getState().reset() +}) + +describe('reportStore identity setters', () => { + it('setPractitioner replaces the practitioner block', () => { + useReportStore.getState().initReport(draft) + useReportStore.getState().setPractitioner({ + name: 'Dr. Curie', + cabinet: 'Clinique du Pré', + contact: 'contact@example.test', + }) + const p = useReportStore.getState().report!.practitioner + expect(p.name).toBe('Dr. Curie') + expect(p.cabinet).toBe('Clinique du Pré') + expect(p.contact).toBe('contact@example.test') + }) + + it('setAnimal replaces the animal block', () => { + useReportStore.getState().initReport(draft) + useReportStore.getState().setAnimal({ + speciesId: 'equine', + name: 'Tempête', + breed: 'Pur-sang', + }) + const a = useReportStore.getState().report!.animal + expect(a.name).toBe('Tempête') + expect(a.breed).toBe('Pur-sang') + }) + + it('setOwner replaces the owner block', () => { + useReportStore.getState().initReport(draft) + useReportStore.getState().setOwner({ name: 'Marie Petit' }) + expect(useReportStore.getState().report!.owner.name).toBe('Marie Petit') + }) + + it('setters are no-ops when no report is loaded', () => { + useReportStore.getState().setPractitioner({ name: 'Ghost' }) + useReportStore.getState().setAnimal({ speciesId: 'equine', name: 'Ghost' }) + useReportStore.getState().setOwner({ name: 'Ghost' }) + expect(useReportStore.getState().report).toBeNull() + }) +})