A block named reg-email would have duplicated the form's input id, so its label and #reg-email links would bind to the wrong element. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
40 lines
2.0 KiB
TypeScript
40 lines
2.0 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { anchorId, contrastText, emptyPage, isSafeLink, pageBody, slugify, withTitle } from "./cmsPages";
|
|
|
|
describe("cmsPages helpers", () => {
|
|
it("slug follows the title until edited", () => {
|
|
const d = withTitle(emptyPage(), "Hotel Technology Day 2027!");
|
|
expect(d.slug).toBe("hotel-technology-day-2027");
|
|
expect(withTitle({ ...d, slug: "mine", slugTouched: true }, "Other").slug).toBe("mine");
|
|
expect(slugify(" ")).toBe("");
|
|
});
|
|
|
|
it("save body trims and drops blank optional fields", () => {
|
|
const body = pageBody({
|
|
...emptyPage(), title: " T ", slug: "t", metaDescription: " ",
|
|
blocks: [{ type: "HERO", heading: " Hi ", text: "", imageId: null }, { type: "FAQ", items: [{ question: " Q ", answer: " A " }] }],
|
|
});
|
|
expect(body.title).toBe("T");
|
|
expect(body.metaDescription).toBeNull();
|
|
expect(body.blocks).toEqual([{ type: "HERO", heading: "Hi" }, { type: "FAQ", items: [{ question: "Q", answer: "A" }] }]);
|
|
});
|
|
|
|
it("picks the more readable text colour", () => {
|
|
expect(contrastText("#FFEE00")).toBe("#000000");
|
|
expect(contrastText("#1A237E")).toBe("#FFFFFF");
|
|
expect(contrastText("bad")).toBe("#FFFFFF");
|
|
});
|
|
|
|
it("allows only safe button links", () => {
|
|
for (const ok of ["/p/INS/a", "https://x.com", "mailto:a@b.c", "tel:+60123"]) expect(isSafeLink(ok), ok).toBe(true);
|
|
for (const bad of ["//evil.com", "javascript:alert(1)", "http://x.com", "https://a b", "/\\evil", undefined]) expect(isSafeLink(bad), String(bad)).toBe(false);
|
|
});
|
|
|
|
it("a same-page link is # plus a block link name", () => {
|
|
for (const ok of ["#signup", "#top", "#2-rooms", "#" + "a".repeat(40)]) expect(isSafeLink(ok), ok).toBe(true);
|
|
for (const bad of ["#", "#Sign Up", "#-x", "#signup/x", "#a\"b", "#" + "a".repeat(41), "#reg-consent"]) expect(isSafeLink(bad), bad).toBe(false);
|
|
expect(anchorId("signup")).toBe("signup");
|
|
for (const bad of [undefined, "", "Sign Up", "x\" onclick=\"y", "reg-email"]) expect(anchorId(bad), String(bad)).toBeUndefined();
|
|
});
|
|
});
|