Files
co-lite/solvway-admin-portal/app/inventory/delivery-orders/IssueDrawer.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

179 lines
8.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type { AdminWarehouse } from "@/app/lib/types";
import type { IssuePlanLine } from "@/app/lib/services/adminDeliveryOrders";
const svc = vi.hoisted(() => ({ getIssuePlan: vi.fn(), issueOrder: vi.fn() }));
vi.mock("@/app/lib/services/adminDeliveryOrders", () => ({
getIssuePlan: (...a: unknown[]) => svc.getIssuePlan(...a),
issueOrder: (...a: unknown[]) => svc.issueOrder(...a),
}));
const wh = vi.hoisted(() => ({ listAdminWarehouses: vi.fn() }));
vi.mock("@/app/lib/services/adminWarehouses", () => ({ listAdminWarehouses: () => wh.listAdminWarehouses() }));
import { IssueDrawer } from "./IssueDrawer";
const WAREHOUSES: AdminWarehouse[] = [
{ id: 7, code: "MYB-ON", name: "Online", type: "W", parentId: null, status: "Y", fulfilmentMode: "MANUAL" },
{ id: 9, code: "QY-1", name: "QianYi", type: "W", parentId: null, status: "Y", fulfilmentMode: "QIANYI" },
];
function line(over: Partial<IssuePlanLine>): IssuePlanLine {
return {
orderLineIndex: 0, productCode: "P1", variationId: "V1", sku: "BA-001", productName: null, warehouseId: "7",
ordered: 8, issued: 0, outstanding: 8, onHand: 5, ...over,
};
}
const onClose = vi.fn();
const onIssued = vi.fn();
function renderDrawer() {
return render(<IssueDrawer orderUuid="O-1" invoice="IN00000101" onClose={onClose} onIssued={onIssued} />);
}
beforeEach(() => {
svc.getIssuePlan.mockReset();
svc.issueOrder.mockReset();
onClose.mockReset();
onIssued.mockReset();
wh.listAdminWarehouses.mockReset();
wh.listAdminWarehouses.mockResolvedValue(WAREHOUSES);
});
describe("IssueDrawer", () => {
it("defaults Issue now to min(outstanding,onHand)", async () => {
svc.getIssuePlan.mockResolvedValue([
line({ orderLineIndex: 0, sku: "BA-001", outstanding: 8, onHand: 5 }),
line({ orderLineIndex: 1, sku: "BA-002", ordered: 4, issued: 1, outstanding: 3, onHand: 10 }),
]);
renderDrawer();
expect(await screen.findByRole("spinbutton", { name: "Issue now for BA-001" })).toHaveValue(5);
expect(screen.getByRole("spinbutton", { name: "Issue now for BA-002" })).toHaveValue(3);
expect(svc.getIssuePlan).toHaveBeenCalledWith("O-1");
});
it("names each line by its SKU code and name, never the raw SKU variation id", async () => {
svc.getIssuePlan.mockResolvedValue([
line({ orderLineIndex: 0, sku: "46", skuCode: "HAM-001-S", skuName: "Hamper small" }),
line({ orderLineIndex: 1, productCode: "HAM-002", sku: "", variationId: "", skuCode: null, skuName: null }),
]);
renderDrawer();
expect(await screen.findByRole("spinbutton", { name: "Issue now for HAM-001-S" })).toBeInTheDocument();
expect(screen.getByText("Hamper small")).toBeInTheDocument();
expect(screen.getByText("HAM-001-S")).toBeInTheDocument();
expect(screen.queryByText("46")).toBeNull();
// A line the catalog cannot name falls back to its product code.
expect(screen.getByRole("spinbutton", { name: "Issue now for HAM-002" })).toBeInTheDocument();
});
it("bundle line: one quantity input with each component, its units per line and on hand", async () => {
svc.getIssuePlan.mockResolvedValue([
line({
sku: null as unknown as string, skuCode: null, skuName: "HAM-003 ×1, HAM-004 ×1", outstanding: 5, onHand: 5,
components: [
{ skuVariationId: "48", skuCode: "HAM-003", skuName: "Hamilton Flexi-Fold Wagon", unitsPerLine: 1, onHand: 21 },
{ skuVariationId: "49", skuCode: "HAM-004", skuName: "Hamilton Folding Wagon - Black", unitsPerLine: 1, onHand: 5 },
],
} as Partial<IssuePlanLine>),
]);
renderDrawer();
await screen.findByText("HAM-003");
expect(screen.getAllByRole("spinbutton")).toHaveLength(1);
const c1 = screen.getByText("HAM-003").closest("li, tr, div")!.parentElement!;
expect(c1).toHaveTextContent("Hamilton Flexi-Fold Wagon");
expect(c1).toHaveTextContent("×1");
expect(c1).toHaveTextContent("21");
expect(screen.getByText("HAM-004")).toBeInTheDocument();
expect(screen.getByText("Hamilton Folding Wagon - Black")).toBeInTheDocument();
expect(screen.getAllByText(/×1/).length).toBeGreaterThanOrEqual(2);
});
it("single-SKU line shows no component rows", async () => {
svc.getIssuePlan.mockResolvedValue([line({ sku: "BA-001", components: [] } as Partial<IssuePlanLine>)]);
renderDrawer();
await screen.findByRole("spinbutton", { name: "Issue now for BA-001" });
expect(screen.queryByText(/×\d/)).toBeNull();
});
it("clamps input above outstanding", async () => {
const user = userEvent.setup();
svc.getIssuePlan.mockResolvedValue([line({ outstanding: 8, onHand: 20 })]);
renderDrawer();
const input = await screen.findByRole("spinbutton", { name: "Issue now for BA-001" });
await user.clear(input);
await user.type(input, "99");
expect(input).toHaveValue(8);
});
it("zero-stock line shows Awaiting stock and 0", async () => {
svc.getIssuePlan.mockResolvedValue([line({ onHand: 0 })]);
renderDrawer();
expect(await screen.findByRole("spinbutton", { name: "Issue now for BA-001" })).toHaveValue(0);
expect(screen.getByText("Awaiting stock")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Issue DO" })).toBeDisabled();
});
it("summary counts issuing and back-order units", async () => {
const user = userEvent.setup();
svc.getIssuePlan.mockResolvedValue([
line({ orderLineIndex: 0, sku: "BA-001", outstanding: 8, onHand: 5 }),
line({ orderLineIndex: 1, sku: "BA-002", outstanding: 3, onHand: 0 }),
]);
renderDrawer();
expect(await screen.findByText("To issue: 5 · Back-order: 6")).toBeInTheDocument();
const input = screen.getByRole("spinbutton", { name: "Issue now for BA-001" });
await user.clear(input);
await user.type(input, "2");
expect(screen.getByText("To issue: 2 · Back-order: 9")).toBeInTheDocument();
});
it("submit sends same issueRequestId on retry after error", async () => {
const user = userEvent.setup();
svc.getIssuePlan.mockResolvedValue([
line({ orderLineIndex: 0, sku: "BA-001", outstanding: 8, onHand: 2 }),
line({ orderLineIndex: 3, sku: "BA-004", outstanding: 1, onHand: 0 }),
]);
svc.issueOrder.mockRejectedValueOnce(new Error("Not enough stock for BA-001"));
const result = { deliveryOrderUuids: ["D-1"], deliveryOrderNumbers: ["MYB-ON-DO261000001"], issuedUnits: 2, outstandingUnits: 7 };
svc.issueOrder.mockResolvedValueOnce(result);
renderDrawer();
await user.click(await screen.findByRole("button", { name: "Issue DO" }));
expect(await screen.findByText("Not enough stock for BA-001")).toBeInTheDocument();
expect(onIssued).not.toHaveBeenCalled();
expect(screen.getByRole("button", { name: "Issue DO" })).toBeEnabled();
await user.click(screen.getByRole("button", { name: "Issue DO" }));
await waitFor(() => expect(onIssued).toHaveBeenCalledWith(result));
const [first, second] = svc.issueOrder.mock.calls;
expect(first[0]).toBe("O-1");
expect(first[1]).toEqual(expect.any(String));
expect(first[1]).not.toBe("");
expect(second[1]).toBe(first[1]);
expect(first[2]).toEqual({ 0: 2 });
});
it("notes a QianYi warehouse line", async () => {
svc.getIssuePlan.mockResolvedValue([line({ warehouseId: "9" })]);
renderDrawer();
expect(await screen.findByText("Sent to QianYi for picking")).toBeInTheDocument();
});
it("no QianYi note for a manual warehouse", async () => {
svc.getIssuePlan.mockResolvedValue([line({ warehouseId: "7" })]);
renderDrawer();
await screen.findByRole("spinbutton", { name: "Issue now for BA-001" });
await waitFor(() => expect(wh.listAdminWarehouses).toHaveBeenCalled());
expect(screen.queryByText("Sent to QianYi for picking")).not.toBeInTheDocument();
});
it("renders nothing without an order", () => {
render(<IssueDrawer orderUuid={null} invoice="" onClose={onClose} onIssued={onIssued} />);
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(svc.getIssuePlan).not.toHaveBeenCalled();
});
});