Files
co-lite/solvway-admin-portal/app/inventory/delivery-orders/BulkIssueDialog.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

144 lines
7.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type { IssuePlanLine } from "@/app/lib/services/adminDeliveryOrders";
// Bulk issue preview (manual-do-issue §6.3): orders oldest paid first, stock allocated in that order,
// each order marked full / partial (n of m) / none; confirm issues the ticked orders one at a time.
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),
}));
import { BulkIssueDialog } from "./BulkIssueDialog";
function line(over: Partial<IssuePlanLine>): IssuePlanLine {
return {
orderLineIndex: 0, productCode: "P-A", variationId: "V-A", sku: "SKU-A", productName: "Item A",
warehouseId: "7", ordered: 3, issued: 0, outstanding: 3, onHand: 4, ...over,
};
}
// Listed newest first on purpose: the dialog must order them oldest paid first.
const ORDERS = [
{ uuid: "O-NEW", documentNo: "IN00000202", transactionDateTime: "2026-10-18T09:00:00" },
{ uuid: "O-OLD", documentNo: "IN00000201", transactionDateTime: "2026-10-10T09:00:00" },
];
function result(doUuid: string, doNumber: string, outstandingUnits = 0) {
return { deliveryOrderUuids: [doUuid], deliveryOrderNumbers: [doNumber], issuedUnits: 3, outstandingUnits };
}
beforeEach(() => {
svc.getIssuePlan.mockReset();
svc.issueOrder.mockReset();
// Both orders need 3 of SKU A; the warehouse holds 4.
svc.getIssuePlan.mockImplementation(async () => [line({})]);
});
function rowOf(invoice: string) {
return screen.getByText(invoice).closest("tr")!;
}
async function renderLoaded(onClose = vi.fn()) {
const user = userEvent.setup();
render(<BulkIssueDialog orders={ORDERS} onClose={onClose} />);
await screen.findByText("partial (1 of 3)");
return { user, onClose };
}
describe("BulkIssueDialog", () => {
it("allocates oldest first and marks the second order partial", async () => {
await renderLoaded();
const rows = within(screen.getByRole("dialog")).getAllByRole("row").slice(1);
expect(rows.map(r => within(r).getByText(/IN0000020\d/).textContent)).toEqual(["IN00000201", "IN00000202"]);
expect(within(rowOf("IN00000201")).getByText("full")).toBeInTheDocument();
expect(within(rowOf("IN00000202")).getByText("partial (1 of 3)")).toBeInTheDocument();
});
it("an order with no stock left is marked none and starts unticked", async () => {
svc.getIssuePlan.mockImplementation(async () => [line({ onHand: 3 })]);
render(<BulkIssueDialog orders={ORDERS} onClose={vi.fn()} />);
expect(await within(await screen.findByText("IN00000202").then(e => e.closest("tr")!)).findByText("none")).toBeInTheDocument();
expect(screen.getByRole("checkbox", { name: "Select IN00000202" })).not.toBeChecked();
expect(screen.getByRole("checkbox", { name: "Select IN00000201" })).toBeChecked();
});
it("issues the ticked orders one at a time with their allocated quantities and a fresh request id each", async () => {
svc.issueOrder
.mockResolvedValueOnce(result("DO-1", "MYB-ON-DO2610000001"))
.mockResolvedValueOnce(result("DO-2", "MYB-ON-DO2610000002", 2));
const { user } = await renderLoaded();
await user.click(screen.getByRole("button", { name: "Issue DO" }));
await screen.findByText("1 issued · 1 partly issued · 0 skipped");
expect(svc.issueOrder).toHaveBeenNthCalledWith(1, "O-OLD", expect.any(String), { 0: 3 });
expect(svc.issueOrder).toHaveBeenNthCalledWith(2, "O-NEW", expect.any(String), { 0: 1 });
expect(svc.issueOrder.mock.calls[0][1]).not.toBe(svc.issueOrder.mock.calls[1][1]);
});
it("skips unticked orders", async () => {
svc.issueOrder.mockResolvedValue(result("DO-1", "MYB-ON-DO2610000001"));
const { user } = await renderLoaded();
await user.click(screen.getByRole("checkbox", { name: "Select IN00000202" }));
await user.click(screen.getByRole("button", { name: "Issue DO" }));
await screen.findByText("1 issued · 0 partly issued · 1 skipped");
expect(svc.issueOrder).toHaveBeenCalledTimes(1);
expect(svc.issueOrder).toHaveBeenCalledWith("O-OLD", expect.any(String), { 0: 3 });
});
it("continues after one order fails, shows its error on the row and counts it as skipped", async () => {
svc.issueOrder
.mockRejectedValueOnce(new Error("Not enough stock for SKU-A"))
.mockResolvedValueOnce(result("DO-2", "MYB-ON-DO2610000002", 2));
const { user } = await renderLoaded();
await user.click(screen.getByRole("button", { name: "Issue DO" }));
await screen.findByText("0 issued · 1 partly issued · 1 skipped");
expect(within(rowOf("IN00000201")).getByText("Not enough stock for SKU-A")).toBeInTheDocument();
expect(svc.issueOrder).toHaveBeenCalledTimes(2);
});
it("closing after the run hands back the issued DO uuids; cancelling before hands back none", async () => {
svc.issueOrder
.mockResolvedValueOnce(result("DO-1", "MYB-ON-DO2610000001"))
.mockResolvedValueOnce(result("DO-2", "MYB-ON-DO2610000002", 2));
const { user, onClose } = await renderLoaded();
await user.click(screen.getByRole("button", { name: "Issue DO" }));
await screen.findByText("1 issued · 1 partly issued · 0 skipped");
await user.click(screen.getByRole("button", { name: "Close" }));
expect(onClose).toHaveBeenCalledWith(["DO-1", "DO-2"]);
});
it("Cancel before confirming issues nothing", async () => {
const { user, onClose } = await renderLoaded();
await user.click(screen.getByRole("button", { name: "Cancel" }));
expect(onClose).toHaveBeenCalledWith([]);
expect(svc.issueOrder).not.toHaveBeenCalled();
});
it("shows progress while issuing", async () => {
let finish!: (v: unknown) => void;
svc.issueOrder.mockReturnValueOnce(new Promise(r => { finish = r; })).mockResolvedValue(result("DO-2", "N2"));
const { user } = await renderLoaded();
await user.click(screen.getByRole("button", { name: "Issue DO" }));
expect(await screen.findByText("0 of 2")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Issue DO" })).toBeDisabled();
finish(result("DO-1", "N1"));
await waitFor(() => expect(screen.getByText("2 issued · 0 partly issued · 0 skipped")).toBeInTheDocument());
});
it("an order whose plan cannot load is marked none with the reason and left unticked", async () => {
svc.getIssuePlan.mockImplementation(async (uuid: string) => {
if (uuid === "O-OLD") throw new Error("Order not found");
return [line({})];
});
render(<BulkIssueDialog orders={ORDERS} onClose={vi.fn()} />);
const oldRow = (await screen.findByText("Order not found")).closest("tr")!;
expect(within(oldRow).getByText("none")).toBeInTheDocument();
expect(screen.getByRole("checkbox", { name: "Select IN00000201" })).not.toBeChecked();
// the failed order took no stock, so the newer one gets all 3
expect(within(rowOf("IN00000202")).getByText("full")).toBeInTheDocument();
});
});