Files
co-lite/solvway-admin-portal/app/lib/cmsForms.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

79 lines
3.4 KiB
TypeScript

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");
});
});