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
217 lines
10 KiB
TypeScript
217 lines
10 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 { malaysiaToday } from "@/app/lib/malaysiaPeriod";
|
|
import type { AdminWarehouse } from "@/app/lib/types";
|
|
import type { ToIssueRow } from "@/app/lib/services/adminDeliveryOrders";
|
|
|
|
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() }));
|
|
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" },
|
|
];
|
|
|
|
/** A Malaysia-time timestamp `days` before today, as counter's LocalDateTime serialises it. */
|
|
function daysAgo(days: number): string {
|
|
const d = new Date(`${malaysiaToday()}T00:00:00Z`);
|
|
d.setUTCDate(d.getUTCDate() - days);
|
|
return `${d.toISOString().slice(0, 10)}T10:00:00`;
|
|
}
|
|
|
|
function row(over: Partial<ToIssueRow>): ToIssueRow {
|
|
return {
|
|
uuid: "O-1", documentNo: "IN00000101", buyer: { buyerName: "Jane" }, buyerCode: "MY1001",
|
|
transactionDateTime: daysAgo(3), pickupOption: false, warehouseId: "7", deliveryOrderStatus: "PE",
|
|
orderedUnits: 8, issuedUnits: 0, ...over,
|
|
};
|
|
}
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/inventory/delivery-orders");
|
|
svc.listToIssue.mockReset();
|
|
svc.getIssuePlan.mockReset();
|
|
svc.issueOrder.mockReset();
|
|
wh.listAdminWarehouses.mockReset();
|
|
wh.listAdminWarehouses.mockResolvedValue(WAREHOUSES);
|
|
});
|
|
|
|
describe("ToIssueTab", () => {
|
|
it('renders rows oldest first with "5 of 8 to issue"', async () => {
|
|
svc.listToIssue.mockResolvedValue({
|
|
content: [
|
|
row({ uuid: "O-1", documentNo: "IN00000101", transactionDateTime: daysAgo(3), issuedUnits: 3, deliveryOrderStatus: "PA" }),
|
|
row({ uuid: "O-2", documentNo: "IN00000102", transactionDateTime: daysAgo(0), pickupOption: true, orderedUnits: 2 }),
|
|
],
|
|
totalElements: 2,
|
|
});
|
|
render(<ToIssueTab />);
|
|
|
|
const first = (await screen.findByText("IN00000101")).closest("tr")!;
|
|
const second = screen.getByText("IN00000102").closest("tr")!;
|
|
expect(first.compareDocumentPosition(second) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
|
expect(within(first).getByText("5 of 8 to issue")).toBeInTheDocument();
|
|
expect(within(first).getByText("Partly issued")).toBeInTheDocument();
|
|
expect(within(first).getByText("3d")).toBeInTheDocument();
|
|
expect(within(first).getByText("Delivery")).toBeInTheDocument();
|
|
expect(within(first).getByText("MYB-ON — Online")).toBeInTheDocument();
|
|
expect(within(second).getByText("2 of 2 to issue")).toBeInTheDocument();
|
|
expect(within(second).getByText("Awaiting issue")).toBeInTheDocument();
|
|
expect(within(second).getByText("Pickup")).toBeInTheDocument();
|
|
expect(svc.listToIssue).toHaveBeenCalledWith({}, 0, expect.any(Number));
|
|
});
|
|
|
|
it("filter writes to URL", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listToIssue.mockResolvedValue({ content: [row({})], totalElements: 1 });
|
|
render(<ToIssueTab />);
|
|
await screen.findByText("IN00000101");
|
|
|
|
await user.click(screen.getByRole("combobox", { name: "Fulfilment" }));
|
|
await user.click(await screen.findByRole("option", { name: "Pickup" }));
|
|
|
|
await waitFor(() => expect(memoryRouter.getSearch().get("ti.pickupOption")).toBe("true"));
|
|
await waitFor(() => expect(svc.listToIssue).toHaveBeenLastCalledWith({ pickupOption: "true" }, 0, expect.any(Number)));
|
|
});
|
|
|
|
it("restores filters from the URL and sends only the set ones", async () => {
|
|
memoryRouter.reset("/inventory/delivery-orders", "ti.warehouseId=7&ti.deliveryOrderStatus=PA&ti.buyerCode=MY1001");
|
|
svc.listToIssue.mockResolvedValue({ content: [], totalElements: 0 });
|
|
render(<ToIssueTab />);
|
|
await waitFor(() =>
|
|
expect(svc.listToIssue).toHaveBeenLastCalledWith(
|
|
{ warehouseId: "7", deliveryOrderStatus: "PA", buyerCode: "MY1001" }, 0, expect.any(Number)),
|
|
);
|
|
expect(await screen.findByText("No paid orders waiting to be issued")).toBeInTheDocument();
|
|
});
|
|
|
|
it("Issue opens the drawer; a successful issue shows the notice and reloads", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listToIssue.mockResolvedValue({ content: [row({})], totalElements: 1 });
|
|
svc.getIssuePlan.mockResolvedValue([
|
|
{ orderLineIndex: 0, productCode: "P1", variationId: "V1", sku: "BA-001", productName: null, warehouseId: "7",
|
|
ordered: 8, issued: 0, outstanding: 8, onHand: 5 },
|
|
]);
|
|
svc.issueOrder.mockResolvedValue({ deliveryOrderUuids: ["D-1"], deliveryOrderNumbers: ["MYB-ON-DO261000001"], issuedUnits: 5, outstandingUnits: 3 });
|
|
render(<ToIssueTab />);
|
|
await screen.findByText("IN00000101");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Issue" }));
|
|
await user.click(await screen.findByRole("button", { name: "Issue DO" }));
|
|
|
|
expect(await screen.findByText("MYB-ON-DO261000001 issued · 3 on back-order")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: "Issue DO" })).not.toBeInTheDocument();
|
|
expect(svc.listToIssue).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("shows the load error", async () => {
|
|
svc.listToIssue.mockRejectedValue(new Error("Forbidden"));
|
|
render(<ToIssueTab />);
|
|
expect(await screen.findByText("Forbidden")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("ToIssueTab overdue chip", () => {
|
|
it("overdueChipClearsFilter", async () => {
|
|
memoryRouter.reset("/inventory/delivery-orders", "ti.overdue=TO_ISSUE");
|
|
svc.listToIssue.mockResolvedValue({ content: [], totalElements: 0 });
|
|
render(<ToIssueTab />);
|
|
await waitFor(() => expect(svc.listToIssue).toHaveBeenCalledWith(expect.objectContaining({ overdue: "TO_ISSUE" }), 0, expect.any(Number)));
|
|
expect(screen.getByText("Showing overdue only")).toBeInTheDocument();
|
|
await userEvent.setup().click(screen.getByRole("button", { name: "Show all" }));
|
|
await waitFor(() => expect(svc.listToIssue).toHaveBeenLastCalledWith(expect.not.objectContaining({ overdue: expect.anything() }), 0, expect.any(Number)));
|
|
expect(screen.queryByText("Showing overdue only")).toBeNull();
|
|
});
|
|
});
|
|
|
|
// ─── BABYBOO-54 B3: header sorting (URL prefix "ti.") ─────────────────────────
|
|
// Assumes listToIssue(filters, page, size, { sortBy, sortDir }): the sort is a 4th argument, absent when unsorted.
|
|
|
|
describe("ToIssueTab header sorting", () => {
|
|
const lastSort = () => {
|
|
const call = svc.listToIssue.mock.calls[svc.listToIssue.mock.calls.length - 1];
|
|
return call[3];
|
|
};
|
|
|
|
it.each([
|
|
["Invoice", "documentNo"],
|
|
["Member", "member"],
|
|
["Paid on", "paidOn"],
|
|
["Fulfilment", "fulfilment"],
|
|
["Status", "status"],
|
|
])("clicking %s sorts server-side by %s, then desc", async (header, key) => {
|
|
const user = userEvent.setup();
|
|
svc.listToIssue.mockResolvedValue({ content: [row({})], totalElements: 1 });
|
|
render(<ToIssueTab />);
|
|
await screen.findByText("IN00000101");
|
|
|
|
await user.click(screen.getByRole("button", { name: header }));
|
|
await waitFor(() => expect(lastSort()).toEqual({ sortBy: key, sortDir: "asc" }));
|
|
expect(memoryRouter.getSearch().get("ti.sortBy")).toBe(key);
|
|
expect(memoryRouter.getSearch().get("ti.sortDir")).toBe("asc");
|
|
|
|
await user.click(screen.getByRole("button", { name: header }));
|
|
await waitFor(() => expect(lastSort()).toEqual({ sortBy: key, sortDir: "desc" }));
|
|
});
|
|
|
|
it("unsorted sends no sort argument", async () => {
|
|
svc.listToIssue.mockResolvedValue({ content: [row({})], totalElements: 1 });
|
|
render(<ToIssueTab />);
|
|
await screen.findByText("IN00000101");
|
|
expect(svc.listToIssue.mock.calls[0][3]).toBeUndefined();
|
|
});
|
|
|
|
// task 2.9: the warehouse header sorts by warehouse code (mobile-api resolves the code order).
|
|
it("clicking Warehouse sorts server-side by warehouse, then desc", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listToIssue.mockResolvedValue({ content: [row({})], totalElements: 1 });
|
|
render(<ToIssueTab />);
|
|
await screen.findByText("IN00000101");
|
|
const head = () => within(screen.getByRole("columnheader", { name: /Warehouse/ })).getByRole("button");
|
|
|
|
await user.click(head());
|
|
await waitFor(() => expect(lastSort()).toEqual({ sortBy: "warehouse", sortDir: "asc" }));
|
|
expect(memoryRouter.getSearch().get("ti.sortBy")).toBe("warehouse");
|
|
await user.click(head());
|
|
await waitFor(() => expect(lastSort()).toEqual({ sortBy: "warehouse", sortDir: "desc" }));
|
|
});
|
|
|
|
it("clicking Items sorts server-side by items, then desc", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listToIssue.mockResolvedValue({ content: [row({})], totalElements: 1 });
|
|
render(<ToIssueTab />);
|
|
await screen.findByText("IN00000101");
|
|
const head = () => within(screen.getByRole("columnheader", { name: /Items/ })).getByRole("button");
|
|
|
|
await user.click(head());
|
|
await waitFor(() => expect(lastSort()).toEqual({ sortBy: "items", sortDir: "asc" }));
|
|
expect(memoryRouter.getSearch().get("ti.sortBy")).toBe("items");
|
|
await user.click(head());
|
|
await waitFor(() => expect(lastSort()).toEqual({ sortBy: "items", sortDir: "desc" }));
|
|
});
|
|
|
|
it("re-sorting clears a row selection", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listToIssue.mockResolvedValue({ content: [row({})], totalElements: 1 });
|
|
render(<ToIssueTab />);
|
|
await screen.findByText("IN00000101");
|
|
await user.click(screen.getByRole("checkbox", { name: "Select IN00000101" }));
|
|
expect(screen.getByRole("button", { name: "Issue selected" })).toBeEnabled();
|
|
|
|
await user.click(screen.getByRole("button", { name: "Member" }));
|
|
|
|
// The selection bar (feedback 2026-10-26) leaves once nothing is ticked.
|
|
await waitFor(() => expect(screen.queryByRole("button", { name: "Issue selected" })).toBeNull());
|
|
});
|
|
});
|