|
1 | 1 | // TODO: Figure out how to test the resize behavior in jsdom, or just write
|
2 | 2 | // tests with cypress
|
3 |
| -import React from "react"; |
| 3 | +import React, { ReactElement, useState } from "react"; |
4 | 4 | import { act, fireEvent, render } from "@testing-library/react";
|
5 | 5 | import { mocked } from "ts-jest/utils";
|
6 | 6 | import ResizeObserverPolyfill from "resize-observer-polyfill";
|
@@ -145,4 +145,53 @@ describe("TextArea", () => {
|
145 | 145 | rerender(<TextArea {...props} resize="horizontal" />);
|
146 | 146 | expect(realContainer).toHaveClass("rmd-text-field-container--inline");
|
147 | 147 | });
|
| 148 | + |
| 149 | + it("should handle the floating label state correctly for controlled values", () => { |
| 150 | + function Test(): ReactElement { |
| 151 | + const [value, setValue] = useState(""); |
| 152 | + |
| 153 | + return ( |
| 154 | + <> |
| 155 | + <button type="button" onClick={() => setValue("100")}> |
| 156 | + Set |
| 157 | + </button> |
| 158 | + <button type="button" onClick={() => setValue("")}> |
| 159 | + Reset |
| 160 | + </button> |
| 161 | + <TextArea |
| 162 | + id="field-id" |
| 163 | + label="Label" |
| 164 | + value={value} |
| 165 | + onChange={(event) => setValue(event.currentTarget.value)} |
| 166 | + /> |
| 167 | + </> |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + const { getByRole, getByText } = render(<Test />); |
| 172 | + |
| 173 | + const setButton = getByRole("button", { name: "Set" }); |
| 174 | + const resetButton = getByRole("button", { name: "Reset" }); |
| 175 | + const field = getByRole("textbox") as HTMLInputElement; |
| 176 | + const label = getByText("Label"); |
| 177 | + expect(label.className).not.toContain("rmd-floating-label--active"); |
| 178 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 179 | + |
| 180 | + fireEvent.click(setButton); |
| 181 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 182 | + expect(label.className).toContain("rmd-floating-label--inactive"); |
| 183 | + |
| 184 | + fireEvent.focus(field); |
| 185 | + fireEvent.change(field, { target: { value: "100-" } }); |
| 186 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 187 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 188 | + |
| 189 | + fireEvent.blur(field); |
| 190 | + expect(label.className).toContain("rmd-floating-label--active"); |
| 191 | + expect(label.className).toContain("rmd-floating-label--inactive"); |
| 192 | + |
| 193 | + fireEvent.click(resetButton); |
| 194 | + expect(label.className).not.toContain("rmd-floating-label--active"); |
| 195 | + expect(label.className).not.toContain("rmd-floating-label--inactive"); |
| 196 | + }); |
148 | 197 | });
|
0 commit comments