|
1 |
| -import React from "react"; |
| 1 | +import React, { ReactElement, useState } from "react"; |
2 | 2 | import { fireEvent, render } from "@testing-library/react";
|
3 | 3 |
|
4 | 4 | import { TextField } from "../TextField";
|
@@ -69,6 +69,95 @@ describe("TextField", () => {
|
69 | 69 | expect(label.className).toContain("rmd-floating-label--inactive");
|
70 | 70 | });
|
71 | 71 |
|
| 72 | + it("should add the inactive floating label state when a number text field is blurred while containing an invalid value when controlled", () => { |
| 73 | + function Test(): ReactElement { |
| 74 | + const [value, setValue] = useState(""); |
| 75 | + |
| 76 | + return ( |
| 77 | + <TextField |
| 78 | + id="text-field" |
| 79 | + label="Label" |
| 80 | + type="number" |
| 81 | + value={value} |
| 82 | + onChange={(event) => setValue(event.currentTarget.value)} |
| 83 | + /> |
| 84 | + ); |
| 85 | + } |
| 86 | + const { getByRole, getByText } = render(<Test />); |
| 87 | + |
| 88 | + const field = getByRole("spinbutton") as HTMLInputElement; |
| 89 | + const label = getByText("Label"); |
| 90 | + expect(field).toHaveAttribute("value", ""); |
| 91 | + expect(label.className).not.toContain("rmd-floating-label--active"); |
| 92 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 93 | + |
| 94 | + fireEvent.focus(field); |
| 95 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 96 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 97 | + |
| 98 | + fireEvent.change(field, { target: { value: "123" } }); |
| 99 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 100 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 101 | + |
| 102 | + // TODO: Look into writing real browser tests since this isn't implemented in JSDOM |
| 103 | + Object.defineProperty(field.validity, "badInput", { |
| 104 | + writable: true, |
| 105 | + value: true, |
| 106 | + }); |
| 107 | + expect(field.validity.badInput).toBe(true); |
| 108 | + fireEvent.change(field, { |
| 109 | + target: { value: "123-" }, |
| 110 | + }); |
| 111 | + expect(field.validity.badInput).toBe(true); |
| 112 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 113 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 114 | + |
| 115 | + fireEvent.blur(field); |
| 116 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 117 | + expect(label.className).toContain("rmd-floating-label--inactive"); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should add the floating inactive state for a number field that is initially rendered with a value", () => { |
| 121 | + const onBlur = jest.fn(); |
| 122 | + function Test(): ReactElement { |
| 123 | + const [value, setValue] = useState("0"); |
| 124 | + |
| 125 | + return ( |
| 126 | + <TextField |
| 127 | + id="text-field" |
| 128 | + label="Label" |
| 129 | + type="number" |
| 130 | + value={value} |
| 131 | + onBlur={onBlur} |
| 132 | + onChange={(event) => setValue(event.currentTarget.value)} |
| 133 | + /> |
| 134 | + ); |
| 135 | + } |
| 136 | + const { getByRole, getByText } = render(<Test />); |
| 137 | + |
| 138 | + const field = getByRole("spinbutton") as HTMLInputElement; |
| 139 | + const label = getByText("Label"); |
| 140 | + expect(field).toHaveAttribute("value", "0"); |
| 141 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 142 | + expect(label.className).toContain("rmd-floating-label--inactive"); |
| 143 | + |
| 144 | + fireEvent.focus(field); |
| 145 | + fireEvent.change(field, { target: { value: "" } }); |
| 146 | + fireEvent.blur(field); |
| 147 | + expect(onBlur).toBeCalledTimes(1); |
| 148 | + expect(label.className).not.toContain("rmd-floating-label--active"); |
| 149 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 150 | + |
| 151 | + fireEvent.focus(field); |
| 152 | + fireEvent.change(field, { target: { value: "3" } }); |
| 153 | + fireEvent.change(field, { target: { value: "3-" } }); |
| 154 | + fireEvent.change(field, { target: { value: "3" } }); |
| 155 | + fireEvent.blur(field); |
| 156 | + expect(onBlur).toBeCalledTimes(2); |
| 157 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 158 | + expect(label.className).toContain("rmd-floating-label--inactive"); |
| 159 | + }); |
| 160 | + |
72 | 161 | it("should not add the inactive floating label state when a non-number type has a badInput validity", () => {
|
73 | 162 | const { getByRole, getByText } = render(
|
74 | 163 | <TextField id="text-field" label="Label" type="url" defaultValue="" />
|
@@ -108,4 +197,55 @@ describe("TextField", () => {
|
108 | 197 | expect(label.className).not.toContain("rmd-floating-label--active");
|
109 | 198 | expect(label.className).not.toContain("rmd-floating-label--inactive");
|
110 | 199 | });
|
| 200 | + |
| 201 | + it("should not add the inactive floating label state when a non-number type has a badInput validity when controlled", () => { |
| 202 | + function Test(): ReactElement { |
| 203 | + const [value, setValue] = useState(""); |
| 204 | + |
| 205 | + return ( |
| 206 | + <TextField |
| 207 | + id="text-field" |
| 208 | + label="Label" |
| 209 | + type="url" |
| 210 | + value={value} |
| 211 | + onChange={(event) => setValue(event.currentTarget.value)} |
| 212 | + /> |
| 213 | + ); |
| 214 | + } |
| 215 | + const { getByRole, getByText } = render(<Test />); |
| 216 | + |
| 217 | + const field = getByRole("textbox") as HTMLInputElement; |
| 218 | + const label = getByText("Label"); |
| 219 | + expect(field).toHaveAttribute("value", ""); |
| 220 | + expect(label.className).not.toContain("rmd-floating-label--active"); |
| 221 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 222 | + |
| 223 | + fireEvent.focus(field); |
| 224 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 225 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 226 | + |
| 227 | + // TODO: Look into writing real browser tests since this isn't implemented in JSDOM |
| 228 | + Object.defineProperty(field.validity, "badInput", { |
| 229 | + writable: true, |
| 230 | + value: true, |
| 231 | + }); |
| 232 | + fireEvent.change(field, { target: { value: "123" } }); |
| 233 | + expect(field.validity.badInput).toBe(true); |
| 234 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 235 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 236 | + |
| 237 | + fireEvent.blur(field); |
| 238 | + expect(field.validity.badInput).toBe(true); |
| 239 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 240 | + expect(label.className).toContain("rmd-floating-label--inactive"); |
| 241 | + |
| 242 | + fireEvent.focus(field); |
| 243 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 244 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 245 | + |
| 246 | + fireEvent.change(field, { target: { value: "" } }); |
| 247 | + fireEvent.blur(field); |
| 248 | + expect(label.className).not.toContain("rmd-floating-label--active"); |
| 249 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 250 | + }); |
111 | 251 | });
|
0 commit comments