113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
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.initReport', () => {
|
|
it('sets a report with version=1, id, and createdAt', () => {
|
|
useReportStore.getState().initReport(draft)
|
|
const r = useReportStore.getState().report!
|
|
expect(r.version).toBe(1)
|
|
expect(r.id.length).toBeGreaterThan(0)
|
|
expect(r.createdAt).toMatch(/^\d{4}-\d{2}-\d{2}T/)
|
|
expect(r.practitioner.name).toBe('Dr. Dupont')
|
|
})
|
|
|
|
it('resets activeObservationIndex to 0', () => {
|
|
useReportStore.getState().initReport(draft)
|
|
expect(useReportStore.getState().activeObservationIndex).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('reportStore.reset', () => {
|
|
it('clears the report', () => {
|
|
useReportStore.getState().initReport(draft)
|
|
useReportStore.getState().reset()
|
|
expect(useReportStore.getState().report).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('reportStore.addObservation', () => {
|
|
it('appends an empty observation and sets it active', () => {
|
|
useReportStore.getState().initReport(draft)
|
|
useReportStore.getState().addObservation()
|
|
const s = useReportStore.getState()
|
|
expect(s.report!.observations.length).toBe(1)
|
|
expect(s.activeObservationIndex).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('reportStore.updateObservation', () => {
|
|
it('replaces the observation at the given index', () => {
|
|
useReportStore.getState().initReport(draft)
|
|
useReportStore.getState().addObservation()
|
|
const next = {
|
|
...useReportStore.getState().report!.observations[0],
|
|
comment: 'patate',
|
|
}
|
|
useReportStore.getState().updateObservation(0, next)
|
|
expect(useReportStore.getState().report!.observations[0].comment).toBe('patate')
|
|
})
|
|
})
|
|
|
|
describe('reportStore.removeObservation', () => {
|
|
it('drops the observation at the given index', () => {
|
|
const store = useReportStore.getState()
|
|
store.initReport(draft)
|
|
store.addObservation()
|
|
store.addObservation()
|
|
store.removeObservation(0)
|
|
expect(useReportStore.getState().report!.observations.length).toBe(1)
|
|
})
|
|
|
|
it('clamps activeObservationIndex within bounds', () => {
|
|
const store = useReportStore.getState()
|
|
store.initReport(draft)
|
|
store.addObservation()
|
|
store.addObservation()
|
|
store.setActiveObservation(1)
|
|
store.removeObservation(1)
|
|
expect(useReportStore.getState().activeObservationIndex).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('reportStore.move up/down', () => {
|
|
it('moveObservationUp swaps with previous', () => {
|
|
const s = useReportStore.getState()
|
|
s.initReport(draft)
|
|
s.addObservation()
|
|
s.addObservation()
|
|
const idA = useReportStore.getState().report!.observations[0].id
|
|
const idB = useReportStore.getState().report!.observations[1].id
|
|
s.moveObservationUp(1)
|
|
const after = useReportStore.getState().report!.observations
|
|
expect(after[0].id).toBe(idB)
|
|
expect(after[1].id).toBe(idA)
|
|
})
|
|
|
|
it('moveObservationDown swaps with next', () => {
|
|
const s = useReportStore.getState()
|
|
s.initReport(draft)
|
|
s.addObservation()
|
|
s.addObservation()
|
|
const idA = useReportStore.getState().report!.observations[0].id
|
|
const idB = useReportStore.getState().report!.observations[1].id
|
|
s.moveObservationDown(0)
|
|
const after = useReportStore.getState().report!.observations
|
|
expect(after[0].id).toBe(idB)
|
|
expect(after[1].id).toBe(idA)
|
|
})
|
|
})
|