Files
co-lite/solvway-admin-portal/app/components/users/ChangePasswordDialog.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

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

78 lines
3.3 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("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()} />);
await fillAndSubmit("abc", "abc");
expect(await screen.findByText(/at least 6 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 });
});
});