A full-width hero spans the window with a larger serif headline and a lighter left-to-right shade. It may list up to 6 points and give its form card a heading and text. Full-width neighbours join with no gap. Custom HTML frames now shrink to 20 px and re-report their height when the page asks, so a short top bar no longer leaves a blank band. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
151 lines
9.0 KiB
TypeScript
151 lines
9.0 KiB
TypeScript
import { render, screen, within } 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();
|
|
});
|
|
|
|
// add-hero-signup-form: a hero with an event carries the sign-up form beside its text.
|
|
it("a hero with an event shows the sign-up form inside the hero and posts to the hero's block", async () => {
|
|
window.history.replaceState(null, "", "/p/INS/hotel?utm_source=fb");
|
|
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: "HERO", heading: "One partner", eventUuid: "77", form: info }])} />);
|
|
const hero = screen.getByRole("heading", { name: "One partner" }).closest("section")!;
|
|
expect(hero).toContainElement(screen.getByLabelText("Your name"));
|
|
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" }));
|
|
expect(fetchMock.mock.calls[0][0]).toBe("/api/public/pages/INS/hotel/form/0");
|
|
});
|
|
|
|
it("a hero whose form closed keeps its text and says so; a hero without an event has no form", () => {
|
|
const { unmount } = render(<LandingPage company={company} page={page([{ type: "HERO", heading: "One partner", eventUuid: "77", closed: true }])} />);
|
|
const hero = screen.getByRole("heading", { name: "One partner" }).closest("section")!;
|
|
expect(hero).toHaveTextContent("Sign-up is closed.");
|
|
unmount();
|
|
render(<LandingPage company={company} page={page([{ type: "HERO", heading: "Plain" }])} />);
|
|
expect(screen.queryByText("Sign-up is closed.")).not.toBeInTheDocument();
|
|
expect(screen.queryByLabelText("Your name")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("the preview of a hero with an event shows the form's note in the hero", () => {
|
|
render(<LandingPage company={company} page={page([{ type: "HERO", heading: "One partner", eventUuid: "77" }])} preview />);
|
|
expect(screen.getByRole("heading", { name: "One partner" }).closest("section"))
|
|
.toHaveTextContent("The sign-up form shows here on the published page.");
|
|
});
|
|
});
|
|
|
|
// add-hero-full-width: edge-to-edge hero, points, the form card's heading, and flush full-width neighbours.
|
|
describe("LandingPage full-width hero", () => {
|
|
const ins = { type: "HERO" as const, heading: "One partner", fullWidth: true, eventUuid: "77", closed: true,
|
|
cards: [{ title: "One accountable partner" }, { title: "Local support team in Malaysia" }],
|
|
formHeading: "Get a free consultation", formText: "Tell us about your property." };
|
|
|
|
it("runs outside the page width with square corners; a plain hero stays boxed in the page width", () => {
|
|
render(<LandingPage company={company} page={page([ins, { type: "HERO", heading: "Boxed" }])} />);
|
|
const wide = screen.getByRole("heading", { name: "One partner" }).closest("section")!;
|
|
expect(wide.closest(".max-w-5xl")).toBeNull();
|
|
expect(wide).not.toHaveClass("rounded-lg");
|
|
const boxed = screen.getByRole("heading", { name: "Boxed" }).closest("section")!;
|
|
expect(boxed.closest(".max-w-5xl")).not.toBeNull();
|
|
expect(boxed).toHaveClass("rounded-lg");
|
|
});
|
|
|
|
it("shows its points as a list under the text", () => {
|
|
render(<LandingPage company={company} page={page([ins])} />);
|
|
const hero = screen.getByRole("heading", { name: "One partner" }).closest("section")!;
|
|
const items = within(hero).getAllByRole("listitem");
|
|
expect(items.map((li) => li.textContent)).toEqual(["One accountable partner", "Local support team in Malaysia"]);
|
|
});
|
|
|
|
it("starts the form card with its heading and text", () => {
|
|
render(<LandingPage company={company} page={page([ins])} />);
|
|
const title = screen.getByRole("heading", { name: "Get a free consultation" });
|
|
const card = title.parentElement!.parentElement!;
|
|
expect(card).toHaveTextContent(/^Get a free consultation\s*Tell us about your property\./);
|
|
expect(card).toHaveTextContent("Sign-up is closed.");
|
|
});
|
|
|
|
it("joins a Custom HTML block and a full-width hero with no gap; other blocks keep theirs", () => {
|
|
render(<LandingPage company={company} frameBase="/f" page={page([
|
|
{ type: "HTML", html: "<header>Top</header>" }, ins, { type: "HTML", html: "<p>Rest</p>" }, { type: "RICH_TEXT", html: "<p>After</p>" },
|
|
])} />);
|
|
const rows = Array.from(document.querySelector("main > div")!.children);
|
|
expect(rows.map((r) => r.classList.contains("mt-10"))).toEqual([false, false, false, true]);
|
|
});
|
|
});
|
|
|
|
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 });
|
|
});
|
|
});
|