Files
co-lite/solvway-admin-portal/app/lib/cmsPages.test.ts
T

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)]) expect(isSafeLink(bad), bad).toBe(false);
expect(anchorId("signup")).toBe("signup");
for (const bad of [undefined, "", "Sign Up", "x\" onclick=\"y"]) expect(anchorId(bad), String(bad)).toBeUndefined();
});
});