Files

75 lines
3.4 KiB
TypeScript

import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { ApiError } from "@/app/lib/clientApi";
const mocks = vi.hoisted(() => ({ setAdminUserPassword: vi.fn() }));
vi.mock("@/app/lib/services/adminUsers", () => ({ setAdminUserPassword: mocks.setAdminUserPassword }));
import { SetPasswordButton } from "./SetPasswordButton";
beforeEach(() => {
mocks.setAdminUserPassword.mockReset();
});
async function open() {
await userEvent.click(screen.getByRole("button", { name: "Set temporary password" }));
}
describe("SetPasswordButton", () => {
it("sends both fields, closes, and reports success", async () => {
mocks.setAdminUserPassword.mockResolvedValue(undefined);
const onDone = vi.fn();
render(<SetPasswordButton userId={9} onDone={onDone} />);
await open();
await userEvent.type(screen.getByLabelText("Password"), "Good#Pass1");
await userEvent.type(screen.getByLabelText("Confirm password"), "Good#Pass1");
await userEvent.click(screen.getByRole("button", { name: "Set password" }));
await waitFor(() => expect(onDone).toHaveBeenCalledTimes(1));
expect(mocks.setAdminUserPassword).toHaveBeenCalledWith(9, "Good#Pass1", "Good#Pass1");
expect(screen.queryByLabelText("Password")).not.toBeInTheDocument();
});
it("opens with empty fields again after a success", async () => {
mocks.setAdminUserPassword.mockResolvedValue(undefined);
render(<SetPasswordButton userId={9} onDone={vi.fn()} />);
await open();
await userEvent.type(screen.getByLabelText("Password"), "Good#Pass1");
await userEvent.type(screen.getByLabelText("Confirm password"), "Good#Pass1");
await userEvent.click(screen.getByRole("button", { name: "Set password" }));
await waitFor(() => expect(screen.queryByLabelText("Password")).not.toBeInTheDocument());
await open();
expect(screen.getByLabelText("Password")).toHaveValue("");
expect(screen.getByLabelText("Confirm password")).toHaveValue("");
});
it("catches a mismatch before calling the server", async () => {
render(<SetPasswordButton userId={9} onDone={vi.fn()} />);
await open();
await userEvent.type(screen.getByLabelText("Password"), "Good#Pass1");
await userEvent.type(screen.getByLabelText("Confirm password"), "Other#Pass1");
await userEvent.click(screen.getByRole("button", { name: "Set password" }));
expect(await screen.findByText("The passwords do not match.")).toBeInTheDocument();
expect(mocks.setAdminUserPassword).not.toHaveBeenCalled();
});
it.each([
["USER_DISABLED", "The user is deactivated"],
["PASSWORD_POLICY", "Password does not meet the password policy"],
["SUPER_ADMIN_REQUIRED", "Only a super admin can manage a super admin"],
])("shows the %s refusal and keeps the dialog open", async (code, message) => {
mocks.setAdminUserPassword.mockImplementation(async () => {
throw new ApiError(message, 400, { code });
});
const onDone = vi.fn();
render(<SetPasswordButton userId={9} onDone={onDone} />);
await open();
await userEvent.type(screen.getByLabelText("Password"), "Good#Pass1");
await userEvent.type(screen.getByLabelText("Confirm password"), "Good#Pass1");
await userEvent.click(screen.getByRole("button", { name: "Set password" }));
expect(await screen.findByText(message)).toBeInTheDocument();
expect(onDone).not.toHaveBeenCalled();
});
});