Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
62 lines
3.1 KiB
TypeScript
62 lines
3.1 KiB
TypeScript
import { render, screen, fireEvent, within, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
vi.mock("next/navigation", () => ({ useRouter: () => ({ push: vi.fn() }) }));
|
|
vi.mock("@/app/lib/clientApi", async (importOriginal) => ({
|
|
ApiError: (await importOriginal<typeof import("@/app/lib/clientApi")>()).ApiError,
|
|
apiGet: vi.fn(),
|
|
apiPost: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: () => ({ refresh: vi.fn() }) }));
|
|
|
|
import ResetPasswordPage from "./page";
|
|
import { apiGet, apiPost, ApiError } from "@/app/lib/clientApi";
|
|
|
|
const get = apiGet as unknown as ReturnType<typeof vi.fn>;
|
|
const post = apiPost as unknown as ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
get.mockReset().mockResolvedValue([{ param: "INPUT_LENGTH_MIN", value: 10 }, { param: "SPACE_NOT_ALLOWED", value: null }]);
|
|
post.mockReset();
|
|
});
|
|
|
|
describe("ResetPasswordPage rules", () => {
|
|
it("lists the switched-on rules from the public route", async () => {
|
|
render(<ResetPasswordPage />);
|
|
expect(await screen.findByText("Length: at least 10")).toBeInTheDocument();
|
|
expect(screen.getByText("No spaces")).toBeInTheDocument();
|
|
expect(get).toHaveBeenCalledWith("/api/public/password-rules");
|
|
});
|
|
|
|
it("ties the hint and the refusal to the password input with aria-describedby", async () => {
|
|
post.mockRejectedValue(new ApiError("PASSWORD_RULE", 400, { code: "PASSWORD_RULE", details: { param: "INPUT_LENGTH_MIN", value: 10 } }));
|
|
render(<ResetPasswordPage />);
|
|
await screen.findByText("No spaces");
|
|
const input = screen.getByLabelText("New password");
|
|
// onShown reaches the parent one render after the hint appears.
|
|
await waitFor(() => expect(input.getAttribute("aria-describedby")).toBe("newPasswordRules"));
|
|
fireEvent.change(input, { target: { value: "Short1" } });
|
|
fireEvent.change(screen.getByLabelText("Confirm new password"), { target: { value: "Short1" } });
|
|
fireEvent.submit(screen.getByRole("button", { name: /set password/i }));
|
|
const alert = await screen.findByRole("alert");
|
|
expect(input.getAttribute("aria-describedby")).toBe(`newPasswordRules ${alert.id}`);
|
|
});
|
|
|
|
it("sets no aria-describedby when the rules cannot be loaded", async () => {
|
|
get.mockRejectedValue(new Error("down"));
|
|
render(<ResetPasswordPage />);
|
|
await new Promise((r) => setTimeout(r, 0));
|
|
expect(screen.getByLabelText("New password")).not.toHaveAttribute("aria-describedby");
|
|
});
|
|
|
|
it("names the refused rule at the password field", async () => {
|
|
post.mockRejectedValue(new ApiError("PASSWORD_RULE", 400, { code: "PASSWORD_RULE", details: { param: "INPUT_LENGTH_MIN", value: 10 } }));
|
|
render(<ResetPasswordPage />);
|
|
fireEvent.change(screen.getByLabelText("New password"), { target: { value: "Short1" } });
|
|
fireEvent.change(screen.getByLabelText("Confirm new password"), { target: { value: "Short1" } });
|
|
fireEvent.submit(screen.getByRole("button", { name: /set password/i }));
|
|
const alert = await screen.findByRole("alert");
|
|
expect(within(alert).getByText('Password does not meet the rule "Length: at least 10"')).toBeInTheDocument();
|
|
});
|
|
});
|