Files
web/packages/design-system/lib/components/PasswordInput/PasswordInput.test.tsx
Joakim Jäderberg d284e82828 Merged in chore/fix-tests (pull request #3430)
Chore/fix tests

* chore: Upgrade nextjs@16.1.1

* chore: upgrade next@16.1.1

* upgrade underlying types

* merge

* Fix broken tests

* bump @swc/plugin-formatjs to latest version

* bump sentry

* transpile "import-in-the-middle" & "require-in-the-middle"

* revert next@16.1.1 upgrade

* revert transpilation addition

* .
2026-01-13 13:48:06 +00:00

104 lines
3.1 KiB
TypeScript

import { describe, expect, it, afterEach } from "vitest"
import { render, screen, cleanup } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { FormProvider, useForm } from "react-hook-form"
import { IntlProvider } from "react-intl"
import { PasswordInput } from "./PasswordInput"
afterEach(() => {
cleanup()
})
function FormWrapper({
children,
defaultValues = { password: "" },
}: {
children: React.ReactNode
defaultValues?: Record<string, string>
}) {
const methods = useForm({ defaultValues })
return (
<IntlProvider locale="en" messages={{}}>
<FormProvider {...methods}>
<form>{children}</form>
</FormProvider>
</IntlProvider>
)
}
const renderPasswordInput = (
props: React.ComponentProps<typeof PasswordInput> = {},
defaultValues?: Record<string, string>
) => {
return render(
<FormWrapper defaultValues={defaultValues}>
<PasswordInput {...props} />
</FormWrapper>
)
}
describe("PasswordInput", () => {
describe("rendering", () => {
it("renders with default password label", async () => {
renderPasswordInput()
expect(await screen.findByLabelText("Password")).toBeTruthy()
})
it("renders with custom label", async () => {
renderPasswordInput({ label: "Enter your password" })
expect(await screen.findByLabelText("Enter your password")).toBeTruthy()
})
it("renders with new password label when isNewPassword is true", async () => {
renderPasswordInput({ isNewPassword: true })
expect(await screen.findByLabelText("New password")).toBeTruthy()
})
})
describe("visibility toggle", () => {
it("shows visibility toggle button by default", async () => {
renderPasswordInput()
expect(await screen.findByLabelText("Show password")).toBeTruthy()
})
it("hides visibility toggle when visibilityToggleable is false", async () => {
renderPasswordInput({ visibilityToggleable: false })
expect(await screen.findByLabelText("Password")).toBeTruthy()
expect(screen.queryByLabelText("Show password")).toBeNull()
expect(screen.queryByLabelText("Hide password")).toBeNull()
})
})
describe("disabled state", () => {
it("disables the input when disabled prop is true", async () => {
renderPasswordInput({ disabled: true })
expect(await screen.findByLabelText("Password")).toHaveProperty(
"disabled",
true
)
})
})
describe("form integration", () => {
it("updates form value when typing", async () => {
const user = userEvent.setup()
renderPasswordInput()
const input = await screen.findByLabelText("Password")
await user.type(input, "secret123")
expect(input).toHaveProperty("value", "secret123")
})
it("uses custom name prop", async () => {
const user = userEvent.setup()
renderPasswordInput({ name: "confirmPassword" }, { confirmPassword: "" })
const input = await screen.findByLabelText("Password")
await user.type(input, "test")
expect(input).toHaveProperty("value", "test")
})
})
})