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
71 lines
3.1 KiB
TypeScript
71 lines
3.1 KiB
TypeScript
import { test, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import ReportsPage from "./page";
|
|
|
|
vi.mock("next/navigation", async () => {
|
|
const { useSyncExternalStore } = await import("react");
|
|
const sp = new URLSearchParams("tab=items");
|
|
return {
|
|
usePathname: () => "/reports",
|
|
useSearchParams: () => useSyncExternalStore(() => () => {}, () => sp, () => sp),
|
|
useRouter: () => ({ replace: () => {} }),
|
|
};
|
|
});
|
|
|
|
const getSalesItemsReport = vi.fn();
|
|
vi.mock("@/app/lib/services/adminDashboard", async () => {
|
|
const actual = await vi.importActual<typeof import("@/app/lib/services/adminDashboard")>("@/app/lib/services/adminDashboard");
|
|
return {
|
|
...actual,
|
|
getSalesReport: vi.fn().mockResolvedValue({ orders: { content: [], totalElements: 0 }, periodRevenue: 0, periodOrders: 0 }),
|
|
getSalesItemsReport: (p: unknown) => getSalesItemsReport(p),
|
|
};
|
|
});
|
|
vi.mock("@/app/lib/services/adminCatalog", () => ({ listCatalogRows: vi.fn().mockResolvedValue([]) }));
|
|
const listSellableCountries = vi.fn();
|
|
vi.mock("@/app/lib/services/sellableCountries", () => ({ listSellableCountries: () => listSellableCountries() }));
|
|
|
|
const MY = { code: "MY", name: "Malaysia", currency: "MYR" };
|
|
const SG = { code: "SG", name: "Singapore", currency: "SGD" };
|
|
const report = {
|
|
content: [{
|
|
lineId: 1, transactionDateTime: "2026-10-12T14:36:05", documentNo: "IN1", saleChannel: "NORMAL", sourcePlatform: "CP",
|
|
productCode: "P1", productName: "Cat tee", skuVariationCode: "P1-RED", supplierId: null, supplierCode: null, supplierName: null,
|
|
qty: 2, amount: 119.8, bundle: false,
|
|
}],
|
|
totalElements: 1,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
getSalesItemsReport.mockReset().mockResolvedValue(report);
|
|
listSellableCountries.mockReset();
|
|
});
|
|
|
|
const panel = () => screen.getByRole("tabpanel", { name: /sales by item/i });
|
|
|
|
test("hides the Country picker with one sellable country and formats RM", async () => {
|
|
listSellableCountries.mockResolvedValue([MY]);
|
|
render(<ReportsPage />);
|
|
expect(await within(panel()).findByText("RM 119.80")).toBeInTheDocument();
|
|
expect(screen.queryByLabelText("Country")).not.toBeInTheDocument();
|
|
});
|
|
|
|
test("choosing SG refetches with country=SG and formats the line amount in SGD", async () => {
|
|
listSellableCountries.mockResolvedValue([MY, SG]);
|
|
render(<ReportsPage />);
|
|
const picker = await screen.findByLabelText("Country");
|
|
await userEvent.click(picker);
|
|
await userEvent.click(await screen.findByRole("option", { name: /Singapore/ }));
|
|
await waitFor(() => expect(getSalesItemsReport).toHaveBeenLastCalledWith(expect.objectContaining({ country: "SG" })));
|
|
expect(await within(panel()).findByText("S$ 119.80")).toBeInTheDocument();
|
|
});
|
|
|
|
test("defaults to MY (no other country requested)", async () => {
|
|
listSellableCountries.mockResolvedValue([MY, SG]);
|
|
render(<ReportsPage />);
|
|
await screen.findByLabelText("Country");
|
|
await waitFor(() => expect(getSalesItemsReport).toHaveBeenCalled());
|
|
for (const [p] of getSalesItemsReport.mock.calls) expect((p as { country?: string }).country ?? "MY").toBe("MY");
|
|
});
|