import { render, screen, waitFor, fireEvent } from "@testing-library/react"; import { describe, it, expect, vi, beforeEach } from "vitest"; const nav = vi.hoisted(() => ({ push: vi.fn() })); vi.mock("next/navigation", () => ({ useRouter: () => ({ push: nav.push }) })); vi.mock("@/app/lib/clientApi", () => ({ apiPost: vi.fn(), ApiError: class ApiError extends Error {}, })); const refresh = vi.fn(); vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: () => ({ refresh }) })); import ResetPasswordPage from "./page"; import { apiPost } from "@/app/lib/clientApi"; const mockPost = apiPost as unknown as ReturnType; function fill(next: string, confirm: string) { fireEvent.change(screen.getByLabelText("New password"), { target: { value: next } }); fireEvent.change(screen.getByLabelText("Confirm new password"), { target: { value: confirm } }); fireEvent.submit(screen.getByRole("button", { name: /set password/i })); } beforeEach(() => { mockPost.mockReset(); nav.push.mockReset(); refresh.mockReset().mockResolvedValue(undefined); }); describe("ResetPasswordPage (first sign-in)", () => { it("does not ask for the current password", () => { render(); expect(screen.queryByLabelText(/current password/i)).toBeNull(); expect(screen.getByLabelText("New password")).toBeInTheDocument(); expect(screen.getByLabelText("Confirm new password")).toBeInTheDocument(); }); it("states the password rules under the new password field", () => { render(); expect(screen.getByText("At least 8 characters, with at least one capital letter and one small letter.")).toBeInTheDocument(); }); it("shows the choose-a-new-password intro, not the temporary-password one", () => { const { container } = render(); expect(screen.getByText("Choose a new password to continue.")).toBeInTheDocument(); expect(container.textContent).not.toMatch(/temporary password/i); }); it("renders a noValidate form so empty fields reach the JS validation", () => { const { container } = render(); expect(container.querySelector("form")).toHaveAttribute("novalidate"); }); it("shows an inline error and posts nothing when a field is empty", async () => { render(); fill("", ""); expect(await screen.findByRole("alert")).toHaveTextContent(/required/i); expect(mockPost).not.toHaveBeenCalled(); }); it("shows an inline error and posts nothing when the confirmation differs", async () => { render(); fill("New#Pass1", "New#Pass2"); expect(await screen.findByRole("alert")).toHaveTextContent(/do not match/i); expect(mockPost).not.toHaveBeenCalled(); }); it("posts only the new password, then goes to the dashboard", async () => { mockPost.mockResolvedValue({ ok: true }); render(); fill("New#Pass1", "New#Pass1"); await waitFor(() => expect(nav.push).toHaveBeenCalledWith("/")); expect(mockPost).toHaveBeenCalledWith("/api/auth/first-login", { newPassword: "New#Pass1", }); }); it("shows the API error inline and stays on the page", async () => { mockPost.mockRejectedValue(new Error("Password does not meet the password policy")); render(); fill("weak", "weak"); expect(await screen.findByRole("alert")).toHaveTextContent("Password does not meet the password policy"); expect(nav.push).not.toHaveBeenCalled(); }); }); describe("Containerverse credit", () => { it("is shown on the reset-password page, linked to containerverse.dev", () => { render(); const link = screen.getByRole("link", { name: "Containerverse Sdn. Bhd." }); expect(link).toHaveAttribute("href", "https://containerverse.dev/"); expect(link).toHaveAttribute("target", "_blank"); expect(link).toHaveAttribute("rel", "noopener noreferrer"); expect(link.parentElement?.textContent).toBe("Powered by Containerverse Sdn. Bhd."); }); });