Files
co-lite/solvway-admin-portal/app/components/inventory/StockCountsTab.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

119 lines
4.9 KiB
TypeScript

import { render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({
listStockCounts: vi.fn(),
openStockCount: vi.fn(),
listAdminWarehouses: vi.fn(),
}));
vi.mock("@/app/lib/services/adminStockCounts", () => ({
listStockCounts: mocks.listStockCounts,
openStockCount: mocks.openStockCount,
}));
vi.mock("@/app/lib/services/adminWarehouses", () => ({ listAdminWarehouses: mocks.listAdminWarehouses }));
const auth = vi.hoisted(() => ({ hasModule: vi.fn() }));
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: () => ({ hasModule: auth.hasModule }) }));
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
import { memoryRouter } from "@/test/memoryRouter";
import { StockCountsTab } from "./StockCountsTab";
const COUNT = {
id: 5,
warehouseId: 7,
warehouseCode: "MYB-W",
warehouseName: "MYB-HQ",
eventId: null,
status: "SUBMITTED",
openedBy: "alice",
openedDate: "2026-10-08T10:00:00",
submittedBy: "bob",
submittedDate: "2026-10-08T11:00:00",
closedBy: null,
closedDate: null,
adjustmentRef: null,
lines: [],
};
const WAREHOUSES = [
{ id: 7, code: "MYB-W", name: "MYB-HQ", type: "W", parentId: null, status: "Y" },
{ id: 37, code: "MYB-ON", name: "Online", type: "ONLINE", parentId: null, status: "Y" },
{ id: 8, code: "OLD-W", name: "Closed store", type: "W", parentId: null, status: "N" },
{ id: 39, code: "EVT-EXPO", name: "Expo", type: "EVENT", parentId: null, status: "Y" },
];
beforeEach(() => {
memoryRouter.reset("/inventory", "tab=counts");
mocks.listStockCounts.mockReset().mockResolvedValue([COUNT]);
mocks.openStockCount.mockReset();
mocks.listAdminWarehouses.mockReset().mockResolvedValue(WAREHOUSES);
auth.hasModule.mockReset().mockReturnValue(true);
});
describe("StockCountsTab", () => {
it("lists counts with warehouse, status and the warehouse linking to the count", async () => {
render(<StockCountsTab />);
const row = (await screen.findByText("MYB-W")).closest("tr")!;
expect(within(row).getByText("Awaiting approval")).toBeInTheDocument();
expect(within(row).getByText("bob")).toBeInTheDocument();
expect(within(row).getByRole("link", { name: "MYB-W" })).toHaveAttribute("href", "/inventory/stock-counts/5");
expect(within(row).getByText("#5")).toBeInTheDocument();
expect(within(row).queryByRole("link", { name: "#5" })).toBeNull();
});
it("shows an empty state", async () => {
mocks.listStockCounts.mockResolvedValue([]);
render(<StockCountsTab />);
expect(await screen.findByText("No stock counts yet")).toBeInTheDocument();
});
it("shows a load error", async () => {
mocks.listStockCounts.mockRejectedValue(new Error("boom"));
render(<StockCountsTab />);
expect(await screen.findByRole("alert")).toHaveTextContent("boom");
});
it("opens a count for an active non-online warehouse and goes to it", async () => {
const user = userEvent.setup();
mocks.openStockCount.mockResolvedValue({ ...COUNT, id: 9, status: "OPEN" });
render(<StockCountsTab />);
await user.click(await screen.findByRole("button", { name: "New count" }));
await user.click(await screen.findByRole("combobox", { name: /^warehouse$/i }));
expect(screen.queryByRole("option", { name: /Online/ })).not.toBeInTheDocument();
expect(screen.queryByRole("option", { name: /Closed store/ })).not.toBeInTheDocument();
expect(screen.getByRole("option", { name: /EVT-EXPO/ })).toBeInTheDocument();
await user.click(screen.getByRole("option", { name: /MYB-W/ }));
await user.click(screen.getByRole("button", { name: "Start count" }));
expect(mocks.openStockCount).toHaveBeenCalledWith("7");
expect(memoryRouter.push).toHaveBeenCalledWith("/inventory/stock-counts/9");
});
it("shows why a count could not be opened", async () => {
const user = userEvent.setup();
mocks.openStockCount.mockRejectedValue(new Error("Warehouse already has an open stock count (#4)"));
render(<StockCountsTab />);
await user.click(await screen.findByRole("button", { name: "New count" }));
await user.click(await screen.findByRole("combobox", { name: /^warehouse$/i }));
await user.click(await screen.findByRole("option", { name: /MYB-W/ }));
await user.click(screen.getByRole("button", { name: "Start count" }));
expect(await screen.findByText("Warehouse already has an open stock count (#4)")).toBeInTheDocument();
expect(memoryRouter.push).not.toHaveBeenCalled();
});
it("hides New count from an approver without the count module", async () => {
auth.hasModule.mockImplementation((m: string) => m === "BO_STOCK_COUNT_APPROVE");
render(<StockCountsTab />);
await screen.findByText("MYB-W");
expect(screen.queryByRole("button", { name: "New count" })).not.toBeInTheDocument();
});
});