Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/form-widget/src/routing.extra.test.ts
T
2026-07-08 16:15:37 +02:00

72 lines
2.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { applyFieldMapping, resolveSubmissionTarget } from "./routing.js";
/**
* Edge-case coverage for the submission-routing resolver: descriptor
* immutability, null/empty target handling and the mapping branches that
* drop missing source keys, resolve duplicate targets and guard against
* non-string target keys.
*/
describe("resolveSubmissionTarget — descriptor shape", () => {
it("returns a frozen field mapping that cannot be mutated", () => {
const target = resolveSubmissionTarget({ targetType: "crm-contact", fieldMapping: { email: "email" } });
expect(Object.isFrozen(target.fieldMapping)).toBe(true);
});
it("freezes the empty mapping on the unknown-target fallback path", () => {
const target = resolveSubmissionTarget({ targetType: "entity-row" });
expect(target.type).toBe("generic");
expect(Object.isFrozen(target.fieldMapping)).toBe(true);
expect(target.fieldMapping).toEqual({});
});
it("forwards an entityId on a crm-contact target", () => {
const target = resolveSubmissionTarget({ targetType: "crm-contact", targetEntityId: "contact_1" });
expect(target.type).toBe("crm-contact");
expect(target.entityId).toBe("contact_1");
});
it("treats an explicit null target type as generic", () => {
expect(resolveSubmissionTarget({ targetType: null }).type).toBe("generic");
});
it("treats an empty-string target type as generic", () => {
expect(resolveSubmissionTarget({ targetType: "" }).type).toBe("generic");
});
it("copies the provided field mapping instead of aliasing it", () => {
const mapping = { firstName: "first_name" };
const target = resolveSubmissionTarget({ targetType: "crm-contact", fieldMapping: mapping });
mapping.firstName = "changed";
expect(target.fieldMapping.firstName).toBe("first_name");
});
});
describe("applyFieldMapping — mapping branches", () => {
it("drops mapping entries whose source key is absent from the payload", () => {
const result = applyFieldMapping({
payload: { email: "a@b.co" },
fieldMapping: { phone: "phone_number", email: "email" },
});
expect(result).toEqual({ email: "a@b.co" });
expect(Object.prototype.hasOwnProperty.call(result, "phone_number")).toBe(false);
});
it("lets the later source win when two fields map to the same target", () => {
// Object.entries preserves insertion order → 'b' overwrites 'a' on target 'x'.
const result = applyFieldMapping({
payload: { a: "first", b: "second" },
fieldMapping: { a: "x", b: "x" },
});
expect(result).toEqual({ x: "second" });
});
it("ignores mapping entries whose target key is not a string", () => {
const result = applyFieldMapping({
payload: { a: "1" },
fieldMapping: { a: 123 } as unknown as Record<string, string>,
});
expect(result).toEqual({});
});
});