/** * 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, the failure alert and the alert that * names what the exported PDF lacks when the engine warned. */ 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) const PDF_BYTES = new Uint8Array([37, 80, 68, 70]) 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() .hydrateImport({ report: minimalReport(sheet), archive: null }, 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 { render( , ) 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({ pdf: PDF_BYTES, warnings: [] }) 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({ pdf: PDF_BYTES, warnings: [] }) await mountAndExport(en['editor.export']!) await waitFor(() => expect(exportMock).toHaveBeenCalledTimes(1)) expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' }) }) it('downloads bilan--.pdf through a blob URL it then revokes', async () => { exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] }) 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('downloads then alerts editor.export_warnings naming the failed logo slots and views', async () => { exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [ { kind: 'logo_failed', slot: 'headerLogo' }, { kind: 'view_raster_failed', observationId: 'obs-1', viewId: 'head' }, ], }) await mountAndExport(fr['editor.export']!) await waitFor(() => expect(window.alert).toHaveBeenCalledTimes(1)) expect(downloads).toEqual(['bilan-Mistral-2026-10-10.pdf']) expect(window.alert).toHaveBeenCalledWith( 'PDF exporté avec des éléments manquants : headerLogo, head. Vérifiez la console du navigateur.', ) }) 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(), ) }) })