94 lines
3.2 KiB
TypeScript
94 lines
3.2 KiB
TypeScript
// @vitest-environment jsdom
|
|
import "@testing-library/jest-dom/vitest";
|
|
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import React from "react";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { assertNoA11yViolations } from "~/utils/test/a11y";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
loaderData: {
|
|
title: "Verify",
|
|
registration: { email: "ada@example.test" },
|
|
creationRequestKey: "d7077797-054b-497a-b1ce-8e4f36257a96",
|
|
verificationStatus: "pending" as const,
|
|
passwordPolicy: {
|
|
minLength: 12,
|
|
requireUpper: true,
|
|
requireLower: true,
|
|
requireDigit: true,
|
|
requireSpecial: true,
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("react-router", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("react-router")>();
|
|
return {
|
|
...actual,
|
|
Form: ({ children, ...props }: React.FormHTMLAttributes<HTMLFormElement>) => (
|
|
<form
|
|
{...props}
|
|
onSubmit={(event) => {
|
|
event.preventDefault();
|
|
}}
|
|
>
|
|
{children}
|
|
</form>
|
|
),
|
|
Link: ({ children, to, ...props }: React.AnchorHTMLAttributes<HTMLAnchorElement> & { to: string }) => (
|
|
<a href={to} {...props}>
|
|
{children}
|
|
</a>
|
|
),
|
|
useActionData: () => undefined,
|
|
useLoaderData: () => mocks.loaderData,
|
|
useNavigation: () => ({ state: "idle" }),
|
|
};
|
|
});
|
|
|
|
vi.mock("react-i18next", () => ({
|
|
useTranslation: () => ({ t: (key: string) => key }),
|
|
}));
|
|
|
|
vi.mock("~/utils/data/useRootData", () => ({
|
|
useRootData: () => ({ csrf: "csrf-token" }),
|
|
}));
|
|
|
|
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() }));
|
|
vi.mock("~/utils/db/registration.db.server", () => ({ getRegistrationVerificationViewByToken: vi.fn() }));
|
|
vi.mock("~/modules/users/services/AuthService", () => ({ default: { verifyFromRequest: vi.fn() } }));
|
|
vi.mock("~/services/auth/password-policy.server", () => ({ loadPasswordPolicy: vi.fn() }));
|
|
|
|
import VerifyRoute from "./verify.$id";
|
|
|
|
describe("verify password policy UX", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("shows live password checks, reports invalid input on blur, and enables setup only for a valid password", () => {
|
|
const { container } = render(<VerifyRoute />);
|
|
const password = screen.getByPlaceholderText("account.register.passwordChoosePlaceholder");
|
|
const submit = screen.getByRole("button", { name: "account.verify.setupWorkspace" });
|
|
|
|
expect(container.querySelector('[data-ui-id="ui.auth.verify.passwordRequirements"]')).toBeVisible();
|
|
expect(submit).toBeDisabled();
|
|
|
|
fireEvent.change(password, { target: { value: "TooShort1!" } });
|
|
expect(submit).toBeDisabled();
|
|
fireEvent.blur(password);
|
|
|
|
expect(screen.getByRole("alert")).toHaveTextContent("account.register.passwordRequirements.fixBeforeContinue");
|
|
expect(password).toHaveAttribute("aria-invalid", "true");
|
|
|
|
fireEvent.change(password, { target: { value: "ValidPassword1!" } });
|
|
|
|
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
|
expect(password).toHaveAttribute("aria-invalid", "false");
|
|
expect(submit).toBeEnabled();
|
|
expect(container.querySelectorAll('[aria-label^="account.register.passwordRequirements.met:"]')).toHaveLength(5);
|
|
assertNoA11yViolations(container);
|
|
});
|
|
});
|