Files
co-lite/solvway-admin-portal/app/components/users/ChangePasswordDialog.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

84 lines
3.6 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";
const mocks = vi.hoisted(() => ({ changeAdminUserPassword: vi.fn() }));
vi.mock("@/app/lib/services/adminUsers", () => ({ changeAdminUserPassword: mocks.changeAdminUserPassword }));
import { ChangePasswordDialog } from "./ChangePasswordDialog";
beforeEach(() => {
mocks.changeAdminUserPassword.mockReset();
});
async function fillAndSubmit(password: string, confirm: string) {
await userEvent.type(screen.getByLabelText("New password"), password);
await userEvent.type(screen.getByLabelText("Confirm password"), confirm);
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
}
describe("ChangePasswordDialog", () => {
it("states the password rules", () => {
render(<ChangePasswordDialog userId={9} onClose={vi.fn()} />);
expect(screen.getByText("At least 8 characters, with at least one capital letter and one small letter.")).toBeInTheDocument();
});
it("blocks submit when the fields are empty", async () => {
render(<ChangePasswordDialog userId={9} onClose={vi.fn()} />);
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
expect(await screen.findByText(/enter and confirm/i)).toBeInTheDocument();
expect(mocks.changeAdminUserPassword).not.toHaveBeenCalled();
});
it("blocks submit when the password is too short", async () => {
render(<ChangePasswordDialog userId={9} onClose={vi.fn()} />);
// 7 characters: the backend admin rule needs at least 8.
await fillAndSubmit("Abcdefg", "Abcdefg");
expect(await screen.findByText(/must be at least 8 characters/i)).toBeInTheDocument();
expect(mocks.changeAdminUserPassword).not.toHaveBeenCalled();
});
it("blocks submit when the passwords do not match", async () => {
render(<ChangePasswordDialog userId={9} onClose={vi.fn()} />);
await fillAndSubmit("Passw0rd!", "Different1!");
expect(await screen.findByText(/do not match/i)).toBeInTheDocument();
expect(mocks.changeAdminUserPassword).not.toHaveBeenCalled();
});
it("submits a matching password", async () => {
mocks.changeAdminUserPassword.mockResolvedValue({ updated: 1 });
render(<ChangePasswordDialog userId={9} onClose={vi.fn()} />);
await fillAndSubmit("Passw0rd!", "Passw0rd!");
await waitFor(() => expect(mocks.changeAdminUserPassword).toHaveBeenCalledWith(9, "Passw0rd!"));
expect(await screen.findByText(/changed successfully/i)).toBeInTheDocument();
});
it("shows an error when the request fails", async () => {
mocks.changeAdminUserPassword.mockRejectedValue(new Error("Server error"));
render(<ChangePasswordDialog userId={9} onClose={vi.fn()} />);
await fillAndSubmit("Passw0rd!", "Passw0rd!");
expect(await screen.findByText("Server error")).toBeInTheDocument();
});
it("disables submit while the request is pending", async () => {
let resolve!: (v: { updated: number }) => void;
mocks.changeAdminUserPassword.mockReturnValue(
new Promise((r) => {
resolve = r;
}),
);
render(<ChangePasswordDialog userId={9} onClose={vi.fn()} />);
await userEvent.type(screen.getByLabelText("New password"), "Passw0rd!");
await userEvent.type(screen.getByLabelText("Confirm password"), "Passw0rd!");
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
await waitFor(() => expect(screen.getByRole("button", { name: "Change password" })).toBeDisabled());
resolve({ updated: 1 });
});
});