shoc-frontend-new/src/test/domain/work-orders/mappers/additional-contacts-mapper.test.ts
arthur.bassi d93ae683c7 fix(work-orders): restore additionalContacts contract and name+phone extras (SH-218)
Document the backend #66 producer, drop incomplete extras from persist, and show the same required-field errors on POC 2+.
2026-08-18 10:03:14 -03:00

87 lines
2.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
isIncompleteAdditionalContact,
normalizeAdditionalContacts,
readAdditionalContacts,
serializeAdditionalContactsForPatch,
} from "@/domain/work-orders/mappers/additional-contacts-mapper";
describe("readAdditionalContacts", () => {
it("returns [] when field is missing", () => {
expect(readAdditionalContacts({ id: 1 })).toEqual([]);
});
it("maps camelCase and PascalCase arrays", () => {
expect(
readAdditionalContacts({
additionalContacts: [{ name: "Alex", phone: "555-0100", notes: "Gate code" }],
}),
).toEqual([{ name: "Alex", phone: "555-0100", notes: "Gate code" }]);
expect(
readAdditionalContacts({
AdditionalContacts: [{ Name: "Sam", Phone: "555-0200" }],
}),
).toEqual([{ name: "Sam", phone: "555-0200" }]);
});
it("drops empty rows", () => {
expect(
readAdditionalContacts({
additionalContacts: [
{ name: "", phone: "" },
{ name: "Pat", phone: "111" },
],
}),
).toEqual([{ name: "Pat", phone: "111" }]);
});
});
describe("normalizeAdditionalContacts", () => {
it("trims and omits empty notes", () => {
expect(
normalizeAdditionalContacts([
{ name: " Alex ", phone: " 555 ", notes: " " },
{ name: "", phone: "" },
]),
).toEqual([{ name: "Alex", phone: "555" }]);
});
it("returns undefined when all rows are empty", () => {
expect(normalizeAdditionalContacts([{ name: "", phone: "" }])).toBeUndefined();
});
it("drops name-only and phone-only rows", () => {
expect(
normalizeAdditionalContacts([
{ name: "Alex", phone: "" },
{ name: "", phone: "555-0100" },
{ name: "Pat", phone: "111" },
]),
).toEqual([{ name: "Pat", phone: "111" }]);
});
});
describe("isIncompleteAdditionalContact", () => {
it("is true when exactly one of name or phone is filled", () => {
expect(isIncompleteAdditionalContact({ name: "Alex", phone: "" })).toBe(true);
expect(isIncompleteAdditionalContact({ name: "", phone: "555" })).toBe(true);
expect(isIncompleteAdditionalContact({ name: "Alex", phone: "555" })).toBe(false);
expect(isIncompleteAdditionalContact({ name: "", phone: "" })).toBe(false);
});
});
describe("serializeAdditionalContactsForPatch", () => {
it("JSON-stringifies normalized contacts for board PATCH", () => {
expect(
serializeAdditionalContactsForPatch([
{ name: "Alex", phone: "555-0100" },
{ name: "", phone: "" },
]),
).toBe('[{"name":"Alex","phone":"555-0100"}]');
});
it("serializes empty array when nothing to persist", () => {
expect(serializeAdditionalContactsForPatch([])).toBe("[]");
});
});