From f666ee215d8ee9a1a0cfaff7d3c43622f962a404 Mon Sep 17 00:00:00 2001 From: Arthur Bassi Date: Tue, 11 Aug 2026 15:15:02 -0300 Subject: [PATCH] test(work-orders): cover site dialog POC autofill races --- .../workorders/use-site-dialog-state.test.tsx | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/test/app/(protected)/workorders/use-site-dialog-state.test.tsx diff --git a/src/test/app/(protected)/workorders/use-site-dialog-state.test.tsx b/src/test/app/(protected)/workorders/use-site-dialog-state.test.tsx new file mode 100644 index 00000000..dc0adb78 --- /dev/null +++ b/src/test/app/(protected)/workorders/use-site-dialog-state.test.tsx @@ -0,0 +1,112 @@ +import { act, renderHook } from "@testing-library/react"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { useSiteDialogState } from "@/app/(protected)/workorders/_components/list/table/cells/use-site-dialog-state"; +import type { Location } from "@/domain/locations/types/location"; +import type { LocationOption } from "@/domain/work-orders/types/work-order"; + +const detailById = new Map(); + +vi.mock("@/domain/locations/use-cases/use-location-detail", () => ({ + useLocationDetail: (id: string | undefined) => ({ + data: id && detailById.has(id) ? detailById.get(id) : undefined, + isLoading: Boolean(id && !detailById.has(id)), + isError: false, + }), +})); + +const SITES: LocationOption[] = [ + { id: "loc-1", name: "BK5 · Dallas", siteCode: "BK5", city: "Dallas", state: "TX" }, + { id: "loc-2", name: "SEA1 · Seattle", siteCode: "SEA1", city: "Seattle", state: "WA" }, +]; + +function locationDetail(id: string, contact: string, phone: string): Location { + return { + id, + name: id, + contact, + phone, + address: "100 Main St", + city: "Dallas", + state: "TX", + zipCode: "75001", + }; +} + +function renderSiteDialogState() { + return renderHook(() => + useSiteDialogState({ + open: true, + onOpenChange: vi.fn(), + value: "", + locationId: "", + pocName: "", + pocPhone: "", + pocNotes: "", + sites: SITES, + createMode: true, + onSave: vi.fn(), + }), + ); +} + +describe("useSiteDialogState POC autofill", () => { + beforeEach(() => { + detailById.clear(); + }); + + it("preserves POC typed before deferred location detail resolves", () => { + const { result, rerender } = renderSiteDialogState(); + + act(() => { + result.current.handlePick("loc-1"); + }); + + act(() => { + result.current.setPn("Dispatcher Typed"); + result.current.setPp("999-000-1111"); + }); + + expect(result.current.pn).toBe("Dispatcher Typed"); + expect(result.current.pp).toBe("999-000-1111"); + + detailById.set("loc-1", locationDetail("loc-1", "Site Contact", "214-555-0111")); + act(() => { + rerender(); + }); + + expect(result.current.pn).toBe("Dispatcher Typed"); + expect(result.current.pp).toBe("999-000-1111"); + }); + + it("clears prior POC on site switch then autofills from the new site detail", () => { + const { result, rerender } = renderSiteDialogState(); + + act(() => { + result.current.handlePick("loc-1"); + }); + + detailById.set("loc-1", locationDetail("loc-1", "Contact A", "111-111-1111")); + act(() => { + rerender(); + }); + + expect(result.current.pn).toBe("Contact A"); + expect(result.current.pp).toBe("111-111-1111"); + + act(() => { + result.current.handlePick("loc-2"); + }); + + expect(result.current.locId).toBe("loc-2"); + expect(result.current.pn).toBe(""); + expect(result.current.pp).toBe(""); + + detailById.set("loc-2", locationDetail("loc-2", "Contact B", "222-222-2222")); + act(() => { + rerender(); + }); + + expect(result.current.pn).toBe("Contact B"); + expect(result.current.pp).toBe("222-222-2222"); + }); +});