|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | +/* eslint-disable jsx-a11y/aria-role */ |
| 3 | +import React, { ReactElement } from "react"; |
| 4 | +import { fireEvent, render } from "@testing-library/react"; |
| 5 | + |
| 6 | +import { PasswordWithMessage } from "../PasswordWithMessage"; |
| 7 | +import { TextFieldHookOptions, useTextField } from "../useTextField"; |
| 8 | +import { FormMessageProps } from "../FormMessage"; |
| 9 | + |
| 10 | +function Test({ |
| 11 | + id = "field-id", |
| 12 | + messageRole, |
| 13 | + ...options |
| 14 | +}: Partial<TextFieldHookOptions> & { messageRole?: "alert" }): ReactElement { |
| 15 | + // first arg is value, but it isn't needed for these examples |
| 16 | + const [, fieldProps] = useTextField({ |
| 17 | + id, |
| 18 | + ...options, |
| 19 | + }); |
| 20 | + |
| 21 | + let messageProps: FormMessageProps = fieldProps.messageProps; |
| 22 | + if (messageRole && messageProps) { |
| 23 | + messageProps = { ...messageProps, role: messageRole }; |
| 24 | + } |
| 25 | + |
| 26 | + return ( |
| 27 | + <PasswordWithMessage |
| 28 | + {...fieldProps} |
| 29 | + messageProps={messageProps} |
| 30 | + placeholder="Password" |
| 31 | + /> |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +describe("PasswordWithMessage", () => { |
| 36 | + it("should work with all the defaults when only an id is provided", () => { |
| 37 | + const { container, getByPlaceholderText, getByRole } = render(<Test />); |
| 38 | + |
| 39 | + expect(container).toMatchSnapshot(); |
| 40 | + const field = getByPlaceholderText("Password") as HTMLInputElement; |
| 41 | + expect(() => getByRole("alert")).toThrow(); |
| 42 | + |
| 43 | + expect(field).toHaveAttribute("aria-describedby", "field-id-message"); |
| 44 | + expect(field.value).toBe(""); |
| 45 | + |
| 46 | + fireEvent.change(field, { target: { value: "a" } }); |
| 47 | + expect(field.value).toBe("a"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should apply all of the constraint props to the Password", () => { |
| 51 | + const { rerender, getByPlaceholderText } = render( |
| 52 | + <Test pattern="\d{5,8}" minLength={5} maxLength={8} required /> |
| 53 | + ); |
| 54 | + |
| 55 | + const field = getByPlaceholderText("Password"); |
| 56 | + expect(field).toHaveAttribute("minLength", "5"); |
| 57 | + expect(field).toHaveAttribute("maxLength", "8"); |
| 58 | + expect(field).toHaveAttribute("pattern", "\\d{5,8}"); |
| 59 | + expect(field).toHaveAttribute("required"); |
| 60 | + |
| 61 | + rerender(<Test pattern="\d{5,8}" minLength={5} maxLength={8} />); |
| 62 | + expect(field).toHaveAttribute("minLength", "5"); |
| 63 | + expect(field).toHaveAttribute("maxLength", "8"); |
| 64 | + expect(field).toHaveAttribute("pattern", "\\d{5,8}"); |
| 65 | + expect(field).not.toHaveAttribute("required"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should render the counter parts in the message when the counter option is enabled along with the maxLength", () => { |
| 69 | + const props = { |
| 70 | + counter: true, |
| 71 | + maxLength: 20, |
| 72 | + }; |
| 73 | + |
| 74 | + const { rerender, getByPlaceholderText, getByRole } = render( |
| 75 | + <Test {...props} messageRole="alert" /> |
| 76 | + ); |
| 77 | + const field = getByPlaceholderText("Password"); |
| 78 | + const message = getByRole("alert"); |
| 79 | + |
| 80 | + expect(field).toHaveAttribute("maxLength", "20"); |
| 81 | + expect(message.textContent).toBe("0 / 20"); |
| 82 | + |
| 83 | + const value = "Hello, world!"; |
| 84 | + fireEvent.change(field, { target: { value } }); |
| 85 | + expect(message.textContent).toBe(`${value.length} / 20`); |
| 86 | + |
| 87 | + rerender(<Test {...props} counter={false} />); |
| 88 | + expect(field).toHaveAttribute("maxLength", "20"); |
| 89 | + expect(message.textContent).toBe(""); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should allow for the maxLength attribute to not be passed to the Password", () => { |
| 93 | + const props = { |
| 94 | + maxLength: 20, |
| 95 | + disableMaxLength: true, |
| 96 | + }; |
| 97 | + |
| 98 | + const { rerender, getByPlaceholderText } = render(<Test {...props} />); |
| 99 | + |
| 100 | + const field = getByPlaceholderText("Password"); |
| 101 | + expect(field).not.toHaveAttribute("maxLength"); |
| 102 | + |
| 103 | + rerender(<Test {...props} disableMaxLength={false} />); |
| 104 | + expect(field).toHaveAttribute("maxLength", "20"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should enable the error state if the value is less than the minLength or more than the maxLength", () => { |
| 108 | + const { getByPlaceholderText, getByRole } = render( |
| 109 | + <Test minLength={5} maxLength={20} messageRole="alert" /> |
| 110 | + ); |
| 111 | + const field = getByPlaceholderText("Password"); |
| 112 | + const container = field.parentElement!; |
| 113 | + const message = getByRole("alert"); |
| 114 | + |
| 115 | + expect(container.className).not.toContain("--error"); |
| 116 | + expect(message.className).not.toContain("--error"); |
| 117 | + |
| 118 | + fireEvent.change(field, { target: { value: "1" } }); |
| 119 | + expect(container.className).toContain("--error"); |
| 120 | + expect(message.className).toContain("--error"); |
| 121 | + |
| 122 | + fireEvent.change(field, { target: { value: "Valid" } }); |
| 123 | + expect(container.className).not.toContain("--error"); |
| 124 | + expect(message.className).not.toContain("--error"); |
| 125 | + |
| 126 | + fireEvent.change(field, { target: { value: "1234567890123456789" } }); |
| 127 | + expect(container.className).not.toContain("--error"); |
| 128 | + expect(message.className).not.toContain("--error"); |
| 129 | + |
| 130 | + fireEvent.change(field, { target: { value: "12345678901234567890" } }); |
| 131 | + expect(container.className).not.toContain("--error"); |
| 132 | + expect(message.className).not.toContain("--error"); |
| 133 | + |
| 134 | + fireEvent.change(field, { target: { value: "123456789012345678901" } }); |
| 135 | + expect(container.className).toContain("--error"); |
| 136 | + expect(message.className).toContain("--error"); |
| 137 | + }); |
| 138 | + |
| 139 | + it("should not update the error state on change or update the value if the custon onChange event stopped propagation", () => { |
| 140 | + const { getByPlaceholderText, getByRole } = render( |
| 141 | + <Test |
| 142 | + minLength={10} |
| 143 | + onChange={(event) => event.stopPropagation()} |
| 144 | + messageRole="alert" |
| 145 | + /> |
| 146 | + ); |
| 147 | + |
| 148 | + const field = getByPlaceholderText("Password"); |
| 149 | + const container = field.parentElement!; |
| 150 | + const message = getByRole("alert"); |
| 151 | + expect(container.className).not.toContain("--error"); |
| 152 | + expect(message.className).not.toContain("--error"); |
| 153 | + |
| 154 | + fireEvent.change(field, { target: { value: "1" } }); |
| 155 | + expect(field).toHaveAttribute("value", ""); |
| 156 | + expect(container.className).not.toContain("--error"); |
| 157 | + expect(message.className).not.toContain("--error"); |
| 158 | + }); |
| 159 | + |
| 160 | + it("should not update the error state on change if `validateOnChange` is false", () => { |
| 161 | + const { getByPlaceholderText, getByRole } = render( |
| 162 | + <Test minLength={10} validateOnChange={false} messageRole="alert" /> |
| 163 | + ); |
| 164 | + |
| 165 | + const field = getByPlaceholderText("Password"); |
| 166 | + const container = field.parentElement!; |
| 167 | + const message = getByRole("alert"); |
| 168 | + expect(container.className).not.toContain("--error"); |
| 169 | + expect(message.className).not.toContain("--error"); |
| 170 | + |
| 171 | + fireEvent.change(field, { target: { value: "1" } }); |
| 172 | + expect(field).toHaveAttribute("value", "1"); |
| 173 | + expect(container.className).not.toContain("--error"); |
| 174 | + expect(message.className).not.toContain("--error"); |
| 175 | + }); |
| 176 | + |
| 177 | + it("should not update the error state on change if `validateOnChange` is an empty array", () => { |
| 178 | + const { getByPlaceholderText, getByRole } = render( |
| 179 | + <Test minLength={10} validateOnChange={[]} messageRole="alert" /> |
| 180 | + ); |
| 181 | + |
| 182 | + const field = getByPlaceholderText("Password"); |
| 183 | + const container = field.parentElement!; |
| 184 | + const message = getByRole("alert"); |
| 185 | + expect(container.className).not.toContain("--error"); |
| 186 | + expect(message.className).not.toContain("--error"); |
| 187 | + |
| 188 | + fireEvent.change(field, { target: { value: "1" } }); |
| 189 | + expect(container.className).not.toContain("--error"); |
| 190 | + expect(message.className).not.toContain("--error"); |
| 191 | + expect(field).toHaveAttribute("value", "1"); |
| 192 | + }); |
| 193 | + |
| 194 | + it("should render the helpText if when there is no error text", () => { |
| 195 | + const { getByPlaceholderText, getByRole } = render( |
| 196 | + <Test |
| 197 | + helpText="Help Text" |
| 198 | + messageRole="alert" |
| 199 | + maxLength={5} |
| 200 | + getErrorMessage={({ value }) => |
| 201 | + value.length > 0 ? "Error Message" : "" |
| 202 | + } |
| 203 | + /> |
| 204 | + ); |
| 205 | + |
| 206 | + const field = getByPlaceholderText("Password"); |
| 207 | + const container = field.parentElement!; |
| 208 | + const message = getByRole("alert"); |
| 209 | + |
| 210 | + expect(message.textContent).toBe("Help Text"); |
| 211 | + expect(container.className).not.toContain("--error"); |
| 212 | + expect(message.className).not.toContain("--error"); |
| 213 | + |
| 214 | + fireEvent.change(field, { target: { value: "Invalid" } }); |
| 215 | + expect(message.textContent).toBe("Error Message"); |
| 216 | + expect(container.className).toContain("--error"); |
| 217 | + expect(message.className).toContain("--error"); |
| 218 | + }); |
| 219 | + |
| 220 | + it.todo( |
| 221 | + "should verify the constraint validation, but it requires a real browser to work" |
| 222 | + ); |
| 223 | +}); |
0 commit comments