import { describe, it, expect } from "vitest"; import { BLOCK_LABELS, BLOCK_TYPES, emptyBlock, emptyPage, moveBlock, pageAddress, pageBody, withTitle } from "./cmsPages"; // add-cms-pages: pure helpers behind the page editor. describe("pageAddress", () => { it("gives the four help pages their short address and everything else /pages/{slug}", () => { expect(pageAddress("help")).toBe("/help"); expect(pageAddress("contact")).toBe("/contact"); expect(pageAddress("delivery-returns")).toBe("/delivery-returns"); expect(pageAddress("warranty")).toBe("/warranty"); expect(pageAddress("about-us")).toBe("/pages/about-us"); }); }); describe("withTitle", () => { it("fills the slug from the title until the slug was edited by hand", () => { const draft = withTitle(emptyPage(), "About Us & Our Story!"); expect(draft.slug).toBe("about-us-our-story"); const edited = { ...draft, slug: "our-story", slugTouched: true }; expect(withTitle(edited, "Something else").slug).toBe("our-story"); }); }); describe("blocks", () => { // localise-admin-content: labels are message codes, translated with t() where they render. it("labels all seven block types (FORM added by add-cms-forms)", () => { expect(BLOCK_TYPES).toEqual(["HERO", "RICH_TEXT", "IMAGE_TEXT", "FEATURED_PRODUCTS", "COLLECTION_LIST", "FAQ", "FORM"]); expect(BLOCK_LABELS).toEqual({ HERO: "CmsBlockHero", RICH_TEXT: "CmsBlockRichText", IMAGE_TEXT: "CmsBlockImageText", FEATURED_PRODUCTS: "CmsBlockFeaturedProducts", COLLECTION_LIST: "CmsBlockCollectionList", FAQ: "CmsBlockFaq", FORM: "CmsBlockForm", }); }); it("creates empty blocks with sensible defaults", () => { expect(emptyBlock("FAQ")).toEqual({ type: "FAQ", heading: "", items: [{ question: "", answer: "" }] }); expect(emptyBlock("IMAGE_TEXT")).toMatchObject({ type: "IMAGE_TEXT", imagePosition: "LEFT", imageUrl: "", html: "" }); expect(emptyBlock("FEATURED_PRODUCTS")).toMatchObject({ type: "FEATURED_PRODUCTS", collectionId: null, limit: 8 }); expect(emptyBlock("COLLECTION_LIST")).toMatchObject({ type: "COLLECTION_LIST", collectionIds: [] }); }); it("moves a block up or down and does nothing at either end", () => { const blocks = [emptyBlock("HERO"), emptyBlock("RICH_TEXT"), emptyBlock("FAQ")]; expect(moveBlock(blocks, 1, -1).map((b) => b.type)).toEqual(["RICH_TEXT", "HERO", "FAQ"]); expect(moveBlock(blocks, 1, 1).map((b) => b.type)).toEqual(["HERO", "FAQ", "RICH_TEXT"]); expect(moveBlock(blocks, 0, -1)).toEqual(blocks); expect(moveBlock(blocks, 2, 1)).toEqual(blocks); expect(blocks.map((b) => b.type)).toEqual(["HERO", "RICH_TEXT", "FAQ"]); }); }); describe("pageBody", () => { it("sends only the saved fields, trimmed, with blank optional texts dropped", () => { const body = pageBody({ ...emptyPage(), id: 4, title: " About us ", slug: "about-us", metaDescription: " ", status: "N", blocks: [{ ...emptyBlock("HERO"), imageUrl: "https://cdn/x.png", heading: " Hi ", text: "", buttonLabel: "", buttonUrl: "" }], }); expect(body).toEqual({ id: 4, title: "About us", slug: "about-us", metaDescription: null, status: "N", blocks: [{ type: "HERO", imageUrl: "https://cdn/x.png", heading: "Hi" }], }); }); });