// @vitest-environment jsdom import { act } from "react"; import { createRoot } from "react-dom/client"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { describe, expect, it, vi } from "vitest"; import { IssueArtifactFile } from "./IssueArtifactCard"; import { loadArtifactCsv } from "@/lib/artifact-card-data"; import { TextAttachmentContext } from "@/context/TextAttachmentContext"; vi.mock("@/lib/artifact-card-data", async (importOriginal) => ({ ...(await importOriginal()), loadArtifactCsv: vi.fn(), })); describe("CSV preview consent", () => { it("does not fetch any CSV until its preview is requested", async () => { const load = vi .mocked(loadArtifactCsv) .mockResolvedValue({ columns: ["Name"], rows: [["Actual row"]], truncated: false, }); const container = document.createElement("div"); document.body.appendChild(container); const root = createRoot(container); const client = new QueryClient({ defaultOptions: { queries: { retry: false } }, }); const openText = vi.fn(); try { await act(async () => root.render( {Array.from({ length: 20 }, (_, i) => ( ))} , ), ); expect(load).not.toHaveBeenCalled(); const open = container.querySelector('button[aria-label="Open in tab: Data 0"]'); await act(async () => open!.click()); expect(openText).toHaveBeenCalledWith("attachment-0", "report-0.csv"); expect(load).not.toHaveBeenCalled(); const preview = Array.from(container.querySelectorAll("button")).find( (button) => button.textContent === "Preview data", ); expect(preview).toBeDefined(); await act(async () => preview!.click()); await vi.waitFor(() => expect(container.textContent).toContain("Actual row"), ); expect(load).toHaveBeenCalledTimes(1); expect(load).toHaveBeenCalledWith( "/api/attachments/csv-0/content", expect.any(AbortSignal), ); expect(container.textContent).toContain("View data"); const download = container.querySelector('a[download="report-0.csv"]'); expect(download?.textContent).toBe("Download file"); expect(download?.getAttribute("href")).toBe("/api/attachments/csv-0/content?download=1"); const openAfterPreview = container.querySelector('button[aria-label="Open in tab: Data 0"]'); await act(async () => openAfterPreview!.click()); expect(openText).toHaveBeenCalledTimes(2); } finally { await act(async () => root.unmount()); client.clear(); container.remove(); vi.clearAllMocks(); } }); });