Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
80 lines
4.6 KiB
TypeScript
80 lines
4.6 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { LandingPage, type PublicBlock } from "./CmsBlocks";
|
|
import { utmTags } from "./LandingForm";
|
|
|
|
const info = {
|
|
eventName: "INS enquiries", startDate: "2027-01-01", endDate: "2099-12-31", location: "KL", companyName: "INS",
|
|
notice: { id: 1, textEn: "We keep your data.", textMs: "Kami simpan data anda." }, interests: [],
|
|
};
|
|
const company = { code: "INS", name: "INS Sdn Bhd", logoImageId: 4, accentColor: "#FFEE00" };
|
|
const page = (blocks: PublicBlock[]) => ({ slug: "hotel", title: "Hotel day", blocks });
|
|
|
|
beforeEach(() => vi.restoreAllMocks());
|
|
|
|
describe("LandingPage", () => {
|
|
it("shows the logo, the title as the only h1 and the accent with readable text", () => {
|
|
const { container } = render(<LandingPage company={company} page={page([])} />);
|
|
expect(screen.getByRole("img", { name: "INS Sdn Bhd" })).toHaveAttribute("src", "/api/public/page-images/4");
|
|
expect(screen.getAllByRole("heading", { level: 1 })).toHaveLength(1);
|
|
const style = (container.firstChild as HTMLElement).style;
|
|
expect(style.getPropertyValue("--primary")).toBe("#FFEE00");
|
|
expect(style.getPropertyValue("--primary-foreground")).toBe("#000000");
|
|
});
|
|
|
|
it("sanitises rich text and drops an unsafe button", () => {
|
|
render(<LandingPage company={company} page={page([
|
|
{ type: "RICH_TEXT", html: '<p>Hello<img src=x onerror="alert(1)"></p><script>alert(2)</script>' },
|
|
{ type: "HERO", heading: "Hi", buttonLabel: "Go", buttonUrl: "javascript:alert(1)" },
|
|
{ type: "IMAGE_TEXT", heading: "Side", buttonLabel: "Mail", buttonUrl: "mailto:a@b.c" },
|
|
])} />);
|
|
expect(screen.getByText("Hello")).toBeInTheDocument();
|
|
expect(document.querySelector("script, [onerror]")).toBeNull();
|
|
expect(screen.queryByRole("link", { name: "Go" })).not.toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: "Mail" })).toHaveAttribute("href", "mailto:a@b.c");
|
|
});
|
|
|
|
it("gives a named block its link name as id, so a #name button jumps to it", () => {
|
|
render(<LandingPage company={company} page={page([
|
|
{ type: "HERO", heading: "Hi", buttonLabel: "Sign up", buttonUrl: "#signup" },
|
|
{ type: "FORM", heading: "Join", closed: true, anchor: "signup" },
|
|
{ type: "RICH_TEXT", html: "<p>x</p>", anchor: 'bad" onclick="x' },
|
|
])} />);
|
|
expect(screen.getByRole("link", { name: "Sign up" })).toHaveAttribute("href", "#signup");
|
|
expect(document.getElementById("signup")).toHaveTextContent("Sign-up is closed.");
|
|
expect(document.querySelectorAll("main [id]")).toHaveLength(1);
|
|
});
|
|
|
|
it("a closed form says so; the preview shows a note instead of a form", () => {
|
|
const { unmount } = render(<LandingPage company={company} page={page([{ type: "FORM", heading: "Join", closed: true }])} />);
|
|
expect(screen.getByText("Sign-up is closed.")).toBeInTheDocument();
|
|
unmount();
|
|
render(<LandingPage company={company} page={page([{ type: "FORM", form: info }])} preview />);
|
|
expect(screen.getByText("The sign-up form shows here on the published page.")).toBeInTheDocument();
|
|
expect(screen.queryByLabelText(/name/i)).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("an open form posts to its block with the page's UTM tags, and a closed answer closes it", async () => {
|
|
window.history.replaceState(null, "", "/p/INS/hotel?utm_source=fb&utm_medium=cpc&utm_campaign=jan");
|
|
const fetchMock = vi.spyOn(globalThis, "fetch")
|
|
.mockResolvedValueOnce(new Response(JSON.stringify({ code: "FORM_CLOSED" }), { status: 409 }));
|
|
const user = userEvent.setup();
|
|
render(<LandingPage company={company} page={page([{ type: "RICH_TEXT", html: "<p>x</p>" }, { type: "FORM", form: info }])} />);
|
|
await user.type(screen.getByLabelText("Your name"), "Tan");
|
|
await user.type(screen.getByLabelText("Mobile number"), "0125550101");
|
|
await user.click(screen.getByRole("checkbox"));
|
|
await user.click(screen.getByRole("button", { name: "Register" }));
|
|
const [url, init] = fetchMock.mock.calls[0];
|
|
expect(url).toBe("/api/public/pages/INS/hotel/form/1");
|
|
expect(JSON.parse(String((init as RequestInit).body))).toMatchObject({ name: "Tan", utmSource: "fb", utmMedium: "cpc", utmCampaign: "jan" });
|
|
expect(await screen.findByText("Sign-up is closed.")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("utmTags", () => {
|
|
it("reads the three tags, capped at 100 characters", () => {
|
|
expect(utmTags("?utm_source=" + "x".repeat(150))).toEqual({ utmSource: "x".repeat(100), utmMedium: undefined, utmCampaign: undefined });
|
|
});
|
|
});
|