Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/form-widget/src/routing.test.ts
T

96 lines
3.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { applyFieldMapping, resolveSubmissionTarget } from "./routing.js";
describe("resolveSubmissionTarget", () => {
it("defaults to generic when no target type is set", () => {
const target = resolveSubmissionTarget({});
expect(target.type).toBe("generic");
expect(target.entityId).toBeNull();
});
it("returns crm-contact when configured", () => {
const target = resolveSubmissionTarget({ targetType: "crm-contact", fieldMapping: { email: "email" } });
expect(target.type).toBe("crm-contact");
expect(target.fieldMapping.email).toBe("email");
});
it("falls back to generic when entity-row has no entityId", () => {
const target = resolveSubmissionTarget({ targetType: "entity-row" });
expect(target.type).toBe("generic");
});
it("preserves entityId for valid entity-row config", () => {
const target = resolveSubmissionTarget({ targetType: "entity-row", targetEntityId: "ent_123" });
expect(target.type).toBe("entity-row");
expect(target.entityId).toBe("ent_123");
});
it("falls back to generic for unknown target types", () => {
const target = resolveSubmissionTarget({ targetType: "rocket-launch" });
expect(target.type).toBe("generic");
});
});
describe("applyFieldMapping", () => {
it("passes payload through unchanged when no mapping is configured", () => {
const result = applyFieldMapping({ payload: { email: "a@b.co", name: "Alice" }, fieldMapping: {} });
expect(result).toEqual({ email: "a@b.co", name: "Alice" });
});
it("passes payload through when mapping is undefined", () => {
const result = applyFieldMapping({ payload: { email: "a@b.co" } });
expect(result).toEqual({ email: "a@b.co" });
});
it("renames keys according to the mapping", () => {
const result = applyFieldMapping({
payload: { firstName: "Alice", email_address: "a@b.co" },
fieldMapping: { firstName: "first_name", email_address: "email" },
});
expect(result).toEqual({ first_name: "Alice", email: "a@b.co" });
});
it("drops form fields that are not in the mapping", () => {
const result = applyFieldMapping({
payload: { firstName: "Alice", lastName: "Smith", honeypot: "" },
fieldMapping: { firstName: "first_name" },
});
expect(result).toEqual({ first_name: "Alice" });
expect(Object.prototype.hasOwnProperty.call(result, "lastName")).toBe(false);
expect(Object.prototype.hasOwnProperty.call(result, "honeypot")).toBe(false);
});
it("ignores mapping entries with empty target keys", () => {
const result = applyFieldMapping({
payload: { firstName: "Alice", email: "a@b.co" },
fieldMapping: { firstName: "", email: "email" },
});
expect(result).toEqual({ email: "a@b.co" });
});
it("preserves null/undefined values in the payload", () => {
const result = applyFieldMapping({
payload: { firstName: null, age: undefined, email: "a@b.co" } as Record<string, unknown>,
fieldMapping: { firstName: "first_name", age: "age", email: "email" },
});
expect(result.first_name).toBeNull();
expect(result.email).toBe("a@b.co");
// `age` was undefined → key dropped only if hasOwnProperty is false; here it
// was an own property explicitly set to undefined, so the helper keeps it.
expect(Object.prototype.hasOwnProperty.call(result, "age")).toBe(true);
});
it("handles an empty payload safely", () => {
expect(applyFieldMapping({ payload: {} })).toEqual({});
expect(applyFieldMapping({ payload: null })).toEqual({});
expect(applyFieldMapping({ payload: undefined })).toEqual({});
});
it("returns a shallow copy — mutating the result does not affect input", () => {
const payload = { email: "a@b.co" };
const result = applyFieldMapping({ payload });
result.email = "changed";
expect(payload.email).toBe("a@b.co");
});
});