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";
vi.mock("next/navigation", () => ({
useRouter: () => ({ replace: vi.fn(), refresh: vi.fn() }),
usePathname: () => "/",
}));
vi.mock("@/app/lib/useAclSidebar", () => ({ useAclSidebar: () => [] }));
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => ({ name: "Admin User", hasModule: () => false }),
}));
vi.mock("@/app/lib/theme", () => ({ useTheme: () => ({ theme: "light", toggleTheme: vi.fn() }) }));
const mocks = vi.hoisted(() => ({ changeOwnPassword: vi.fn() }));
vi.mock("@/app/lib/services/adminUsers", () => ({ changeOwnPassword: mocks.changeOwnPassword }));
import Header from "./Header";
beforeEach(() => {
mocks.changeOwnPassword.mockReset();
});
async function openDialog() {
await userEvent.click(screen.getByRole("button", { name: /admin user/i }));
await userEvent.click(await screen.findByText("Change password"));
}
describe("Header: change password", () => {
it("lists Change password above Sign out", async () => {
render();
await userEvent.click(screen.getByRole("button", { name: /admin user/i }));
const items = (await screen.findAllByRole("menuitem")).map((i) => i.textContent);
expect(items).toEqual(["My QR codes", "Change password", "Logout"]);
});
it("blocks empty fields and mismatched passwords without calling the server", async () => {
render();
await openDialog();
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
expect(await screen.findByRole("alert")).toHaveTextContent("Fill in all the password fields.");
await userEvent.type(screen.getByLabelText("Current password"), "Old#1");
await userEvent.type(screen.getByLabelText("New password"), "New#1");
await userEvent.type(screen.getByLabelText("Confirm password"), "Other#1");
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
expect(await screen.findByRole("alert")).toHaveTextContent("The passwords do not match.");
expect(mocks.changeOwnPassword).not.toHaveBeenCalled();
});
it("submits, closes, confirms, and starts empty next time", async () => {
mocks.changeOwnPassword.mockResolvedValue(undefined);
render();
await openDialog();
expect(screen.getByLabelText("Current password")).toHaveAttribute("autocomplete", "current-password");
await userEvent.type(screen.getByLabelText("Current password"), "Old#1");
await userEvent.type(screen.getByLabelText("New password"), "New#1");
await userEvent.type(screen.getByLabelText("Confirm password"), "New#1");
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
await waitFor(() => expect(mocks.changeOwnPassword).toHaveBeenCalledWith("Old#1", "New#1", "New#1"));
await waitFor(() => expect(screen.queryByLabelText("Current password")).not.toBeInTheDocument());
expect(screen.getByText("Your password has been changed.")).toBeInTheDocument();
await openDialog();
expect(screen.getByLabelText("Current password")).toHaveValue("");
expect(screen.getByLabelText("New password")).toHaveValue("");
});
it("shows the server message on failure and clears the fields on cancel", async () => {
mocks.changeOwnPassword.mockRejectedValue(new ApiError("Current password is incorrect.", 400, { code: "CURRENT_PASSWORD_WRONG" }));
render();
await openDialog();
await userEvent.type(screen.getByLabelText("Current password"), "bad");
await userEvent.type(screen.getByLabelText("New password"), "New#1");
await userEvent.type(screen.getByLabelText("Confirm password"), "New#1");
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
expect(await screen.findByRole("alert")).toHaveTextContent("Current password is incorrect.");
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
await openDialog();
expect(screen.getByLabelText("Current password")).toHaveValue("");
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
});
});