import { render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ listEmails: vi.fn(), useAdminAuth: vi.fn() })); vi.mock("@/app/lib/services/adminEmails", async (importActual) => ({ ...(await importActual()), listEmails: mocks.listEmails, })); vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth })); vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock); import { memoryRouter } from "@/test/memoryRouter"; import EmailsPage from "./page"; const ROWS = [ { id: 9, datetime: "2026-11-30T11:44:26", recipients: ["sales1@example.com"], subject: "Reset Admin Password", status: "Y" }, { id: 8, datetime: "2026-11-29T09:05:00", recipients: ["a@example.com", "b@example.com"], subject: "Welcome", status: "N" }, ]; beforeEach(() => { memoryRouter.reset("/emails"); mocks.listEmails.mockReset().mockResolvedValue({ items: ROWS, total: 2 }); mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, hasModule: () => true }); }); describe("EmailsPage", () => { it("renders NotAuthorized without BO_EMAILS", () => { mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => false }); render(); expect(screen.getByTestId("not-authorized")).toBeInTheDocument(); expect(mocks.listEmails).not.toHaveBeenCalled(); }); it("puts the subject link first so it is reachable on a phone", async () => { render(); const table = await screen.findByRole("table"); const headers = within(table).getAllByRole("columnheader").map((h) => h.textContent); expect(headers).toEqual(["Subject", "Recipient", "Date", "Status"]); }); it("lists date, recipients, subject and status in the order given, with the subject linking to the detail", async () => { render(); const table = await screen.findByRole("table"); const rows = within(table).getAllByRole("row").slice(1); expect(rows[0]).toHaveTextContent("30/11/2026 11:44"); expect(rows[0]).toHaveTextContent("sales1@example.com"); expect(rows[0]).toHaveTextContent("Success"); expect(rows[1]).toHaveTextContent("a@example.com, b@example.com"); expect(rows[1]).toHaveTextContent("Failed"); expect(within(rows[0]).getByRole("link", { name: "Reset Admin Password" })).toHaveAttribute("href", "/emails/9"); }); it("sends the recipient and subject filters", async () => { render(); await screen.findByRole("table"); await userEvent.type(screen.getByRole("searchbox", { name: "Recipient" }), "sales1"); await waitFor(() => expect(mocks.listEmails).toHaveBeenLastCalledWith(expect.objectContaining({ recipient: "sales1" }))); await userEvent.type(screen.getByRole("searchbox", { name: "Subject" }), "Reset"); await waitFor(() => expect(mocks.listEmails).toHaveBeenLastCalledWith(expect.objectContaining({ recipient: "sales1", subject: "Reset" }))); }); it("asks for the page it shows", async () => { render(); await screen.findByRole("table"); expect(mocks.listEmails).toHaveBeenCalledWith(expect.objectContaining({ page: 1, pageSize: 20 })); }); it("asks for the next page when paging forward", async () => { mocks.listEmails.mockResolvedValue({ items: ROWS, total: 45 }); render(); await screen.findByRole("table"); await userEvent.click(screen.getByRole("button", { name: /next/i })); await waitFor(() => expect(mocks.listEmails).toHaveBeenLastCalledWith(expect.objectContaining({ page: 2 }))); }); it("shows an empty state", async () => { mocks.listEmails.mockResolvedValue({ items: [], total: 0 }); render(); expect(await screen.findByText("No emails found")).toBeInTheDocument(); }); });