shoc-frontend-new/src/test/app/(protected)/workorders/aveta-service-notes.test.tsx

181 lines
5.6 KiB
TypeScript

import { fireEvent, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { PMTypeCell } from "@/app/(protected)/workorders/_components/list/table/cells/pm-type-cell";
import { ServiceNotesDialog } from "@/app/(protected)/workorders/_components/list/table/cells/service-notes-dialog";
import { serviceIdentityPatch } from "@/domain/work-orders/utils/service-identity-patch";
import { renderWithProviders } from "@/test/test-utils";
describe("Aveta Service & Notes", () => {
it("shows eligible registry services and saves the selected service identity", () => {
const onSave = vi.fn();
renderWithProviders(
<PMTypeCell
value="Old snapshot"
serviceId={null}
pmTypes={["Leak", "No Power", "Not Cooling", "Other"]}
services={[
{
id: 701,
name: "QA Test Service",
trade: "Electrical",
iconKey: "wrench",
requiresCompletionDocument: false,
completionDocTemplate: null,
isActive: true,
supportedWorkOrderTypes: ["PM"],
},
{
id: 702,
name: "Reactive Only",
trade: "Electrical",
iconKey: "wrench",
requiresCompletionDocument: false,
completionDocTemplate: null,
isActive: true,
supportedWorkOrderTypes: ["Reactive"],
},
]}
workOrderType="PM"
onSave={onSave}
/>,
);
fireEvent.click(screen.getByRole("button", { name: /Old snapshot/i }));
expect(screen.getByRole("button", { name: "QA Test Service" })).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Leak" })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Reactive Only" })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "QA Test Service" }));
expect(onSave).toHaveBeenCalledWith("QA Test Service", 701);
});
it("keeps the historical label when its linked registry service is inactive", () => {
renderWithProviders(
<PMTypeCell
value="Historic Service Name"
serviceId={701}
pmTypes={[]}
services={[
{
id: 701,
name: "Renamed Service",
trade: "Electrical",
iconKey: "wrench",
requiresCompletionDocument: false,
completionDocTemplate: null,
isActive: false,
supportedWorkOrderTypes: ["PM"],
},
]}
workOrderType="PM"
onSave={vi.fn()}
/>,
);
expect(screen.getByRole("button", { name: /Historic Service Name/i })).toBeInTheDocument();
expect(screen.queryByRole("button", { name: /Renamed Service/i })).not.toBeInTheDocument();
});
it("preserves the historic identity when only service notes change", () => {
const onSaveDetails = vi.fn();
renderWithProviders(
<PMTypeCell
value="Historic Service Name"
serviceId={701}
pmTypes={[]}
serviceNotes=""
services={[
{
id: 701,
name: "Renamed Service",
trade: "Electrical",
iconKey: "wrench",
requiresCompletionDocument: false,
completionDocTemplate: null,
isActive: false,
supportedWorkOrderTypes: ["PM"],
},
]}
workOrderType="PM"
onSave={vi.fn()}
onSaveDetails={onSaveDetails}
/>,
);
fireEvent.click(screen.getByRole("button", { name: /Historic Service Name/i }));
fireEvent.click(screen.getByRole("button", { name: /Add service and notes/i }));
fireEvent.change(
screen.getByPlaceholderText(
"Describe the specific issue, location details, door number, etc.",
),
{ target: { value: "Changed detail only" } },
);
fireEvent.click(screen.getByRole("button", { name: "Save" }));
const saved = onSaveDetails.mock.calls[0]![0];
expect(saved).toMatchObject({ pm: "Historic Service Name", serviceId: 701 });
expect(serviceIdentityPatch(701, "Historic Service Name", saved.pm, saved.serviceId)).toEqual(
{},
);
});
it("shows the Aveta shield on the Service cell when required", () => {
renderWithProviders(
<PMTypeCell
value="HVAC"
pmTypes={["HVAC"]}
serviceNotes=""
extraServices={[]}
avetaRequired
onSave={vi.fn()}
onSaveDetails={vi.fn()}
/>,
);
expect(screen.getByLabelText("Aveta required")).toBeInTheDocument();
});
it("shows the green Aveta shield when the document is attached", () => {
renderWithProviders(
<PMTypeCell
value="HVAC"
pmTypes={["HVAC"]}
serviceNotes=""
extraServices={[]}
avetaRequired
avetaAttached
onSave={vi.fn()}
onSaveDetails={vi.fn()}
/>,
);
expect(screen.getByLabelText("Aveta attached")).toBeInTheDocument();
});
it("persists avetaRequired from Service & Notes", () => {
const onSave = vi.fn();
renderWithProviders(
<ServiceNotesDialog
open
onOpenChange={vi.fn()}
pm="HVAC"
extraServices={[]}
serviceNotes=""
avetaRequired={false}
pmTypes={["HVAC"]}
onSave={onSave}
/>,
);
fireEvent.click(screen.getByRole("checkbox", { name: /Requires Aveta documentation/i }));
fireEvent.click(screen.getByRole("button", { name: "Save" }));
expect(onSave).toHaveBeenCalledWith(
expect.objectContaining({
pm: "HVAC",
avetaRequired: true,
}),
);
});
});