Files
co-lite/solvway-admin-portal/app/reset-password/page.test.tsx
T
ngqileandClaude Opus 5.5 49966586ca fix(admin-portal): match the screens to the Co-Lite backend after the browser check
- Login copy describes the event CRM instead of orders and payouts.
- Role dialog has no parent field; the role routes require a trimmed name and forward only {name}.
- New-user banner says a temporary password was emailed; TEMP_PASSWORD_NOT_SENT shows a message and a link back to users.
- Username is read-only when editing.
- Password hint and an 8-character minimum match the backend admin password rules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 22:16:02 +08:00

97 lines
4.0 KiB
TypeScript

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<typeof vi.fn>;
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(<ResetPasswordPage />);
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(<ResetPasswordPage />);
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(<ResetPasswordPage />);
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(<ResetPasswordPage />);
expect(container.querySelector("form")).toHaveAttribute("novalidate");
});
it("shows an inline error and posts nothing when a field is empty", async () => {
render(<ResetPasswordPage />);
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(<ResetPasswordPage />);
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(<ResetPasswordPage />);
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(<ResetPasswordPage />);
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(<ResetPasswordPage />);
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.");
});
});