shoc-frontend-new/src/test/domain/locations/mappers/location-mapper.test.ts
Alexandre Brandizzi 07734d8361
Some checks are pending
Frontend checks / Build and test (push) Waiting to run
Frontend checks / governance (push) Waiting to run
Frontend checks / Visual regression (push) Waiting to run
Deploy dev content / Deploy shoc-frontend-new-dev through Terraform (push) Waiting to run
SH-138: manage ordered site contacts (#191)
* feat(locations): derive site areas from state

* chore(locations): keep ticket keys out of source comments

* feat(locations): manage ordered site contacts

---------

Co-authored-by: Codex Review Integration <codex-review@local.invalid>
2026-09-16 14:53:58 -03:00

94 lines
2.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
mapLocation,
mapLocationListItem,
mapLocationToBackend,
} from "@/domain/locations/mappers/location-mapper";
describe("location contacts hydration", () => {
it("maps the response contacts array and derives the main contact fields", () => {
const item = mapLocationListItem({
id: 1,
name: "Dallas DC",
contacts: [
{ name: "Jane", phone: "(421) 433-0032" },
{ name: "Bob", phone: "(421) 433-0033" },
],
});
expect(item.contacts).toEqual([
{ name: "Jane", phone: "(421) 433-0032" },
{ name: "Bob", phone: "(421) 433-0033" },
]);
expect(item.contact).toBe("Jane");
expect(item.phone).toBe("(421) 433-0032");
});
it("falls back to legacy contact/phone fields when no contacts array exists", () => {
const item = mapLocationListItem({
id: 2,
contact: "Bob",
phone: "(312) 555-0100",
});
expect(item.contacts).toBeUndefined();
expect(item.contact).toBe("Bob");
expect(item.phone).toBe("(312) 555-0100");
});
it("hydrates detail contacts and mirrors the first row in contact/phone", () => {
const detail = mapLocation({
id: 3,
contacts: [{ id: 31, name: "Carol", phone: "555-0143" }],
});
expect(detail.contacts).toEqual([{ id: 31, name: "Carol", phone: "555-0143" }]);
expect(detail.contact).toBe("Carol");
expect(detail.phone).toBe("555-0143");
});
it("keeps legacy detail contact/phone for zero-contact data", () => {
const detail = mapLocation({ id: 4, contact: "Legacy Person", phone: "555-0199" });
expect(detail.contacts).toBeUndefined();
expect(detail.contact).toBe("Legacy Person");
expect(detail.phone).toBe("555-0199");
});
});
describe("mapLocationToBackend contacts payload", () => {
it("sends trimmed contacts in displayed order and derives legacy fields from the first", () => {
const payload = mapLocationToBackend({
name: "Dallas DC",
address: "3811 Distribution Dr",
city: "Dallas",
state: "TX",
zipCode: "70068",
contactEmail: "jane@example.com",
status: "Active",
contacts: [
{ id: 41, name: " Jane ", phone: " (421) 433-0032 " },
{ name: "Bob", phone: "(421) 433-0033" },
],
});
expect(payload.contacts).toEqual([
{ id: 41, name: "Jane", phone: "(421) 433-0032" },
{ name: "Bob", phone: "(421) 433-0033" },
]);
expect(payload.contact).toBe("Jane");
expect(payload.phone).toBe("(421) 433-0032");
});
it("keeps legacy contact/phone untouched when no contacts are provided", () => {
const payload = mapLocationToBackend({
name: "Legacy DC",
contact: "Legacy Person",
phone: "555-0199",
});
expect(payload.contact).toBe("Legacy Person");
expect(payload.phone).toBe("555-0199");
expect(payload.contacts).toEqual([]);
});
});