/** * ChartExport — image-export failure vs not-ready (arch 05 §7 fail-loud). * * `getImageUrl` (injected by LivePreview) returns `null` ONLY when no view is * live — the not-ready case — and rejects on a real rasterize/serialize failure. * These two outcomes must land as DIFFERENT toasts: the not-ready one tells the * user to wait (no `detail`), the failure one carries diagnostic `detail` so the * problem can be reported. Burying the rejection behind the not-ready copy is the * regression this guards. */ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import { act } from 'react'; import { createRoot, type Root } from 'react-dom/client'; import { useNotificationStore } from '../stores/NotificationStore'; import { usePopoverStore } from '../stores/PopoverStore'; import { useSnippetStore } from '../stores/SnippetStore'; import { useDatasetStore } from '../stores/DatasetStore'; import { ChartExport, type ChartExportProps } from './ChartExport'; (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; let container: HTMLDivElement; let root: Root; beforeEach(() => { useNotificationStore.getState().clear(); usePopoverStore.setState({ openId: null }); useSnippetStore.getState().reset(); useDatasetStore.getState().reset(); // A non-empty draft makes `hasSpec` true so the export trigger is enabled. useSnippetStore.setState({ draftText: '{"mark":"point"}' }); container = document.createElement('div'); document.body.appendChild(container); root = createRoot(container); }); afterEach(() => { act(() => root.unmount()); container.remove(); useNotificationStore.getState().clear(); usePopoverStore.setState({ openId: null }); useSnippetStore.getState().reset(); useDatasetStore.getState().reset(); }); /** Mount, open the export popover (portaled to
), and click "Download PNG". * `downloadImage` awaits `getImageUrl`, so the caller flushes microtasks after. */ async function mountAndDownloadPng(getImageUrl: ChartExportProps['getImageUrl']) { act(() => { root.render(