mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 05:43:22 +00:00
AccountId is resolved from Location on the server. Create UI matches AAP: required Site, optional Service, no Account picker.
232 lines
7 KiB
TypeScript
232 lines
7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { mapWizardDraftToFormValues } from "@/domain/work-orders/mappers/wizard-draft-mapper";
|
|
import {
|
|
EMPTY_WIZARD_DRAFT,
|
|
canContinueWizardStep1,
|
|
canContinueWizardStep2,
|
|
canCreateWorkOrderFromWizard,
|
|
isWizardStepValid,
|
|
isWizardWoNumberValid,
|
|
wizardStep2ContinueHint,
|
|
} from "@/domain/work-orders/types/work-order-wizard";
|
|
|
|
describe("mapWizardDraftToFormValues", () => {
|
|
it("maps minimal draft with type and site to valid form values", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
locationId: "42",
|
|
locationName: "DFW8 · Dallas, TX",
|
|
woNumber: "24818",
|
|
assigneeName: "Sarah Chen",
|
|
status: "Incomplete" as const,
|
|
};
|
|
|
|
const values = mapWizardDraftToFormValues(draft);
|
|
|
|
expect(values.title).toBe("PM — #24818");
|
|
expect(values.workOrderNumber).toBe("24818");
|
|
expect(values.locationId).toBe("42");
|
|
expect(values.assignedTo).toBe("Sarah Chen");
|
|
expect(values.status).toBe("Open");
|
|
expect(values.priority).toBe("Sev 3");
|
|
expect(values.source).toBe("PM");
|
|
expect(values.createdDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
});
|
|
|
|
it("maps emergency type to Sev 1 priority", () => {
|
|
const values = mapWizardDraftToFormValues({
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "Emergency",
|
|
locationId: "1",
|
|
locationName: "Site A",
|
|
});
|
|
|
|
expect(values.priority).toBe("Sev 1");
|
|
expect(values.title).toBe("Emergency — Site A");
|
|
});
|
|
|
|
it("includes appointment and notes in description", () => {
|
|
const values = mapWizardDraftToFormValues({
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "Reactive",
|
|
locationId: "1",
|
|
locationName: "Site A",
|
|
apptStart: "09:00",
|
|
apptEnd: "11:00",
|
|
pocNotes: "Call ahead",
|
|
vendorName: "Roto-Rooter",
|
|
vendorNotes: "Prefers morning",
|
|
});
|
|
|
|
expect(values.description).toContain("Appt:");
|
|
expect(values.description).toContain("POC notes: Call ahead");
|
|
expect(values.description).toContain("Vendor: Roto-Rooter");
|
|
});
|
|
});
|
|
|
|
describe("wizard step validation", () => {
|
|
it("allows step 1 continue without a woNumber for backend SH generation", () => {
|
|
expect(canContinueWizardStep1(EMPTY_WIZARD_DRAFT)).toBe(true);
|
|
expect(isWizardStepValid(1, EMPTY_WIZARD_DRAFT)).toBe(true);
|
|
});
|
|
|
|
it("allows step 1 without type when woNumber is set (type defaults to PM)", () => {
|
|
const draft = { ...EMPTY_WIZARD_DRAFT, woNumber: "12345" };
|
|
expect(canContinueWizardStep1(draft)).toBe(true);
|
|
expect(isWizardStepValid(1, draft)).toBe(true);
|
|
});
|
|
|
|
it("allows step 1 when woNumber is missing even with type", () => {
|
|
const draft = { ...EMPTY_WIZARD_DRAFT, type: "PM" as const };
|
|
expect(canContinueWizardStep1(draft)).toBe(true);
|
|
expect(isWizardStepValid(1, draft)).toBe(true);
|
|
});
|
|
|
|
it("allows step 1 when type and woNumber are set (schedule optional)", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
woNumber: "12345",
|
|
};
|
|
expect(canContinueWizardStep1(draft)).toBe(true);
|
|
expect(isWizardStepValid(1, draft)).toBe(true);
|
|
});
|
|
|
|
it("blocks step 1 for Emergency without severity", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "Emergency" as const,
|
|
woNumber: "12345",
|
|
};
|
|
expect(canContinueWizardStep1(draft)).toBe(false);
|
|
});
|
|
|
|
it("allows step 1 for Emergency with severity", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "Emergency" as const,
|
|
woNumber: "12345",
|
|
severity: 2 as const,
|
|
};
|
|
expect(canContinueWizardStep1(draft)).toBe(true);
|
|
});
|
|
|
|
it("blocks step 2 without POC name/phone", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
woNumber: "12345",
|
|
locationId: "99",
|
|
siteCode: "BK5",
|
|
};
|
|
expect(canContinueWizardStep2(draft)).toBe(false);
|
|
expect(isWizardStepValid(2, draft)).toBe(false);
|
|
});
|
|
|
|
it("allows step 2 without an account when site and POC are set", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
woNumber: "12345",
|
|
locationId: "99",
|
|
siteCode: "BK5",
|
|
pocName: "Jordan",
|
|
pocPhone: "555-0100",
|
|
};
|
|
expect(canContinueWizardStep2(draft)).toBe(true);
|
|
expect(isWizardStepValid(2, draft)).toBe(true);
|
|
expect(wizardStep2ContinueHint(draft)).toBeNull();
|
|
});
|
|
|
|
it("hints pick a site when step 2 has no location", () => {
|
|
expect(wizardStep2ContinueHint(EMPTY_WIZARD_DRAFT)).toBe("Pick a site to continue");
|
|
});
|
|
|
|
it("hints POC when step 2 has site but no phone", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
locationId: "99",
|
|
siteCode: "BK5",
|
|
pocName: "Jordan",
|
|
};
|
|
expect(wizardStep2ContinueHint(draft)).toBe("Enter POC name and phone to continue");
|
|
});
|
|
|
|
it("allows step 2 when site and POC are set", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
woNumber: "12345",
|
|
locationId: "99",
|
|
siteCode: "BK5",
|
|
pocName: "Jordan",
|
|
pocPhone: "555-0100",
|
|
};
|
|
expect(canContinueWizardStep2(draft)).toBe(true);
|
|
expect(isWizardStepValid(2, draft)).toBe(true);
|
|
expect(wizardStep2ContinueHint(draft)).toBeNull();
|
|
});
|
|
|
|
it("blocks create without siteCode", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
woNumber: "12345",
|
|
locationId: "99",
|
|
};
|
|
expect(canCreateWorkOrderFromWizard(draft)).toBe(false);
|
|
expect(isWizardStepValid(2, draft)).toBe(false);
|
|
});
|
|
|
|
it("allows create when type, woNumber, site, and POC are set", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
woNumber: "12345678901234567890",
|
|
locationId: "99",
|
|
siteCode: "BK5",
|
|
pocName: "Jordan",
|
|
pocPhone: "555-0100",
|
|
};
|
|
expect(canCreateWorkOrderFromWizard(draft)).toBe(true);
|
|
expect(isWizardStepValid(2, draft)).toBe(true);
|
|
});
|
|
|
|
it("allows provisional SH-prefixed woNumber", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
woNumber: "SH12345",
|
|
};
|
|
expect(canContinueWizardStep1(draft)).toBe(true);
|
|
});
|
|
|
|
it("accepts blank woNumber for backend SH generation", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
woNumber: "",
|
|
};
|
|
expect(isWizardWoNumberValid(draft.woNumber)).toBe(true);
|
|
expect(canContinueWizardStep1(draft)).toBe(true);
|
|
});
|
|
|
|
it("accepts whitespace-only woNumber for backend SH generation", () => {
|
|
const draft = {
|
|
...EMPTY_WIZARD_DRAFT,
|
|
type: "PM" as const,
|
|
woNumber: " ",
|
|
};
|
|
expect(isWizardWoNumberValid(draft.woNumber)).toBe(true);
|
|
expect(canContinueWizardStep1(draft)).toBe(true);
|
|
});
|
|
|
|
it("rejects non-alphanumeric woNumber", () => {
|
|
expect(isWizardWoNumberValid("!!!")).toBe(false);
|
|
expect(canContinueWizardStep1({ ...EMPTY_WIZARD_DRAFT, woNumber: "!!!" })).toBe(false);
|
|
});
|
|
|
|
it("accepts alphanumeric woNumber", () => {
|
|
expect(isWizardWoNumberValid("ABC123")).toBe(true);
|
|
});
|
|
});
|