/**
* Replacing the bilan in progress (R3 final checks): New report and the Home file pick ask
* first when a report is in memory or a draft sits in the slot (named, unloaded and
* unsaved variants); Annuler changes nothing, Ouvrir goes back to the editor, Remplacer
* proceeds; nothing in progress, no dialog.
*/
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import HomeRoute from '@/app/routes/HomeRoute'
import NewReportRoute from '@/app/routes/NewReportRoute'
import { DRAFT_KEY } from '@/state/persistence'
import { useReportStore } from '@/state/reportStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet, minimalReport } from '../helpers/sheet'
let sheet: SheetIndex
beforeAll(async () => {
sheet = await loadTestSheet()
})
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
sessionStorage.clear()
localStorage.clear()
useReportStore.setState({
report: null,
archive: null,
activeObservationIndex: 0,
saveFailed: false,
})
useReportStore.getState().clearTrapped()
vi.stubGlobal('alert', vi.fn())
})
afterEach(() => {
cleanup()
vi.unstubAllGlobals()
vi.restoreAllMocks()
useReportStore.setState({ report: null, saveFailed: false })
})
const mount = (path: '/' | '/new') =>
render(
} />
} />
} />
,
)
const openReport = (animalName = 'Mistral') =>
useReportStore.getState().hydrateImport(
{ report: minimalReport(sheet, { animal: { speciesId: 'equine', name: animalName } }), archive: null },
sheet,
)
async function submitForm(user: ReturnType) {
await user.type(screen.getByLabelText(fr['form.practitioner.name']!), 'Dr X')
await user.type(screen.getByLabelText(fr['form.animal.name']!), 'Rex')
await user.type(screen.getByLabelText(fr['form.owner.name']!), 'Y')
const submit = screen.getByRole('button', { name: fr['new_report.submit']! })
await waitFor(() => expect(submit).toBeEnabled())
await user.click(submit)
}
const button = (key: string) => screen.getByRole('button', { name: fr[key]! })
/** A button of the open dialog (the page behind has its own Annuler). */
const dialogButton = (key: string) =>
within(screen.getByRole('dialog')).getByRole('button', { name: fr[key]! })
describe('New report over a bilan in progress', () => {
it('names the animal of the report in memory and replaces it on Remplacer', async () => {
const user = userEvent.setup()
openReport()
mount('/new')
await submitForm(user)
const dialog = await screen.findByRole('dialog')
expect(dialog).toHaveTextContent(fr['session.replace.body']!.replace('{animal}', 'Mistral'))
expect(dialog).not.toHaveTextContent(fr['session.replace.unsaved']!)
await user.click(button('session.replace.confirm'))
await screen.findByTestId('editor-route')
expect(useReportStore.getState().report?.animal.name).toBe('Rex')
})
it('renders an animal name with a replacement pattern literally', async () => {
const user = userEvent.setup()
openReport('Rex$&$1')
mount('/new')
await submitForm(user)
expect(await screen.findByRole('dialog')).toHaveTextContent('Le bilan de Rex$&$1 sera')
})
it('uses the unloaded text for a draft this page never opened', async () => {
const user = userEvent.setup()
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet)))
mount('/new')
await submitForm(user)
expect(await screen.findByRole('dialog')).toHaveTextContent(fr['session.replace.body_unloaded']!)
})
it('adds the unsaved line and the copy button when the tab storage is full', async () => {
const user = userEvent.setup()
openReport()
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
throw new DOMException('full', 'QuotaExceededError')
})
act(() => useReportStore.getState().setConclusion('edited'))
mount('/new')
await submitForm(user)
const dialog = await screen.findByRole('dialog')
expect(dialog).toHaveTextContent(fr['session.replace.unsaved']!)
expect(dialog).toHaveTextContent(fr['session.held.download']!)
})
it('Annuler changes nothing and Ouvrir goes to the editor', async () => {
const user = userEvent.setup()
openReport()
const before = useReportStore.getState().report
mount('/new')
await submitForm(user)
await screen.findByRole('dialog')
await user.click(dialogButton('common.cancel'))
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
expect(useReportStore.getState().report).toBe(before)
await user.click(button('new_report.submit'))
await screen.findByRole('dialog')
await user.click(button('session.replace.open'))
await screen.findByTestId('editor-route')
expect(useReportStore.getState().report).toBe(before)
})
it('asks nothing when no bilan is in progress', async () => {
const user = userEvent.setup()
mount('/new')
await submitForm(user)
await screen.findByTestId('editor-route')
expect(screen.queryByRole('dialog')).toBeNull()
expect(useReportStore.getState().report?.animal.name).toBe('Rex')
})
})
describe('the Home file pick over a bilan in progress', () => {
const pickButton = () => button('home.import.cta')
const enabled = () => waitFor(() => expect(pickButton()).toBeEnabled())
it('asks first; Remplacer opens the picker inside the same click', async () => {
const user = userEvent.setup()
openReport()
const click = vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => undefined)
mount('/')
await enabled()
await user.click(pickButton())
expect(await screen.findByRole('dialog')).toHaveTextContent('Mistral')
expect(click).not.toHaveBeenCalled()
fireEvent.click(button('session.replace.confirm'))
expect(click).toHaveBeenCalledOnce()
})
it('Annuler never opens the picker; Ouvrir goes to the editor; the report stays', async () => {
const user = userEvent.setup()
openReport()
const before = useReportStore.getState().report
const click = vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => undefined)
mount('/')
await enabled()
await user.click(pickButton())
await user.click(dialogButton('common.cancel'))
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
await user.click(pickButton())
await user.click(button('session.replace.open'))
await screen.findByTestId('editor-route')
expect(click).not.toHaveBeenCalled()
expect(useReportStore.getState().report).toBe(before)
})
it('also asks for a draft only in the slot (unloaded text), nothing otherwise', async () => {
const user = userEvent.setup()
const click = vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => undefined)
mount('/')
await enabled()
await user.click(pickButton())
expect(screen.queryByRole('dialog')).toBeNull()
expect(click).toHaveBeenCalledOnce()
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(minimalReport(sheet)))
await user.click(pickButton())
expect(await screen.findByRole('dialog')).toHaveTextContent(fr['session.replace.body_unloaded']!)
expect(click).toHaveBeenCalledOnce()
})
})