Files
co-lite/solvway-admin-portal/app/collections/page.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

99 lines
4.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 { CollectionListItem } from "@/app/lib/collections";
const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false }));
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
}));
const svc = vi.hoisted(() => ({ listCollections: vi.fn() }));
vi.mock("@/app/lib/services/adminCollections", () => ({
listCollections: () => svc.listCollections(),
}));
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
import { memoryRouter } from "@/test/memoryRouter";
import CollectionsPage from "./page";
const rows: CollectionListItem[] = [
{ id: 1, slug: "newborn", title: "Newborn", type: "AUTOMATIC", status: "Y", productCount: 12, updatedAt: "2026-10-05T10:15:00" },
{ id: 2, slug: "our-picks", title: "Our picks", type: "MANUAL", status: "N", productCount: 0, updatedAt: null },
];
beforeEach(() => {
memoryRouter.reset("/collections");
auth.hasModule.mockReset();
auth.hasModule.mockReturnValue(true);
svc.listCollections.mockReset();
});
describe("/collections", () => {
it("lists each collection with its type, product count and status, linking to its form", async () => {
svc.listCollections.mockResolvedValue(rows);
render(<CollectionsPage />);
const table = await screen.findByRole("table", { name: "Collections" });
const newborn = within(table).getByRole("row", { name: /Newborn/ });
expect(within(newborn).getByText("Automatic")).toBeInTheDocument();
expect(within(newborn).getByText("12")).toBeInTheDocument();
expect(within(newborn).getByText("Active")).toBeInTheDocument();
expect(within(newborn).getByRole("link", { name: "Newborn" })).toHaveAttribute("href", "/collections/1");
const picks = within(table).getByRole("row", { name: /Our picks/ });
expect(within(picks).getByText("Manual")).toBeInTheDocument();
expect(within(picks).getByText("Inactive")).toBeInTheDocument();
expect(screen.getByRole("link", { name: /add collection/i })).toHaveAttribute("href", "/collections/new");
});
it("searches by title", async () => {
svc.listCollections.mockResolvedValue(rows);
render(<CollectionsPage />);
await screen.findByRole("table", { name: "Collections" });
await userEvent.type(screen.getByRole("searchbox", { name: "Search by title" }), "pick");
await waitFor(() => expect(screen.queryByRole("row", { name: /Newborn/ })).not.toBeInTheDocument());
expect(screen.getByRole("row", { name: /Our picks/ })).toBeInTheDocument();
expect(memoryRouter.getSearch().get("q")).toBe("pick");
});
// unify-admin-listing-toolbar: labelled filter bar, search kept in the URL.
it("puts the search in the filter bar and restores it from the URL", async () => {
memoryRouter.reset("/collections", "q=newb");
svc.listCollections.mockResolvedValue(rows);
render(<CollectionsPage />);
await screen.findByRole("table", { name: "Collections" });
const bar = document.querySelector('[data-slot="filter-panel"]') as HTMLElement;
expect(within(bar).getByRole("group", { name: "Title" })).toContainElement(screen.getByRole("searchbox", { name: "Search by title" }));
expect(screen.getByRole("searchbox", { name: "Search by title" })).toHaveValue("newb");
expect(screen.getByRole("row", { name: /Newborn/ })).toBeInTheDocument();
expect(screen.queryByRole("row", { name: /Our picks/ })).not.toBeInTheDocument();
});
it("shows an empty state when there are no collections", async () => {
svc.listCollections.mockResolvedValue([]);
render(<CollectionsPage />);
expect(await screen.findByText(/no collections yet/i)).toBeInTheDocument();
});
it("shows the load error instead of a table", async () => {
svc.listCollections.mockRejectedValue(new Error("Service unavailable"));
render(<CollectionsPage />);
expect(await screen.findByText("Service unavailable")).toBeInTheDocument();
expect(screen.queryByRole("table")).not.toBeInTheDocument();
});
it("is gated on the Collections module", async () => {
auth.hasModule.mockReturnValue(false);
svc.listCollections.mockResolvedValue(rows);
render(<CollectionsPage />);
expect(screen.queryByRole("table")).not.toBeInTheDocument();
expect(auth.hasModule.mock.calls.map((call) => call[0])).toContain("BO_COLLECTIONS");
});
});