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

140 lines
7.5 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 { AdminWarehouse } from "@/app/lib/types";
import type { ToIssueRow } from "@/app/lib/services/adminDeliveryOrders";
// To-issue tab, manual-do-issue §6.3 and §6.4: tick orders and issue them in bulk; after an issue,
// print the packing slip for the DOs it created.
const svc = vi.hoisted(() => ({ listToIssue: vi.fn(), getIssuePlan: vi.fn(), issueOrder: vi.fn() }));
vi.mock("@/app/lib/services/adminDeliveryOrders", () => ({
listToIssue: (...a: unknown[]) => svc.listToIssue(...a),
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() }));
const ful = vi.hoisted(() => ({ printDeliveryOrders: vi.fn() }));
vi.mock("@/app/lib/services/adminFulfilment", () => ({ printDeliveryOrders: (...a: unknown[]) => ful.printDeliveryOrders(...a) }));
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
import { memoryRouter } from "@/test/memoryRouter";
import { ToIssueTab } from "./ToIssueTab";
const WAREHOUSES: AdminWarehouse[] = [
{ id: 7, code: "MYB-ON", name: "Online", type: "W", parentId: null, status: "Y", fulfilmentMode: "MANUAL" },
];
function row(over: Partial<ToIssueRow>): ToIssueRow {
return {
uuid: "O-1", documentNo: "IN00000101", buyer: { buyerName: "Jane" }, buyerCode: "MY1001",
transactionDateTime: "2026-10-10T10:00:00", pickupOption: false, warehouseId: "7", deliveryOrderStatus: "PE",
orderedUnits: 3, issuedUnits: 0, ...over,
};
}
const PLAN = [{ orderLineIndex: 0, productCode: "P1", variationId: "V1", sku: "BA-001", productName: "Wipes",
warehouseId: "7", ordered: 3, issued: 0, outstanding: 3, onHand: 50 }];
beforeEach(() => {
memoryRouter.reset("/inventory/delivery-orders");
Object.values(svc).forEach(f => f.mockReset());
ful.printDeliveryOrders.mockReset().mockResolvedValue({ pdf: new Blob(["%PDF"]), skippedDrafts: 0 });
wh.listAdminWarehouses.mockReset().mockResolvedValue(WAREHOUSES);
svc.listToIssue.mockResolvedValue({
content: [
row({}),
row({ uuid: "O-2", documentNo: "IN00000102", transactionDateTime: "2026-10-12T10:00:00" }),
row({ uuid: "O-3", documentNo: "IN00000103", transactionDateTime: "2026-10-14T10:00:00" }),
],
totalElements: 3,
});
svc.getIssuePlan.mockResolvedValue(PLAN);
URL.createObjectURL = vi.fn(() => "blob:slip");
URL.revokeObjectURL = vi.fn();
});
describe("ToIssueTab bulk issue and packing slip", () => {
it("Issue selected appears once a row is ticked, then opens the bulk preview for the ticked orders only", async () => {
const user = userEvent.setup();
render(<ToIssueTab />);
await screen.findByText("IN00000101");
// Feedback 2026-10-26: the selection bar, and its button, only appear once a row is ticked.
expect(screen.queryByRole("button", { name: "Issue selected" })).toBeNull();
await user.click(screen.getByRole("checkbox", { name: "Select IN00000101" }));
await user.click(screen.getByRole("checkbox", { name: "Select IN00000103" }));
await user.click(screen.getByRole("button", { name: "Issue selected" }));
const dialog = await screen.findByRole("dialog");
expect(await within(dialog).findAllByText("full")).toHaveLength(2);
expect(within(dialog).getByText("IN00000101")).toBeInTheDocument();
expect(within(dialog).getByText("IN00000103")).toBeInTheDocument();
expect(within(dialog).queryByText("IN00000102")).toBeNull();
expect(svc.getIssuePlan.mock.calls.map(c => c[0]).sort()).toEqual(["O-1", "O-3"]);
});
it("closing the bulk run reloads the list and offers the packing slip for the DOs it issued", async () => {
const user = userEvent.setup();
svc.issueOrder
.mockResolvedValueOnce({ deliveryOrderUuids: ["D-1"], deliveryOrderNumbers: ["N1"], issuedUnits: 3, outstandingUnits: 0 })
.mockResolvedValueOnce({ deliveryOrderUuids: ["D-2"], deliveryOrderNumbers: ["N2"], issuedUnits: 3, outstandingUnits: 0 });
render(<ToIssueTab />);
await screen.findByText("IN00000101");
expect(screen.queryByRole("button", { name: "Print packing slip" })).toBeNull();
await user.click(screen.getByRole("checkbox", { name: "Select IN00000101" }));
await user.click(screen.getByRole("checkbox", { name: "Select IN00000102" }));
await user.click(screen.getByRole("button", { name: "Issue selected" }));
const dialog = await screen.findByRole("dialog");
await within(dialog).findAllByText("full");
await user.click(within(dialog).getByRole("button", { name: "Issue DO" }));
await within(dialog).findByText("2 issued · 0 partly issued · 0 skipped");
await user.click(within(dialog).getByRole("button", { name: "Close" }));
await waitFor(() => expect(svc.listToIssue).toHaveBeenCalledTimes(2));
await user.click(await screen.findByRole("button", { name: "Print packing slip" }));
expect(ful.printDeliveryOrders).toHaveBeenCalledWith({ uuids: ["D-1", "D-2"] });
await waitFor(() => expect(URL.createObjectURL).toHaveBeenCalled());
});
it("cancelling the bulk preview issues nothing and offers no packing slip", async () => {
const user = userEvent.setup();
render(<ToIssueTab />);
await screen.findByText("IN00000101");
await user.click(screen.getByRole("checkbox", { name: "Select IN00000101" }));
await user.click(screen.getByRole("button", { name: "Issue selected" }));
const dialog = await screen.findByRole("dialog");
await within(dialog).findByText("full");
await user.click(within(dialog).getByRole("button", { name: "Cancel" }));
expect(svc.issueOrder).not.toHaveBeenCalled();
expect(screen.queryByRole("button", { name: "Print packing slip" })).toBeNull();
});
it("a single issue from the drawer offers the packing slip for its DOs", async () => {
const user = userEvent.setup();
svc.issueOrder.mockResolvedValue({ deliveryOrderUuids: ["D-9"], deliveryOrderNumbers: ["MYB-ON-DO261000009"], issuedUnits: 3, outstandingUnits: 0 });
render(<ToIssueTab />);
await screen.findByText("IN00000101");
await user.click(within(screen.getByText("IN00000101").closest("tr")!).getByRole("button", { name: "Issue" }));
await user.click(await screen.findByRole("button", { name: "Issue DO" }));
await screen.findByText("MYB-ON-DO261000009 issued · 0 on back-order");
await user.click(screen.getByRole("button", { name: "Print packing slip" }));
expect(ful.printDeliveryOrders).toHaveBeenCalledWith({ uuids: ["D-9"] });
});
it("shows the print error when the packing slip fails", async () => {
const user = userEvent.setup();
ful.printDeliveryOrders.mockRejectedValue(new Error("Print service unavailable"));
svc.issueOrder.mockResolvedValue({ deliveryOrderUuids: ["D-9"], deliveryOrderNumbers: ["N9"], issuedUnits: 3, outstandingUnits: 0 });
render(<ToIssueTab />);
await screen.findByText("IN00000101");
await user.click(within(screen.getByText("IN00000101").closest("tr")!).getByRole("button", { name: "Issue" }));
await user.click(await screen.findByRole("button", { name: "Issue DO" }));
await user.click(await screen.findByRole("button", { name: "Print packing slip" }));
expect(await screen.findByText("Print service unavailable")).toBeInTheDocument();
});
});