test(pdf): step 6 phase 0 freeze — demo report, bare v3 PDF, 39 goldens (text, images, links, views) and route characterisation tests
This commit is contained in:
1 parent
694092610d
commit
8b86b926c8
46 files changed
+18502
-5
No files matched your search
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Step 6 Phase 0 characterisation of the export flow on the editor route,
|
||||
* with `exportReportToPdf` mocked: the call shape (store report, loaded
|
||||
* index, locale), the download name and the failure alert. Phase 4 adapts
|
||||
* only the mocked module path and the call-shape assertion.
|
||||
*/
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
vi,
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
} from 'vitest'
|
||||
import { cleanup, render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { MemoryRouter } from 'react-router-dom'
|
||||
import EditorRoute from '@/app/routes/EditorRoute'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { useReportStore } from '@/state/reportStore'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { fr } from '@/i18n/fr'
|
||||
import { en } from '@/i18n/en'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
||||
|
||||
vi.mock('@/pdf/export', () => ({ exportReportToPdf: vi.fn() }))
|
||||
const exportMock = vi.mocked(exportReportToPdf)
|
||||
|
||||
let sheet: SheetIndex
|
||||
const downloads: string[] = []
|
||||
const realCreateObjectURL = URL.createObjectURL
|
||||
const realRevokeObjectURL = URL.revokeObjectURL
|
||||
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
})
|
||||
beforeEach(() => {
|
||||
useLocaleStore.setState({ locale: 'fr' })
|
||||
sessionStorage.clear()
|
||||
sessionStorage.setItem('osteo:disclaimer-shown', '1')
|
||||
window.scrollTo = vi.fn() as unknown as typeof window.scrollTo
|
||||
useReportStore.setState({ report: null, activeObservationIndex: 0 })
|
||||
useReportStore.getState().hydrateReport(minimalReport(sheet))
|
||||
exportMock.mockReset()
|
||||
downloads.length = 0
|
||||
URL.createObjectURL = vi.fn(() => 'blob:faunosteo-test')
|
||||
URL.revokeObjectURL = vi.fn()
|
||||
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (
|
||||
this: HTMLAnchorElement,
|
||||
) {
|
||||
downloads.push(this.download)
|
||||
})
|
||||
vi.stubGlobal('alert', vi.fn())
|
||||
})
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
URL.createObjectURL = realCreateObjectURL
|
||||
URL.revokeObjectURL = realRevokeObjectURL
|
||||
})
|
||||
|
||||
async function mountAndExport(label: string): Promise<void> {
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/editor']}>
|
||||
<EditorRoute />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
const button = await screen.findByRole('button', { name: label })
|
||||
await userEvent.setup().click(button)
|
||||
}
|
||||
|
||||
describe('EditorRoute export', () => {
|
||||
it('calls exportReportToPdf with the store report, the loaded index and the FR locale', async () => {
|
||||
exportMock.mockResolvedValue(new Uint8Array([37, 80, 68, 70]))
|
||||
await mountAndExport(fr['editor.export']!)
|
||||
await waitFor(() => expect(exportMock).toHaveBeenCalledTimes(1))
|
||||
const [report, index, options] = exportMock.mock.calls[0]!
|
||||
expect(report).toBe(useReportStore.getState().report)
|
||||
expect(index).toBe(sheet)
|
||||
expect(options).toEqual({ locale: 'fr' })
|
||||
})
|
||||
|
||||
it('passes the active locale (EN)', async () => {
|
||||
useLocaleStore.setState({ locale: 'en' })
|
||||
exportMock.mockResolvedValue(new Uint8Array([37, 80, 68, 70]))
|
||||
await mountAndExport(en['editor.export']!)
|
||||
await waitFor(() => expect(exportMock).toHaveBeenCalledTimes(1))
|
||||
expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' })
|
||||
})
|
||||
|
||||
it('downloads bilan-<animal>-<date>.pdf through a blob URL it then revokes', async () => {
|
||||
exportMock.mockResolvedValue(new Uint8Array([37, 80, 68, 70]))
|
||||
await mountAndExport(fr['editor.export']!)
|
||||
await waitFor(() => expect(downloads).toHaveLength(1))
|
||||
expect(downloads[0]).toBe('bilan-Mistral-2026-10-10.pdf')
|
||||
expect(URL.createObjectURL).toHaveBeenCalledTimes(1)
|
||||
const blob = vi.mocked(URL.createObjectURL).mock.calls[0]![0] as Blob
|
||||
expect(blob.type).toBe('application/pdf')
|
||||
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:faunosteo-test')
|
||||
expect(window.alert).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('alerts editor.export_failed when the export rejects', async () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => undefined)
|
||||
exportMock.mockRejectedValue(new Error('render exploded'))
|
||||
await mountAndExport(fr['editor.export']!)
|
||||
await waitFor(() => expect(window.alert).toHaveBeenCalledTimes(1))
|
||||
expect(window.alert).toHaveBeenCalledWith(fr['editor.export_failed'])
|
||||
expect(downloads).toHaveLength(0)
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByRole('button', { name: fr['editor.export'] }),
|
||||
).toBeEnabled(),
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user