Files
co-lite/solvway-admin-portal/app/components/pages/CmsBlocks.test.tsx
T

331 lines
20 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]);
});
});
// add-compact-landing-form: a page's form is the short one, and coloured bands join.
describe("LandingPage compact form and bands", () => {
it("a page's form has no job title and a service dropdown", () => {
render(<LandingPage company={company} page={page([{ type: "FORM", form: { ...info, interests: [{ id: 3, name: "Cloud IP PABX" }] } }])} />);
expect(screen.queryByLabelText("Job title")).not.toBeInTheDocument();
expect(screen.getByRole("combobox", { name: "Service interested in (optional)" })).toBeInTheDocument();
});
it("joins a dark call to action and a dark text block with no gap", () => {
render(<LandingPage company={company} page={page([
{ type: "RICH_TEXT", html: "<p>Intro</p>" },
{ type: "CTA", heading: "Talk to us", look: "DARK" },
{ type: "RICH_TEXT", html: "<p>Footer</p>", look: "DARK" },
])} />);
const rows = Array.from(document.querySelector("main > div")!.children);
expect(rows.map((r) => r.classList.contains("mt-10"))).toEqual([false, true, false]);
expect(rows.slice(1).map((r) => r.querySelector("[data-look]")?.getAttribute("data-look"))).toEqual(["DARK", "DARK"]);
});
});
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 });
});
});
// add-landing-thanks-and-footer: the page's own thank-you text, and the footer block.
describe("LandingPage thank-you text and footer", () => {
async function signUp() {
const user = userEvent.setup();
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" }));
}
it("a hero form shows its thank-you text after sign-up, with the number as a call link", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(new Response("", { status: 201 }));
render(<LandingPage company={company} page={page([{ type: "HERO", heading: "One partner", eventUuid: "77", form: info,
thanksText: "Prefer to talk now? Call +60 12-833 2775." }])} />);
await signUp();
const done = await screen.findByRole("status");
expect(done).toHaveTextContent("Thank you, you're registered for INS enquiries");
expect(done).toHaveTextContent("Prefer to talk now? Call +60 12-833 2775.");
expect(within(done).getByRole("link", { name: "+60 12-833 2775" })).toHaveAttribute("href", "tel:+60128332775");
});
it("a form block without a thank-you text keeps today's thank-you", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(new Response("", { status: 201 }));
render(<LandingPage company={company} page={page([{ type: "FORM", form: info }])} />);
await signUp();
const done = await screen.findByRole("status");
expect(done.querySelector("p")).toBeNull();
expect(within(done).getByRole("button", { name: "Register another person" })).toBeInTheDocument();
});
const footer: PublicBlock = { type: "FOOTER", look: "DARK", text: "© 2026 Infinity Networks Security Sdn Bhd. All rights reserved.", cards: [
{ title: "Infinity Networks Security Sdn Bhd", text: "Unit 2-6, Infinity Tower\nPetaling Jaya, Malaysia" },
{ title: "Talk to us", text: "+603-7623 9807\ninfo@infinitynetworks.com.my" },
{ title: "Website", text: "www.infinitynetworks.com.my" },
] };
it("shows a footer's columns side by side with contact links, and the legal line under a divider", () => {
render(<LandingPage company={company} page={page([footer])} />);
const foot = screen.getByRole("contentinfo");
expect(foot.closest("[data-look]")).toHaveAttribute("data-look", "DARK");
const columns = within(foot).getAllByRole("listitem");
expect(columns).toHaveLength(3);
expect(columns[0].parentElement).toHaveClass("md:grid-cols-3");
expect(within(columns[0]).getByText(/Unit 2-6/)).toHaveClass("whitespace-pre-line");
expect(within(foot).getByRole("link", { name: "+603-7623 9807" })).toHaveAttribute("href", "tel:+60376239807");
expect(within(foot).getByRole("link", { name: "info@infinitynetworks.com.my" })).toHaveAttribute("href", "mailto:info@infinitynetworks.com.my");
const site = within(foot).getByRole("link", { name: "www.infinitynetworks.com.my" });
expect(site).toHaveAttribute("href", "https://www.infinitynetworks.com.my");
expect(site).toHaveAttribute("target", "_blank");
expect(site).toHaveAttribute("rel", "noopener noreferrer");
expect(within(foot).getByText(/^© 2026/)).toHaveClass("border-t");
});
it("ends the page at the footer: no blank strip below a closing footer", () => {
const { unmount } = render(<LandingPage company={company} page={page([{ type: "FAQ", items: [{ question: "Q", answer: "A" }] }, footer])} />);
expect(screen.getByRole("main")).not.toHaveClass("pb-8");
expect(screen.getByRole("main")).not.toHaveClass("py-8");
unmount();
render(<LandingPage company={company} page={page([footer, { type: "FAQ", items: [{ question: "Q", answer: "A" }] }])} />);
expect(screen.getByRole("main").className).toMatch(/p[by]-8/);
});
it("keeps markup in footer text as text", () => {
render(<LandingPage company={company} page={page([{ type: "FOOTER", cards: [{ title: "Call", text: "<b>Call</b> +60 12-833 2775" }] }])} />);
const foot = screen.getByRole("contentinfo");
expect(foot.querySelector("b")).toBeNull();
expect(foot).toHaveTextContent("<b>Call</b> +60 12-833 2775");
expect(within(foot).getByRole("link", { name: "+60 12-833 2775" })).toHaveAttribute("href", "tel:+60128332775");
});
});
// add-landing-page-polish: the top bar, section labels and serif headings.
describe("LandingPage polish", () => {
const bar: PublicBlock = { type: "TOPBAR", buttonLabel: "Get My Free Consultation", buttonUrl: "#consult" };
const hero: PublicBlock = { type: "HERO", heading: "One partner", fullWidth: true };
it("a top bar shows the logo and its button, stays at the top, and joins the hero", () => {
render(<LandingPage company={company} page={{ ...page([bar, hero]), hideHeader: true }} />);
const top = screen.getByRole("banner");
expect(within(top).getByRole("img", { name: "INS Sdn Bhd" })).toHaveAttribute("src", "/api/public/page-images/4");
expect(within(top).getByRole("link", { name: "Get My Free Consultation" })).toHaveAttribute("href", "#consult");
// The row sticks, not the header: a sticky element only sticks inside its parent.
expect(top.closest("main > div > div")).toHaveClass("sticky", "top-0");
expect(top.closest(".max-w-5xl")).toBeNull();
expect(screen.getAllByRole("banner")).toHaveLength(1);
const rows = Array.from(document.querySelector("main > div")!.children);
expect(rows.map((r) => r.classList.contains("mt-10"))).toEqual([false, false]);
});
it("a top bar without a logo shows the company name, and no button without one", () => {
render(<LandingPage company={{ ...company, logoImageId: null }} page={{ ...page([{ type: "TOPBAR" }]), hideHeader: true }} />);
const top = screen.getByRole("banner");
expect(top).toHaveTextContent("INS Sdn Bhd");
expect(within(top).queryByRole("link")).toBeNull();
});
it("named blocks keep clear of a sticky bar when jumped to", () => {
render(<LandingPage company={company} page={page([bar, { type: "FORM", heading: "Join", closed: true, anchor: "consult" }])} />);
expect(document.getElementById("consult")).toHaveClass("scroll-mt-24");
});
it("shows a section's label above its heading, and section headings in serif", () => {
render(<LandingPage company={company} page={page([
{ type: "FEATURES", eyebrow: "Services", heading: "Everything your guests touch", cards: [{ icon: "wifi", title: "WiFi", text: "Fast." }] },
{ type: "FAQ", eyebrow: "FAQ", heading: "Questions hotels ask us", items: [{ question: "Q", answer: "A" }], look: "DARK" },
])} />);
const heading = screen.getByRole("heading", { level: 2, name: "Everything your guests touch" });
expect(heading).toHaveClass("font-serif");
const label = heading.previousElementSibling!;
expect(label).toHaveTextContent("Services");
expect(label).toHaveClass("uppercase");
expect(screen.getByRole("heading", { level: 2, name: "Questions hotels ask us" })).toHaveClass("font-serif");
expect(screen.getByRole("heading", { level: 2, name: "Questions hotels ask us" }).previousElementSibling).toHaveClass("text-primary");
});
});
// add-hero-fits-screen: the top bar, hero and trust strip fit one desktop screen.
describe("LandingPage first screen", () => {
it("gives a trust band less padding than other bands, and the full-width hero less padding", () => {
render(<LandingPage company={company} page={page([
{ type: "HERO", heading: "One partner", fullWidth: true, eventUuid: "77", closed: true },
{ type: "TRUST", look: "MUTED", cards: [{ title: "Design" }, { title: "Local support" }] },
{ type: "CTA", heading: "Talk to us", look: "DARK" },
])} />);
const bands = Array.from(document.querySelectorAll("[data-look]"));
expect(bands[0]).toHaveClass("py-5");
expect(bands[1]).toHaveClass("py-12");
const grid = screen.getByRole("heading", { name: "One partner" }).closest(".grid")!;
expect(grid).toHaveClass("pt-8", "pb-8");
expect(screen.getByText("Sign-up is closed.").closest(".shadow-card")).toHaveClass("space-y-3");
});
});
// add-event-ticket-orders: the TICKETS block, a card with its label and heading, the event's types, or closed.
describe("LandingPage TICKETS block", () => {
const tickets = {
eventName: "KL Hotel Tech Day", startDate: "2027-01-15", endDate: "2027-01-16", location: "KLCC", companyName: "INS",
notice: { id: 9, textEn: "English notice", textMs: "Notis" },
types: [{ id: 1, name: "Early bird", priceSen: 15000, left: 12, state: "ON_SALE" as const }],
};
it("shows the label, heading, text and the ticket types", () => {
render(<LandingPage company={company} page={page([
{ type: "TICKETS", eventUuid: "900", eyebrow: "Tickets", heading: "Get your seat", text: "Seats are limited.", tickets, anchor: "tickets" },
])} />);
const heading = screen.getByRole("heading", { level: 2, name: "Get your seat" });
expect(heading.previousElementSibling).toHaveTextContent("Tickets");
expect(screen.getByText("Seats are limited.")).toBeInTheDocument();
expect(screen.getByRole("group", { name: "Early bird" })).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Request tickets" })).toBeInTheDocument();
expect(document.getElementById("tickets")).not.toBeNull();
});
it("says sales are closed when the page carries closed", () => {
render(<LandingPage company={company} page={page([{ type: "TICKETS", eventUuid: "900", heading: "Get your seat", closed: true }])} />);
expect(screen.getByText("Ticket sales are closed.")).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Request tickets" })).not.toBeInTheDocument();
});
});