shoc-frontend-new/src/test/domain/locations/mappers/location-mapper.test.ts

163 lines
5.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
mapLocation,
mapLocationListItem,
mapLocationToBackend,
mapSiteOpenWorkOrders,
} 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 site payload", () => {
it("sends trimmed contacts in displayed order and names the first as the legacy contact", () => {
const payload = mapLocationToBackend({
name: " dal1 ",
address: "3811 Distribution Dr",
city: "Dallas",
state: "TX",
zipCode: "70068",
contactEmail: "jane@example.com",
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.name).toBe("dal1");
});
it("sends the Site Phone as phone, never the main contact's phone", () => {
const payload = mapLocationToBackend({
sitePhone: " (214) 555-0100 ",
contacts: [{ name: "Jane", phone: "(421) 433-0032" }],
});
const withoutSitePhone = mapLocationToBackend({
contacts: [{ name: "Jane", phone: "(421) 433-0032" }],
});
expect(payload.phone).toBe("(214) 555-0100");
expect(withoutSitePhone.phone).toBe("");
});
it("omits status, title and notes unless given, so an update keeps the stored values", () => {
const bare = mapLocationToBackend({ name: "DAL1", contacts: [] });
const full = mapLocationToBackend({
name: "DAL1",
status: "Active",
title: "Dallas DC",
notes: "Gate code 1234",
contacts: [],
});
expect(bare).not.toHaveProperty("status");
expect(bare).not.toHaveProperty("title");
expect(bare).not.toHaveProperty("notes");
expect(full).toMatchObject({ status: "Active", title: "Dallas DC", notes: "Gate code 1234" });
});
it("keeps the legacy contact name when the caller sends no contacts list", () => {
const addressOnly = mapLocationToBackend({
name: "DAL1",
address: "3811 Distribution Dr",
contact: "Jane Doe",
});
const withContacts = mapLocationToBackend({
contact: "Jane Doe",
contacts: [{ name: "Bob", phone: "(421) 433-0033" }],
});
expect(addressOnly.contact).toBe("Jane Doe");
expect(withContacts.contact).toBe("Bob");
});
});
describe("mapLocation site fields", () => {
it("reads client name, site phone, title and notes separately from the main contact", () => {
const detail = mapLocation({
id: 9,
name: "DAL1",
title: "Dallas DC",
clientName: "Acme",
phone: "(214) 555-0100",
notes: "Gate code 1234",
contacts: [{ id: 31, name: "Carol", phone: "555-0143" }],
});
const row = mapLocationListItem({ id: 9, accountName: "Acme", phone: "(214) 555-0100" });
expect(detail).toMatchObject({
clientName: "Acme",
sitePhone: "(214) 555-0100",
title: "Dallas DC",
notes: "Gate code 1234",
phone: "555-0143",
});
expect(row).toMatchObject({ clientName: "Acme", sitePhone: "(214) 555-0100" });
});
});
describe("mapSiteOpenWorkOrders", () => {
it("keeps the server count and drops invalid ids", () => {
expect(mapSiteOpenWorkOrders({ count: 240, workOrderIds: [101, "x", -3, 205] })).toEqual({
count: 240,
workOrderIds: [101, 205],
});
expect(mapSiteOpenWorkOrders({ Count: 0, WorkOrderIds: [] })).toEqual({
count: 0,
workOrderIds: [],
});
});
});