Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from "react"; | ||
import { cleanup, render, wait, fireEvent } from "@testing-library/react"; | ||
import "@testing-library/jest-dom/extend-expect"; | ||
import userEvent from "../../src"; | ||
|
||
afterEach(cleanup); | ||
|
||
describe("userEvent.clear", () => { | ||
it.each(["input", "textarea"])("should clear text in <%s>", type => { | ||
const onChange = jest.fn(); | ||
const { getByTestId } = render( | ||
React.createElement(type, { | ||
"data-testid": "input", | ||
onChange: onChange, | ||
value: "Hello, world!" | ||
}) | ||
); | ||
|
||
const input = getByTestId("input"); | ||
userEvent.clear(input); | ||
expect(input.value).toBe(""); | ||
}); | ||
|
||
it.each(["input", "textarea"])( | ||
"should not clear when <%s> is disabled", | ||
type => { | ||
const text = "Hello, world!"; | ||
const onChange = jest.fn(); | ||
const { getByTestId } = render( | ||
React.createElement(type, { | ||
"data-testid": "input", | ||
onChange: onChange, | ||
value: text, | ||
disabled: true | ||
}) | ||
); | ||
|
||
const input = getByTestId("input"); | ||
userEvent.clear(input); | ||
expect(input).toHaveProperty("value", text); | ||
} | ||
); | ||
|
||
it.each(["input", "textarea"])( | ||
"should not clear when <%s> is readOnly", | ||
type => { | ||
const onChange = jest.fn(); | ||
const onKeyDown = jest.fn(); | ||
const onKeyUp = jest.fn(); | ||
|
||
const text = "Hello, world!"; | ||
const { getByTestId } = render( | ||
React.createElement(type, { | ||
"data-testid": "input", | ||
onChange, | ||
onKeyDown, | ||
onKeyUp, | ||
value: text, | ||
readOnly: true | ||
}) | ||
); | ||
|
||
const input = getByTestId("input"); | ||
userEvent.clear(input); | ||
expect(onKeyDown).toHaveBeenCalledTimes(1); | ||
expect(onKeyUp).toHaveBeenCalledTimes(1); | ||
expect(input).toHaveProperty("value", text); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters