import { describe, it, expect, vi, afterEach, beforeEach } from "vitest"; import { render, screen, fireEvent, within } from "@testing-library/react"; import ImageGallery, { type GalleryImage } from "./ImageGallery"; // pdf.js cannot run in jsdom; the loader is the seam between the gallery and pdf.js. const { loadPdf } = vi.hoisted(() => ({ loadPdf: vi.fn() })); vi.mock("./pdfDocument", () => ({ loadPdf })); const IMAGE: GalleryImage = { id: "doc-1", title: "Test Doc", subtitle: "A test document", gradient: "linear-gradient(145deg,#000,#fff)", src: "https://example.com/doc.png", }; describe("ImageGallery", () => { it("renders the image card once before the lightbox is opened", () => { render(); expect(screen.getAllByAltText("Test Doc")).toHaveLength(1); }); it("opens a fullscreen lightbox showing the image when the card is tapped", () => { render(); fireEvent.click(screen.getByAltText("Test Doc")); // grid card image + lightbox image expect(screen.getAllByAltText("Test Doc")).toHaveLength(2); }); it("labels the action 'View' (not 'Download') on the card", () => { render(); expect(screen.getByText("View")).toBeInTheDocument(); expect(screen.queryByText("Download")).not.toBeInTheDocument(); }); it("still downloads and shows 'Saved' when the lightbox 'View' button is tapped", () => { render(); fireEvent.click(screen.getByAltText("Test Doc")); // grid card + lightbox each render a "View" button; the lightbox one is last const viewButtons = screen.getAllByText("View"); fireEvent.click(viewButtons[viewButtons.length - 1]); expect(screen.getAllByText("Saved").length).toBeGreaterThanOrEqual(1); }); describe("PDF entries", () => { const PDF: GalleryImage = { ...IMAGE, id: "pdf-1", title: "Stage Activities", src: "https://s3.example.com/EVENT_STAGE_ACTIVITIES/123.PDF?X-Amz-Signature=abc", }; /** Fake PDF document with `numPages` pages; records which pages were requested. */ function fakePdf(numPages: number) { const getPage = vi.fn(async () => ({ getViewport: () => ({ width: 600, height: 800 }), render: () => ({ promise: Promise.resolve() }), })); return { numPages, getPage }; } beforeEach(() => { loadPdf.mockReset(); }); afterEach(() => vi.restoreAllMocks()); it("previews page 1 on the card instead of an image", async () => { const doc = fakePdf(3); loadPdf.mockResolvedValue(doc); render(); expect(await screen.findByLabelText("Stage Activities page 1")).toBeInTheDocument(); expect(loadPdf).toHaveBeenCalledWith(PDF.src); expect(screen.queryByRole("img")).not.toBeInTheDocument(); expect(screen.queryByLabelText("Stage Activities page 2")).not.toBeInTheDocument(); }); it("opens an in-app reader with every page when the card is tapped", async () => { const open = vi.spyOn(window, "open").mockReturnValue(null); loadPdf.mockResolvedValue(fakePdf(3)); render(); fireEvent.click(await screen.findByLabelText("Stage Activities page 1")); const reader = await screen.findByRole("dialog", { name: "Stage Activities" }); for (const n of [1, 2, 3]) { expect(await within(reader).findByLabelText(`Stage Activities page ${n}`)).toBeInTheDocument(); } expect(within(reader).queryByText(/Showing/)).not.toBeInTheDocument(); expect(open).not.toHaveBeenCalled(); }); it("caps the reader at 5 pages and links to the full PDF", async () => { loadPdf.mockResolvedValue(fakePdf(8)); render(); fireEvent.click(await screen.findByLabelText("Stage Activities page 1")); const reader = await screen.findByRole("dialog", { name: "Stage Activities" }); expect(await within(reader).findByLabelText("Stage Activities page 5")).toBeInTheDocument(); expect(within(reader).queryByLabelText("Stage Activities page 6")).not.toBeInTheDocument(); expect(within(reader).getByText("Showing 5 of 8 pages")).toBeInTheDocument(); const link = within(reader).getByRole("link", { name: "Open full PDF" }); expect(link).toHaveAttribute("href", PDF.src); expect(link).toHaveAttribute("target", "_blank"); }); it("falls back to a PDF placeholder that opens a new tab when the PDF cannot load", async () => { const open = vi.spyOn(window, "open").mockReturnValue(null); loadPdf.mockRejectedValue(new Error("CORS")); render(); fireEvent.click(await screen.findByText("PDF")); expect(open).toHaveBeenCalledWith(PDF.src, "_blank", "noopener,noreferrer"); expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); }); it("downloads the PDF with a .pdf filename", () => { loadPdf.mockReturnValue(new Promise(() => {})); let downloadName = ""; vi.spyOn(HTMLAnchorElement.prototype, "click").mockImplementation(function (this: HTMLAnchorElement) { downloadName = this.download; }); render(); fireEvent.click(screen.getByText("View")); expect(downloadName).toBe("stage_activities.pdf"); }); it("keeps the .png filename for image entries", () => { let downloadName = ""; vi.spyOn(HTMLAnchorElement.prototype, "click").mockImplementation(function (this: HTMLAnchorElement) { downloadName = this.download; }); render(); fireEvent.click(screen.getByText("View")); expect(downloadName).toBe("test_doc.png"); expect(loadPdf).not.toHaveBeenCalled(); }); }); });