|
| 1 | +import { useState } from "react" |
| 2 | +import { describe, it, expect, vi } from "vitest" |
| 3 | +import { render, screen } from "@testing-library/react" |
| 4 | +import userEvent from "@testing-library/user-event" |
| 5 | +import type { WidgetProps } from "@rjsf/utils" |
| 6 | +import { SensitiveStringWidget } from "./SensitiveStringWidget.tsx" |
| 7 | + |
| 8 | +function makeProps(overrides: Partial<WidgetProps> = {}): WidgetProps { |
| 9 | + const base = { |
| 10 | + id: "sensitive", |
| 11 | + name: "sensitive", |
| 12 | + label: "sensitive", |
| 13 | + value: undefined as unknown, |
| 14 | + onChange: vi.fn(), |
| 15 | + onBlur: vi.fn(), |
| 16 | + onFocus: vi.fn(), |
| 17 | + required: false, |
| 18 | + disabled: false, |
| 19 | + readonly: false, |
| 20 | + autofocus: false, |
| 21 | + placeholder: "", |
| 22 | + options: {}, |
| 23 | + schema: { type: "string" }, |
| 24 | + uiSchema: {}, |
| 25 | + formContext: {}, |
| 26 | + rawErrors: [], |
| 27 | + hideError: false, |
| 28 | + multiple: false, |
| 29 | + registry: {}, |
| 30 | + } |
| 31 | + return { ...base, ...overrides } as unknown as WidgetProps |
| 32 | +} |
| 33 | + |
| 34 | +function renderWithLabel(props: WidgetProps) { |
| 35 | + return render( |
| 36 | + <> |
| 37 | + <label htmlFor={props.id}>access key</label> |
| 38 | + <SensitiveStringWidget {...props} /> |
| 39 | + </>, |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +const TOGGLE_NAME = /toggle credential visibility/i |
| 44 | + |
| 45 | +describe("SensitiveStringWidget", () => { |
| 46 | + it("renders a real <input> control associated with its label, regardless of state", () => { |
| 47 | + renderWithLabel(makeProps({ value: "super-secret" })) |
| 48 | + |
| 49 | + const input = screen.getByLabelText("access key") as HTMLInputElement |
| 50 | + expect(input.tagName).toBe("INPUT") |
| 51 | + expect(input.type).toBe("password") |
| 52 | + expect(input.value).toBe("super-secret") |
| 53 | + }) |
| 54 | + |
| 55 | + it("flips the input type from password to text when the toggle is pressed", async () => { |
| 56 | + const user = userEvent.setup() |
| 57 | + renderWithLabel(makeProps({ value: "super-secret" })) |
| 58 | + |
| 59 | + const toggle = screen.getByRole("button", { name: TOGGLE_NAME }) |
| 60 | + expect(toggle).toHaveAttribute("aria-pressed", "false") |
| 61 | + |
| 62 | + await user.click(toggle) |
| 63 | + |
| 64 | + expect(toggle).toHaveAttribute("aria-pressed", "true") |
| 65 | + expect((screen.getByLabelText("access key") as HTMLInputElement).type).toBe("text") |
| 66 | + }) |
| 67 | + |
| 68 | + it("keeps the toggle's accessible name stable across state changes", async () => { |
| 69 | + const user = userEvent.setup() |
| 70 | + renderWithLabel(makeProps({ value: "super-secret" })) |
| 71 | + |
| 72 | + const toggle = screen.getByRole("button", { name: TOGGLE_NAME }) |
| 73 | + await user.click(toggle) |
| 74 | + |
| 75 | + // Same button is still findable by the same stable name after pressing. |
| 76 | + expect(screen.getByRole("button", { name: TOGGLE_NAME })).toBe(toggle) |
| 77 | + }) |
| 78 | + |
| 79 | + it("hides the value again after a second toggle press", async () => { |
| 80 | + const user = userEvent.setup() |
| 81 | + renderWithLabel(makeProps({ value: "super-secret" })) |
| 82 | + |
| 83 | + const toggle = screen.getByRole("button", { name: TOGGLE_NAME }) |
| 84 | + await user.click(toggle) |
| 85 | + await user.click(toggle) |
| 86 | + |
| 87 | + expect(toggle).toHaveAttribute("aria-pressed", "false") |
| 88 | + expect((screen.getByLabelText("access key") as HTMLInputElement).type).toBe("password") |
| 89 | + }) |
| 90 | + |
| 91 | + it("carries the project's input styling in both states (no shape change on toggle)", async () => { |
| 92 | + const user = userEvent.setup() |
| 93 | + renderWithLabel(makeProps({ value: "super-secret" })) |
| 94 | + |
| 95 | + const input = screen.getByLabelText("access key") as HTMLInputElement |
| 96 | + expect(input).toHaveClass("rounded-md") |
| 97 | + expect(input).toHaveClass("border") |
| 98 | + |
| 99 | + await user.click(screen.getByRole("button", { name: TOGGLE_NAME })) |
| 100 | + |
| 101 | + expect(input).toHaveClass("rounded-md") |
| 102 | + expect(input).toHaveClass("border") |
| 103 | + }) |
| 104 | + |
| 105 | + it("opts the input out of browser password-manager autofill", () => { |
| 106 | + renderWithLabel(makeProps({ value: "super-secret" })) |
| 107 | + |
| 108 | + const input = screen.getByLabelText("access key") as HTMLInputElement |
| 109 | + expect(input).toHaveAttribute("autocomplete", "new-password") |
| 110 | + }) |
| 111 | + |
| 112 | + it("forwards typing into onChange without requiring the toggle first", async () => { |
| 113 | + const user = userEvent.setup() |
| 114 | + const onChange = vi.fn() |
| 115 | + |
| 116 | + function Host() { |
| 117 | + const [value, setValue] = useState<string | undefined>("") |
| 118 | + return ( |
| 119 | + <> |
| 120 | + <label htmlFor="sensitive">access key</label> |
| 121 | + <SensitiveStringWidget |
| 122 | + {...makeProps({ |
| 123 | + value, |
| 124 | + onChange: (next) => { |
| 125 | + onChange(next) |
| 126 | + setValue(next as string | undefined) |
| 127 | + }, |
| 128 | + })} |
| 129 | + /> |
| 130 | + </> |
| 131 | + ) |
| 132 | + } |
| 133 | + render(<Host />) |
| 134 | + |
| 135 | + await user.type(screen.getByLabelText("access key"), "abc") |
| 136 | + |
| 137 | + expect(onChange).toHaveBeenLastCalledWith("abc") |
| 138 | + }) |
| 139 | + |
| 140 | + it("emits undefined when the input is cleared", async () => { |
| 141 | + const user = userEvent.setup() |
| 142 | + const onChange = vi.fn() |
| 143 | + renderWithLabel(makeProps({ value: "secret", onChange })) |
| 144 | + |
| 145 | + await user.clear(screen.getByLabelText("access key") as HTMLInputElement) |
| 146 | + |
| 147 | + expect(onChange).toHaveBeenLastCalledWith(undefined) |
| 148 | + }) |
| 149 | + |
| 150 | + it("keeps the reveal toggle usable when the form is readonly", async () => { |
| 151 | + const user = userEvent.setup() |
| 152 | + renderWithLabel(makeProps({ value: "secret", readonly: true })) |
| 153 | + |
| 154 | + const input = screen.getByLabelText("access key") as HTMLInputElement |
| 155 | + expect(input).toHaveAttribute("readonly") |
| 156 | + |
| 157 | + await user.click(screen.getByRole("button", { name: TOGGLE_NAME })) |
| 158 | + |
| 159 | + expect(input.type).toBe("text") |
| 160 | + expect(input.value).toBe("secret") |
| 161 | + }) |
| 162 | + |
| 163 | + it("keeps the reveal toggle usable when the form is disabled", async () => { |
| 164 | + const user = userEvent.setup() |
| 165 | + renderWithLabel(makeProps({ value: "secret", disabled: true })) |
| 166 | + |
| 167 | + const input = screen.getByLabelText("access key") as HTMLInputElement |
| 168 | + expect(input).toBeDisabled() |
| 169 | + |
| 170 | + await user.click(screen.getByRole("button", { name: TOGGLE_NAME })) |
| 171 | + |
| 172 | + expect(input.type).toBe("text") |
| 173 | + }) |
| 174 | +}) |
0 commit comments