2026-08-13 11:04:48 -03:00
|
|
|
import { fireEvent, screen, waitFor } from "@testing-library/react";
|
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
|
import { NewWoWizard } from "@/app/(protected)/workorders/_components/wizard/new-wo-wizard";
|
|
|
|
|
import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-table-row";
|
|
|
|
|
import { renderWithProviders } from "@/test/test-utils";
|
|
|
|
|
|
|
|
|
|
const existingRow = { id: "wo-99", woNumber: "12345678901" } as WorkOrderTableRow;
|
|
|
|
|
const findByWoNumber = vi.fn();
|
|
|
|
|
|
|
|
|
|
vi.mock("@/domain/work-orders/use-cases/use-create-work-order-from-wizard", () => ({
|
|
|
|
|
useCreateWorkOrderFromWizard: () => ({
|
|
|
|
|
mutate: vi.fn(),
|
|
|
|
|
isPending: false,
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/domain/work-orders/use-cases/use-work-order-detail", () => ({
|
|
|
|
|
useWorkOrderVendors: () => ({ data: [] }),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-08-13 18:43:11 -03:00
|
|
|
vi.mock("@/domain/locations/use-cases/use-location-sites", () => ({
|
|
|
|
|
useLocationSites: () => ({ data: [] }),
|
|
|
|
|
}));
|
|
|
|
|
|
2026-08-13 11:04:48 -03:00
|
|
|
vi.mock("@/domain/locations/use-cases/use-location-detail", () => ({
|
|
|
|
|
useLocationDetail: () => ({ data: undefined }),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/domain/settings/dropdown-options/use-cases/use-dropdown-options-by-category", () => ({
|
|
|
|
|
useDropdownOptionsByCategory: () => ({ data: [] }),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/domain/work-orders/api/work-orders-api", () => ({
|
|
|
|
|
workOrdersApi: {
|
|
|
|
|
findByWoNumber: (...args: unknown[]) => findByWoNumber(...args),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
describe("NewWoWizard duplicate wo number", () => {
|
|
|
|
|
it("shows the duplicate dialog when the wizard wo number matches an existing row", async () => {
|
|
|
|
|
renderWithProviders(
|
|
|
|
|
<NewWoWizard
|
|
|
|
|
open
|
|
|
|
|
onOpenChange={vi.fn()}
|
|
|
|
|
users={[]}
|
|
|
|
|
existingRows={[existingRow]}
|
|
|
|
|
onViewExistingWo={vi.fn()}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
2026-08-14 11:59:52 -03:00
|
|
|
const woNumberInput = screen.getByPlaceholderText("Leave blank for SH provisional number");
|
2026-08-13 11:04:48 -03:00
|
|
|
fireEvent.change(woNumberInput, {
|
|
|
|
|
target: { value: "12345678901" },
|
|
|
|
|
});
|
|
|
|
|
fireEvent.blur(woNumberInput);
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
expect(screen.getByText("Duplicate Work Order Number")).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
expect(screen.getByText(/is already assigned to another work order/i)).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("finds a duplicate outside the loaded board rows", async () => {
|
|
|
|
|
const offPageRow = {
|
|
|
|
|
id: "wo-off-page",
|
|
|
|
|
woNumber: "98765432109",
|
|
|
|
|
} as WorkOrderTableRow;
|
|
|
|
|
findByWoNumber.mockResolvedValueOnce(offPageRow);
|
|
|
|
|
|
|
|
|
|
renderWithProviders(
|
|
|
|
|
<NewWoWizard
|
|
|
|
|
open
|
|
|
|
|
onOpenChange={vi.fn()}
|
|
|
|
|
users={[]}
|
|
|
|
|
existingRows={[]}
|
|
|
|
|
onViewExistingWo={vi.fn()}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
2026-08-14 11:59:52 -03:00
|
|
|
const woNumberInput = screen.getByPlaceholderText("Leave blank for SH provisional number");
|
2026-08-13 11:04:48 -03:00
|
|
|
fireEvent.change(woNumberInput, {
|
|
|
|
|
target: { value: "98765432109" },
|
|
|
|
|
});
|
|
|
|
|
fireEvent.blur(woNumberInput);
|
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
|
expect(screen.getByText("Duplicate Work Order Number")).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
expect(findByWoNumber).toHaveBeenCalledWith("98765432109");
|
|
|
|
|
});
|
|
|
|
|
});
|