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

113 lines
5.1 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 { CmsPageListItem } from "@/app/lib/cmsPages";
// add-cms-pages Task 7: the /pages list.
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(() => ({ listPages: vi.fn() }));
vi.mock("@/app/lib/services/adminPages", () => ({
listPages: () => svc.listPages(),
}));
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
import { memoryRouter } from "@/test/memoryRouter";
import PagesPage from "./page";
const rows: CmsPageListItem[] = [
{ id: 1, slug: "about-us", title: "About us", status: "Y", updatedAt: "2026-10-05T10:15:00" },
{ id: 2, slug: "contact", title: "Contact us", status: "N", updatedAt: "2026-10-06T08:00:00" },
];
beforeEach(() => {
memoryRouter.reset("/pages");
auth.hasModule.mockReset();
auth.hasModule.mockReturnValue(true);
svc.listPages.mockReset();
});
describe("/pages", () => {
it("lists each page with its address and status, linking to its editor", async () => {
svc.listPages.mockResolvedValue(rows);
render(<PagesPage />);
const table = await screen.findByRole("table", { name: "Pages" });
const about = within(table).getByRole("row", { name: /About us/ });
expect(within(about).getByText("/pages/about-us")).toBeInTheDocument();
expect(within(about).getByText("Visible")).toBeInTheDocument();
expect(within(about).getByRole("link", { name: "About us" })).toHaveAttribute("href", "/pages/1");
const contact = within(table).getByRole("row", { name: /Contact us/ });
expect(within(contact).getByText("/contact")).toBeInTheDocument();
expect(within(contact).getByText("Hidden")).toBeInTheDocument();
expect(screen.getByRole("link", { name: /add page/i })).toHaveAttribute("href", "/pages/new");
});
it("shows when each page was last updated", async () => {
svc.listPages.mockResolvedValue(rows);
render(<PagesPage />);
const table = await screen.findByRole("table", { name: "Pages" });
expect(within(within(table).getByRole("row", { name: /About us/ })).getByText(/5 Oct 2026/)).toBeInTheDocument();
});
it("searches by title or slug", async () => {
svc.listPages.mockResolvedValue(rows);
render(<PagesPage />);
await screen.findByRole("table", { name: "Pages" });
await userEvent.type(screen.getByRole("searchbox", { name: "Search by title or slug" }), "contact");
await waitFor(() => expect(screen.queryByRole("row", { name: /About us/ })).not.toBeInTheDocument());
expect(screen.getByRole("row", { name: /Contact us/ })).toBeInTheDocument();
await userEvent.clear(screen.getByRole("searchbox", { name: "Search by title or slug" }));
await userEvent.type(screen.getByRole("searchbox", { name: "Search by title or slug" }), "about-u");
await waitFor(() => expect(screen.queryByRole("row", { name: /Contact us/ })).not.toBeInTheDocument());
expect(screen.getByRole("row", { name: /About us/ })).toBeInTheDocument();
expect(memoryRouter.getSearch().get("q")).toBe("about-u");
});
// 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("/pages", "q=contact");
svc.listPages.mockResolvedValue(rows);
render(<PagesPage />);
await screen.findByRole("table", { name: "Pages" });
const bar = document.querySelector('[data-slot="filter-panel"]') as HTMLElement;
expect(within(bar).getByRole("group", { name: "Title or slug" })).toContainElement(screen.getByRole("searchbox", { name: "Search by title or slug" }));
expect(screen.getByRole("row", { name: /Contact us/ })).toBeInTheDocument();
expect(screen.queryByRole("row", { name: /About us/ })).not.toBeInTheDocument();
});
it("shows an empty state when there are no pages", async () => {
svc.listPages.mockResolvedValue([]);
render(<PagesPage />);
expect(await screen.findByText(/no pages yet/i)).toBeInTheDocument();
});
it("shows the load error with a retry instead of a table", async () => {
svc.listPages.mockRejectedValueOnce(new Error("Service unavailable")).mockResolvedValue(rows);
render(<PagesPage />);
expect(await screen.findByText("Service unavailable")).toBeInTheDocument();
expect(screen.queryByRole("table")).not.toBeInTheDocument();
await userEvent.click(screen.getByRole("button", { name: /retry/i }));
expect(await screen.findByRole("table", { name: "Pages" })).toBeInTheDocument();
});
it("is gated on the Pages module", async () => {
auth.hasModule.mockReturnValue(false);
svc.listPages.mockResolvedValue(rows);
render(<PagesPage />);
expect(screen.queryByRole("table")).not.toBeInTheDocument();
expect(auth.hasModule.mock.calls.map((call) => call[0])).toContain("BO_PAGES");
});
});