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

73 lines
3.4 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { ANCHOR, ICONS, isSafeLink, pageBody, emptyPage, type CmsBlock } from "@/app/lib/cmsPages";
import { PAGE_TEMPLATES } from "@/app/lib/pageTemplates";
// The server's block rules (CmsBlockValidator) that a template can break, so a starter page never saves with an
// error staff did not make. The event and images are left for staff to choose.
function problems(blocks: CmsBlock[]): string[] {
const out: string[] = [];
const anchors = new Set<string>();
const len = (v: string | undefined, max: number, path: string, required = false) => {
if (required && !v?.trim()) out.push(`${path} required`);
if (v && v.length > max) out.push(`${path} too long`);
};
blocks.forEach((b, i) => {
const p = `blocks[${i}]`;
if (b.anchor) {
if (!ANCHOR.test(b.anchor) || anchors.has(b.anchor)) out.push(`${p}.anchor`);
anchors.add(b.anchor);
}
const label = !!b.buttonLabel?.trim();
if (label !== !!b.buttonUrl?.trim()) out.push(`${p} button pair`);
if (b.buttonUrl && !isSafeLink(b.buttonUrl)) out.push(`${p}.buttonUrl`);
if (b.buttonUrl?.startsWith("#") && !blocks.some((x) => x.anchor === b.buttonUrl!.slice(1))) out.push(`${p}.buttonUrl target`);
len(b.buttonLabel, 40, `${p}.buttonLabel`, b.type === "CTA");
len(b.heading, 120, `${p}.heading`, ["HERO", "FEATURES", "STEPS", "CTA"].includes(b.type));
len(b.text, 300, `${p}.text`);
if (b.type === "TRUST" && b.heading != null) out.push(`${p}.heading unused`);
if (["FEATURES", "STEPS", "TRUST"].includes(b.type)) {
const cards = b.cards ?? [];
if (cards.length < 2 || cards.length > 6) out.push(`${p}.cards count`);
cards.forEach((c, j) => {
const cp = `${p}.cards[${j}]`;
if (b.type === "FEATURES" ? !(c.icon && c.icon in ICONS) : c.icon != null) out.push(`${cp}.icon`);
len(c.title, 80, `${cp}.title`, true);
if (b.type === "TRUST" ? c.text != null : !c.text?.trim()) out.push(`${cp}.text`);
len(c.text, 300, `${cp}.text`);
});
} else if (b.cards) out.push(`${p}.cards unused`);
if (b.type === "FAQ") {
if (!b.items?.length) out.push(`${p}.items count`);
b.items?.forEach((it, j) => { len(it.question, 300, `${p}.items[${j}].question`, true); len(it.answer, 2000, `${p}.items[${j}].answer`, true); });
}
});
return out;
}
describe("page templates", () => {
it.each(PAGE_TEMPLATES)("$id passes the server's block rules", (tpl) => {
expect(problems(pageBody({ ...emptyPage(), blocks: tpl.blocks() }).blocks)).toEqual([]);
});
it("the rules catch a broken block", () => {
expect(problems([{ type: "CTA", heading: "x", buttonLabel: "Go", buttonUrl: "#nowhere" }])).toEqual(["blocks[0].buttonUrl target"]);
});
it.each(PAGE_TEMPLATES)("$id ends with a sign-up form named signup and no event", (tpl) => {
const blocks = tpl.blocks();
expect(blocks.at(-1)).toMatchObject({ type: "FORM", anchor: "signup", eventUuid: null });
});
it("the sections are as designed", () => {
expect(PAGE_TEMPLATES.map((t) => t.blocks().map((b) => b.type + (b.look ? `:${b.look}` : "")))).toEqual([
["HERO:DARK", "TRUST", "FEATURES", "STEPS:MUTED", "FAQ", "FORM:MUTED"],
["HERO:DARK", "TRUST", "FEATURES", "STEPS", "FAQ", "CTA:ACCENT", "FORM:MUTED"],
]);
});
it("each use gets fresh blocks", () => {
const [a, b] = [PAGE_TEMPLATES[0].blocks(), PAGE_TEMPLATES[0].blocks()];
expect(a[0]).not.toBe(b[0]);
});
});