shoc-frontend-new/src/test/app/(protected)/workorders/use-comp-doc-dialog-address-seed.test.tsx
Arthur Bassi 116f65ca48 feat(work-orders): consume completion freeze snapshot on the board
Prefer FrozenSite, FrozenCompany, and FrozenPoc on Completed rows.
2026-09-10 15:41:01 -03:00

197 lines
5.1 KiB
TypeScript

import { act, renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ReactNode } from "react";
import { useCompDocDialog } from "@/app/(protected)/workorders/_hooks/use-comp-doc-dialog";
import type { Location } from "@/domain/locations/types/location";
import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-table-row";
const locationDetailState = vi.hoisted(() => ({
data: undefined as Location | undefined,
}));
const updateLocation = vi.hoisted(() => vi.fn());
vi.mock("@/domain/locations/use-cases/use-location-detail", () => ({
useLocationDetail: () => ({ data: locationDetailState.data }),
}));
vi.mock("@/domain/locations/api/locations-api", () => ({
locationsApi: { update: updateLocation },
}));
function baseRow(overrides: Partial<WorkOrderTableRow> = {}): WorkOrderTableRow {
const defaults: WorkOrderTableRow = {
id: 42,
woNumber: "WO-42",
site: "SEA1",
type: "PM",
severity: null,
dispatcherId: "u1",
dispatcherName: "Dispatcher",
scheduledOn: "2026-06-01",
completedDate: "",
targetWeek: "",
dayGroup: "",
dayLabel: "",
location: "Seattle",
locationId: "loc-real-1",
pm: "HVAC",
serviceNotes: "",
extraServices: [],
vendorId: "",
company: "Vendor Co",
tech: "Tech One",
techPhone: "",
apptStart: "",
apptEnd: "",
apptTime: "",
status: "Scheduled",
docStatus: "No",
pocName: "Amazon Rep",
pocPhone: "",
pocNotes: "",
additionalContacts: [],
dueDate: "",
rescheduleCount: 0,
carriedOver: 0,
originalWeek: "",
originalDate: "",
isPastDue: false,
scheduleWeekOnly: false,
mediaCount: 0,
title: "PM — SEA1",
priority: "Sev 3",
primaryDispatchId: null,
rowVersion: "",
dispatchRowVersion: "",
flagColor: null,
isAddOn: false,
upliftSummary: { hasUplift: false, pendingCount: 0 },
};
return {
...defaults,
...overrides,
severity: overrides.severity !== undefined ? overrides.severity : defaults.severity,
};
}
function wrapper({ children }: { children: ReactNode }) {
const client = new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
});
return <QueryClientProvider client={client}>{children}</QueryClientProvider>;
}
describe("useCompDocDialog address seeding", () => {
beforeEach(() => {
locationDetailState.data = undefined;
updateLocation.mockReset();
});
it("seeds address from the first authoritative locationDetail without locking on the empty fallback", async () => {
const row = baseRow();
const { result, rerender } = renderHook(
() =>
useCompDocDialog({
row,
open: true,
onSave: vi.fn(),
}),
{ wrapper },
);
expect(result.current.editAddress).toBe("");
locationDetailState.data = {
id: "loc-real-1",
name: "SEA1",
address: "3811 Distribution Dr",
city: "Seattle",
state: "WA",
zipCode: "98101",
} as Location;
rerender();
await waitFor(() => {
expect(result.current.editAddress).toContain("3811 Distribution Dr");
});
});
it("does not overwrite address after the dispatcher edits it", async () => {
locationDetailState.data = {
id: "loc-real-1",
name: "SEA1",
address: "3811 Distribution Dr",
city: "Seattle",
state: "WA",
zipCode: "98101",
} as Location;
const row = baseRow();
const { result, rerender } = renderHook(
() =>
useCompDocDialog({
row,
open: true,
onSave: vi.fn(),
}),
{ wrapper },
);
await waitFor(() => {
expect(result.current.editAddress).toContain("3811 Distribution Dr");
});
act(() => {
result.current.setEditAddress("Dispatcher typed address");
});
locationDetailState.data = {
id: "loc-real-1",
name: "SEA1",
address: "999 Other St",
city: "Seattle",
state: "WA",
zipCode: "98101",
} as Location;
rerender();
expect(result.current.editAddress).toBe("Dispatcher typed address");
});
it("does not update the live location when a completed work order uses a frozen site", async () => {
const onSave = vi.fn().mockResolvedValue(undefined);
const row = baseRow({
status: "Completed",
frozenSite: {
address: "100 Frozen Ave",
city: "Chicago",
state: "IL",
zip: "60601",
},
});
const { result } = renderHook(
() =>
useCompDocDialog({
row,
open: true,
onSave,
}),
{ wrapper },
);
await waitFor(() => {
expect(result.current.editAddress).toBe("100 Frozen Ave");
});
act(() => {
result.current.setEditAddress("Edited document address");
});
await act(async () => {
await result.current.handleSaveDraft();
});
expect(updateLocation).not.toHaveBeenCalled();
expect(onSave).toHaveBeenCalledWith(expect.any(Object), "No");
});
});