Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
162 lines
6.5 KiB
TypeScript
162 lines
6.5 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getEventCloseOut: vi.fn(),
|
|
startEventCloseOut: vi.fn(),
|
|
closeEvent: vi.fn(),
|
|
listAdminWarehouses: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminStockCounts", () => ({
|
|
getEventCloseOut: mocks.getEventCloseOut,
|
|
startEventCloseOut: mocks.startEventCloseOut,
|
|
closeEvent: mocks.closeEvent,
|
|
}));
|
|
vi.mock("@/app/lib/services/adminWarehouses", () => ({ listAdminWarehouses: mocks.listAdminWarehouses }));
|
|
|
|
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
|
|
import { EventCloseOutDialog } from "./EventCloseOutDialog";
|
|
|
|
const EVENT = {
|
|
id: "42",
|
|
code: "EXPO",
|
|
name: "Baby Expo",
|
|
venue: "Hall A",
|
|
salesFrom: "2026-10-01T09:00",
|
|
salesTo: "2026-10-03T21:00",
|
|
warehouseId: "39",
|
|
status: "Y" as const,
|
|
};
|
|
|
|
const WAREHOUSES = [
|
|
{ id: 2, code: "MYB-W", name: "MYB-HQ", type: "W", parentId: null, status: "Y" },
|
|
{ id: 5, code: "KL-M", name: "KL store", type: "M", parentId: null, status: "Y" },
|
|
{ id: 39, code: "EVT-EXPO", name: "Expo", type: "EVENT", parentId: null, status: "Y" },
|
|
{ id: 9, code: "OLD", name: "Old", type: "W", parentId: null, status: "N" },
|
|
];
|
|
|
|
const state = (phase: string, extra: Record<string, unknown> = {}) => ({
|
|
eventId: 42,
|
|
phase,
|
|
countId: null,
|
|
windowsOver: true,
|
|
openReservations: [],
|
|
suggestedReturnWarehouseId: 2,
|
|
returnWarehouseId: null,
|
|
...extra,
|
|
});
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/events");
|
|
mocks.getEventCloseOut.mockReset();
|
|
mocks.startEventCloseOut.mockReset();
|
|
mocks.closeEvent.mockReset();
|
|
mocks.listAdminWarehouses.mockReset().mockResolvedValue(WAREHOUSES);
|
|
});
|
|
|
|
const renderDialog = (onClosed = vi.fn()) =>
|
|
render(<EventCloseOutDialog event={EVENT} onClose={vi.fn()} onClosed={onClosed} />);
|
|
|
|
describe("EventCloseOutDialog", () => {
|
|
it("starts a close-out count and opens it", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.getEventCloseOut.mockResolvedValue(state("NOT_STARTED"));
|
|
mocks.startEventCloseOut.mockResolvedValue({ id: 8 });
|
|
renderDialog();
|
|
|
|
expect(await screen.findByRole("heading", { name: "Close out Baby Expo" })).toBeInTheDocument();
|
|
await user.click(screen.getByRole("button", { name: "Start close-out count" }));
|
|
|
|
expect(mocks.startEventCloseOut).toHaveBeenCalledWith("42");
|
|
expect(memoryRouter.push).toHaveBeenCalledWith("/inventory/stock-counts/8");
|
|
});
|
|
|
|
it("cannot start before the sales window ends", async () => {
|
|
mocks.getEventCloseOut.mockResolvedValue(state("NOT_STARTED", { windowsOver: false }));
|
|
renderDialog();
|
|
|
|
expect(await screen.findByText("Close-out opens after the sales window ends.")).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: "Start close-out count" })).toBeDisabled();
|
|
});
|
|
|
|
it("names blocking orders by invoice number, and an unpaid order by its reference", async () => {
|
|
mocks.getEventCloseOut.mockResolvedValue(state("NOT_STARTED", {
|
|
openReservations: [{ ref: "3288627917505028096", orderNo: "IN00000042" }, { ref: "3288628039810932736" }],
|
|
}));
|
|
renderDialog();
|
|
|
|
expect(await screen.findByText("IN00000042")).toBeInTheDocument();
|
|
expect(screen.queryByText("3288627917505028096")).not.toBeInTheDocument();
|
|
expect(screen.getByText("Unpaid order 3288628039810932736")).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: "Start close-out count" })).toBeDisabled();
|
|
});
|
|
|
|
it.each([
|
|
["COUNTING", /count in progress/i],
|
|
["AWAITING_APPROVAL", /waiting for approval/i],
|
|
])("links to the count while %s", async (phase, text) => {
|
|
mocks.getEventCloseOut.mockResolvedValue(state(phase, { countId: 8 }));
|
|
renderDialog();
|
|
|
|
expect(await screen.findByText(text)).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: "Open count #8" })).toHaveAttribute("href", "/inventory/stock-counts/8");
|
|
});
|
|
|
|
it("returns the leftovers to the suggested warehouse and closes the event", async () => {
|
|
const user = userEvent.setup();
|
|
const onClosed = vi.fn();
|
|
mocks.getEventCloseOut.mockResolvedValue(state("READY_TO_CLOSE", { countId: 8 }));
|
|
mocks.closeEvent.mockResolvedValue({ status: "C" });
|
|
renderDialog(onClosed);
|
|
|
|
const select = await screen.findByRole("combobox", { name: /return warehouse/i });
|
|
expect(select).toHaveTextContent("MYB-W");
|
|
await user.click(screen.getByRole("button", { name: "Return stock and close event" }));
|
|
|
|
expect(mocks.closeEvent).toHaveBeenCalledWith("42", 2);
|
|
expect(onClosed).toHaveBeenCalled();
|
|
});
|
|
|
|
it("offers only active non-event warehouses as the return warehouse", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.getEventCloseOut.mockResolvedValue(state("READY_TO_CLOSE", { countId: 8, suggestedReturnWarehouseId: null }));
|
|
renderDialog();
|
|
|
|
await user.click(await screen.findByRole("combobox", { name: /return warehouse/i }));
|
|
expect(screen.getByRole("option", { name: /MYB-W/ })).toBeInTheDocument();
|
|
expect(screen.getByRole("option", { name: /KL-M/ })).toBeInTheDocument();
|
|
expect(screen.queryByRole("option", { name: /EVT-EXPO/ })).not.toBeInTheDocument();
|
|
expect(screen.queryByRole("option", { name: /OLD/ })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("needs a return warehouse before closing", async () => {
|
|
mocks.getEventCloseOut.mockResolvedValue(state("READY_TO_CLOSE", { countId: 8, suggestedReturnWarehouseId: null }));
|
|
renderDialog();
|
|
|
|
expect(await screen.findByRole("button", { name: "Return stock and close event" })).toBeDisabled();
|
|
});
|
|
|
|
it("shows the server's refusal", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.getEventCloseOut.mockResolvedValue(state("NOT_STARTED"));
|
|
mocks.startEventCloseOut.mockRejectedValue(new Error("Uncollected pickups or reservations block close-out: ORDER-Z"));
|
|
renderDialog();
|
|
|
|
await user.click(await screen.findByRole("button", { name: "Start close-out count" }));
|
|
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("ORDER-Z");
|
|
expect(memoryRouter.push).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("describes a closed event", async () => {
|
|
mocks.getEventCloseOut.mockResolvedValue(state("CLOSED", { returnWarehouseId: 2, suggestedReturnWarehouseId: null }));
|
|
renderDialog();
|
|
|
|
expect(await screen.findByText(/leftover stock went to MYB-W/i)).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /start|close event/i })).not.toBeInTheDocument();
|
|
});
|
|
});
|