Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
86 lines
5.1 KiB
TypeScript
86 lines
5.1 KiB
TypeScript
import { act, render, screen } from "@testing-library/react";
|
|
import { describe, it, expect, onTestFinished, vi } from "vitest";
|
|
import { LandingPage, type PublicBlock } from "./CmsBlocks";
|
|
import { FRAME_SANDBOX } from "@/app/lib/frameHtml";
|
|
|
|
// add-custom-html-block: an HTML block is a sealed frame sized by its own reports; "hide header" drops the header.
|
|
const company = { code: "INS", name: "INS Sdn Bhd", logoImageId: 4, accentColor: null };
|
|
const page = (blocks: PublicBlock[], hideHeader = false) => ({ slug: "hotel", title: "Hotel day", hideHeader, blocks });
|
|
const HTML: PublicBlock = { type: "HTML", html: "<h1>Designed</h1><script>x()</script>" };
|
|
|
|
const frame = () => document.querySelector("iframe") as HTMLIFrameElement;
|
|
|
|
describe("LandingPage: Custom HTML", () => {
|
|
it("shows an HTML block as a sealed frame from its own address, never inline", () => {
|
|
render(<LandingPage company={company} page={page([{ type: "FAQ", items: [{ question: "Q", answer: "A" }] }, HTML])} />);
|
|
expect(frame()).toHaveAttribute("src", "/p/INS/hotel/frame/1");
|
|
expect(frame()).toHaveAttribute("sandbox", FRAME_SANDBOX);
|
|
expect(frame()).toHaveAttribute("title", "Hotel day");
|
|
expect(frame().style.height).toBe("600px");
|
|
expect(screen.queryByText("Designed")).not.toBeInTheDocument();
|
|
expect(document.querySelector("script")).toBeNull();
|
|
});
|
|
|
|
it("gives a named HTML block its link name as the frame's id", () => {
|
|
render(<LandingPage company={company} page={page([{ ...HTML, anchor: "story" }])} />);
|
|
expect(frame()).toHaveAttribute("id", "story");
|
|
});
|
|
|
|
it("scrolls the page for a #name link in its own frame: to the frame's target, else to the named block", () => {
|
|
const scrollTo = vi.spyOn(window, "scrollTo").mockImplementation(() => {});
|
|
const into = vi.fn();
|
|
Element.prototype.scrollIntoView = into;
|
|
render(<LandingPage company={company} page={page([HTML, { type: "FORM", heading: "Join", closed: true, anchor: "signup" }])} />);
|
|
vi.spyOn(frame(), "getBoundingClientRect").mockReturnValue({ top: 100 } as DOMRect);
|
|
const ask = (data: unknown, source: MessageEventSource | null) => act(() => { window.dispatchEvent(new MessageEvent("message", { data, source })); });
|
|
ask({ type: "colite-frame-anchor", name: "signup", y: null }, window);
|
|
expect(into).not.toHaveBeenCalled();
|
|
ask({ type: "colite-frame-anchor", name: "signup", y: null }, frame().contentWindow);
|
|
expect(into).toHaveBeenCalledTimes(1);
|
|
expect(into.mock.contexts[0]).toBe(document.getElementById("signup"));
|
|
ask({ type: "colite-frame-anchor", name: "top", y: 300 }, frame().contentWindow);
|
|
expect(scrollTo).toHaveBeenCalledWith({ top: 400, behavior: "smooth" });
|
|
ask({ type: "colite-frame-anchor", name: "nowhere", y: null }, frame().contentWindow);
|
|
expect(into).toHaveBeenCalledTimes(1);
|
|
expect(scrollTo).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("uses the preview's frame address when given", () => {
|
|
render(<LandingPage company={company} page={page([HTML])} frameBase="/pages/5/preview/frame" preview />);
|
|
expect(frame()).toHaveAttribute("src", "/pages/5/preview/frame/0");
|
|
});
|
|
|
|
it("grows to its frame's reported height and ignores other windows", () => {
|
|
render(<LandingPage company={company} page={page([HTML])} />);
|
|
act(() => { window.dispatchEvent(new MessageEvent("message", { data: { type: "colite-frame-height", height: 2400 }, source: window })); });
|
|
expect(frame().style.height).toBe("600px");
|
|
act(() => { window.dispatchEvent(new MessageEvent("message", { data: { type: "colite-frame-height", height: 2400 }, source: frame().contentWindow })); });
|
|
expect(frame().style.height).toBe("2400px");
|
|
});
|
|
|
|
it("grows inside another document too: it listens on its own document's window (add-page-visual-editor)", () => {
|
|
const host = document.createElement("iframe");
|
|
document.body.appendChild(host);
|
|
onTestFinished(() => host.remove());
|
|
const inner = host.contentDocument!;
|
|
render(<LandingPage company={company} page={page([HTML])} />, { container: inner.body.appendChild(inner.createElement("div")) });
|
|
const sealed = inner.querySelector("iframe") as HTMLIFrameElement;
|
|
act(() => { host.contentWindow!.dispatchEvent(new MessageEvent("message", { data: { type: "colite-frame-height", height: 2400 }, source: sealed.contentWindow })); });
|
|
expect(sealed.style.height).toBe("2400px");
|
|
});
|
|
|
|
it("hides the company header and shows the title to screen readers only when the page says so", () => {
|
|
render(<LandingPage company={company} page={page([HTML], true)} />);
|
|
expect(screen.queryByRole("img", { name: "INS Sdn Bhd" })).not.toBeInTheDocument();
|
|
expect(document.querySelector("header")).toBeNull();
|
|
expect(screen.getByRole("heading", { level: 1, name: "Hotel day" })).toHaveClass("sr-only");
|
|
});
|
|
|
|
it("starts a header-less page flush at the top: the hidden title takes no part in block spacing", () => {
|
|
render(<LandingPage company={company} page={page([HTML], true)} />);
|
|
const title = screen.getByRole("heading", { level: 1, name: "Hotel day" });
|
|
expect(title.parentElement!.className).not.toMatch(/space-y/);
|
|
expect(frame().parentElement!.className).toMatch(/space-y/);
|
|
});
|
|
});
|