import { describe, it, expect } from "vitest"; import { ICON_NAMES } from "@/app/components/app/icon-map"; import { draftFromView, emptyForm, FIELD_LABELS, FIELD_TYPES, formBody, moveField, newField, type CmsFormView } from "./cmsForms"; // add-cms-forms Task 8: pure helpers behind the form editor. describe("cmsForms", () => { // localise-admin-content: labels are message codes, translated with t() where they render. it("offers the six field types with admin label codes", () => { expect(FIELD_TYPES).toEqual(["TEXT", "TEXTAREA", "EMAIL", "PHONE", "SELECT", "CHECKBOX"]); expect(FIELD_TYPES.map((t) => FIELD_LABELS[t])).toEqual(["FrmFieldTypeText", "FrmFieldTypeTextarea", "Email", "Phone", "FrmFieldTypeSelect", "FrmFieldTypeCheckbox"]); }); it("starts a new form with the default label and message and one Name field", () => { const draft = emptyForm(); expect(draft).toMatchObject({ name: "", submitLabel: "Send", successMessage: "Thanks! We'll be in touch soon.", notifyEmailsText: "", status: "Y" }); expect(draft.fields).toHaveLength(1); expect(draft.fields[0]).toMatchObject({ label: "Name", type: "TEXT", required: true, optionsText: "" }); }); it("gives new fields unique keys the server accepts", () => { const keys = new Set(Array.from({ length: 50 }, () => newField("TEXT").key)); expect(keys.size).toBe(50); for (const key of keys) expect(key).toMatch(/^[a-z0-9]{1,16}$/); }); it("splits emails and options and drops options from non-dropdown fields", () => { const draft = emptyForm(); draft.id = 3; draft.name = " Contact us "; draft.notifyEmailsText = " a@babyboo.my, ,b@babyboo.my\n"; draft.fields = [ { key: "name", label: " Name ", type: "TEXT", required: true, optionsText: "stale" }, { key: "topic", label: "Topic", type: "SELECT", required: false, optionsText: " Order \n\nBilling\n" }, ]; expect(formBody(draft)).toEqual({ id: 3, name: "Contact us", submitLabel: "Send", successMessage: "Thanks! We'll be in touch soon.", notifyEmails: ["a@babyboo.my", "b@babyboo.my"], status: "Y", fields: [ { key: "name", label: "Name", type: "TEXT", required: true }, { key: "topic", label: "Topic", type: "SELECT", required: false, options: ["Order", "Billing"] }, ], }); }); it("round-trips a saved form into a draft", () => { const view: CmsFormView = { id: 3, name: "Contact us", submitLabel: "Send", successMessage: "Thanks!", notifyEmails: ["a@babyboo.my", "b@babyboo.my"], status: "N", fields: [{ key: "topic", label: "Topic", type: "SELECT", required: true, options: ["Order", "Billing"] }], }; const draft = draftFromView(view); expect(draft).toMatchObject({ id: 3, notifyEmailsText: "a@babyboo.my, b@babyboo.my", status: "N" }); expect(draft.fields[0]).toEqual({ key: "topic", label: "Topic", type: "SELECT", required: true, optionsText: "Order\nBilling" }); expect(formBody(draft).fields).toEqual(view.fields); }); it("moves a field up and down within bounds", () => { const a = newField("TEXT"); const b = newField("EMAIL"); expect(moveField([a, b], 1, -1)).toEqual([b, a]); expect(moveField([a, b], 0, -1)).toEqual([a, b]); expect(moveField([a, b], 1, 1)).toEqual([a, b]); }); it("has the seeded ClipboardList icon in the sidebar icon map", () => { expect(ICON_NAMES).toContain("ClipboardList"); }); });