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
102 lines
4.9 KiB
TypeScript
102 lines
4.9 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import type { CampaignView } from "@/app/lib/types";
|
|
import { formatDate } from "@/app/lib/format";
|
|
|
|
// listing-exports task 6.1: Promotions Campaigns tab "Export .xlsx".
|
|
|
|
const svc = vi.hoisted(() => ({ listCampaigns: vi.fn() }));
|
|
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
|
vi.mock("@/app/lib/services/adminPromotions", async (importActual) => ({
|
|
...(await importActual<typeof import("@/app/lib/services/adminPromotions")>()),
|
|
listCampaigns: (p: number, s: number, f?: unknown) => svc.listCampaigns(p, s, f),
|
|
}));
|
|
const xlsx = vi.hoisted(() => ({ toXlsx: vi.fn(), downloadXlsx: vi.fn() }));
|
|
vi.mock("@/app/lib/xlsx", () => xlsx);
|
|
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
import { CampaignsPanel } from "./CampaignsPanel";
|
|
|
|
const camp: CampaignView = {
|
|
uuid: "C-1", name: "Raya sale", customerLabel: "RAYA 20%", benefitType: "PERCENT_OFF", benefitValue: 20,
|
|
maxDiscountCap: null, claimType: "SHARED_CODE", sharedCode: "RAYA15", channelOnline: true, channelOutlet: true,
|
|
channelEvent: false, allProducts: true, products: [], ranks: [], minSpend: null, maxUsesTotal: 500,
|
|
maxUsesPerMember: null, budgetAmount: 1000, effectiveFrom: "2026-10-01T00:00", effectiveTo: "2026-10-15T23:59",
|
|
status: "PAUSED", usedCount: 12, usedAmount: 240, locked: true,
|
|
};
|
|
|
|
const exportCall = () => svc.listCampaigns.mock.calls.find((c) => c[1] === 500);
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/promotions", "c.status=PAUSED");
|
|
svc.listCampaigns.mockReset();
|
|
xlsx.toXlsx.mockReset().mockResolvedValue(new Uint8Array([1]));
|
|
xlsx.downloadXlsx.mockReset();
|
|
svc.listCampaigns.mockResolvedValue({ content: [camp], totalElements: 1, page: 0, size: 20 });
|
|
});
|
|
|
|
describe("CampaignsPanel export", () => {
|
|
it("exports every matching campaign with the table's columns", async () => {
|
|
render(<CampaignsPanel />);
|
|
await screen.findByText("Raya sale");
|
|
await userEvent.setup().click(screen.getByRole("button", { name: "Export .xlsx" }));
|
|
|
|
await waitFor(() => expect(xlsx.toXlsx).toHaveBeenCalled());
|
|
expect(exportCall()![0]).toBe(0);
|
|
expect(exportCall()![2]).toEqual(expect.objectContaining({ status: "PAUSED" }));
|
|
const [rows, columns] = xlsx.toXlsx.mock.calls[0] as [Record<string, unknown>[], { key: string; title: string }[]];
|
|
expect(columns.map((c) => c.title)).toEqual([
|
|
"Name", "Customer label", "Benefit", "Claim", "Channels", "Window from", "Window to", "Status", "Usage", "Budget used", "Budget",
|
|
]);
|
|
const byTitle = Object.fromEntries(columns.map((c, i) => [c.title, rows[0]![c.key]]));
|
|
expect(rows).toHaveLength(1);
|
|
expect(byTitle).toEqual({
|
|
"Name": "Raya sale",
|
|
"Customer label": "RAYA 20%",
|
|
"Benefit": "20% off",
|
|
"Claim": "Code RAYA15",
|
|
"Channels": "Online + Branch counter",
|
|
"Window from": formatDate("2026-10-01T00:00"),
|
|
"Window to": formatDate("2026-10-15T23:59"),
|
|
"Status": "Paused",
|
|
"Usage": "12 / 500 uses",
|
|
"Budget used": 240,
|
|
"Budget": 1000,
|
|
});
|
|
expect(xlsx.downloadXlsx).toHaveBeenCalledWith(expect.stringMatching(/^campaigns-\d{4}-\d{2}-\d{2}\.xlsx$/), expect.anything());
|
|
});
|
|
|
|
it("exports the amount used for a campaign with no budget, leaving Budget blank", async () => {
|
|
svc.listCampaigns.mockResolvedValue({
|
|
content: [{ ...camp, budgetAmount: null, usedAmount: 75 }], totalElements: 1, page: 0, size: 20,
|
|
});
|
|
render(<CampaignsPanel />);
|
|
await screen.findByText("Raya sale");
|
|
await userEvent.setup().click(screen.getByRole("button", { name: "Export .xlsx" }));
|
|
|
|
await waitFor(() => expect(xlsx.toXlsx).toHaveBeenCalled());
|
|
const [rows, columns] = xlsx.toXlsx.mock.calls[0] as [Record<string, unknown>[], { key: string; title: string }[]];
|
|
const byTitle = Object.fromEntries(columns.map((c) => [c.title, rows[0]![c.key]]));
|
|
expect(byTitle["Budget used"]).toBe(75);
|
|
expect(byTitle["Budget"] ?? "").toBe("");
|
|
});
|
|
|
|
it("is disabled when there are no campaigns", async () => {
|
|
svc.listCampaigns.mockResolvedValue({ content: [], totalElements: 0, page: 0, size: 20 });
|
|
render(<CampaignsPanel />);
|
|
expect(await screen.findByRole("button", { name: "Export .xlsx" })).toBeDisabled();
|
|
});
|
|
|
|
it("shows an error and downloads nothing when the export fetch fails", async () => {
|
|
svc.listCampaigns.mockImplementation((_p: number, s: number) =>
|
|
s === 500 ? Promise.reject(new Error("boom")) : Promise.resolve({ content: [camp], totalElements: 1, page: 0, size: 20 }));
|
|
render(<CampaignsPanel />);
|
|
await screen.findByText("Raya sale");
|
|
await userEvent.setup().click(screen.getByRole("button", { name: "Export .xlsx" }));
|
|
|
|
expect(await screen.findByText("Export failed. Please try again.")).toBeInTheDocument();
|
|
expect(xlsx.downloadXlsx).not.toHaveBeenCalled();
|
|
});
|
|
});
|