Files
co-lite/solvway-customer-portal/app/components/ui/ImageGallery.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

133 lines
5.8 KiB
TypeScript

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(<ImageGallery images={[IMAGE]} />);
expect(screen.getAllByAltText("Test Doc")).toHaveLength(1);
});
it("opens a fullscreen lightbox showing the image when the card is tapped", () => {
render(<ImageGallery images={[IMAGE]} />);
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(<ImageGallery images={[IMAGE]} />);
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(<ImageGallery images={[IMAGE]} />);
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(<ImageGallery images={[PDF]} />);
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(<ImageGallery images={[PDF]} />);
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(<ImageGallery images={[PDF]} />);
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(<ImageGallery images={[PDF]} />);
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(<ImageGallery images={[PDF]} />);
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(<ImageGallery images={[IMAGE]} />);
fireEvent.click(screen.getByText("View"));
expect(downloadName).toBe("test_doc.png");
expect(loadPdf).not.toHaveBeenCalled();
});
});
});