Skip to content

Commit e2272ab

Browse files
authored
fix: clear function on non text inputs (testing-library#262)
1 parent cc01536 commit e2272ab

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

__tests__/react/clear.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import userEvent from "../../src";
66
afterEach(cleanup);
77

88
describe("userEvent.clear", () => {
9-
it.each(["input", "textarea"])("should clear text in <%s>", type => {
9+
it.each(["input", "textarea"])("should clear text in <%s>", (type) => {
1010
const onChange = jest.fn();
1111
const { getByTestId } = render(
1212
React.createElement(type, {
1313
"data-testid": "input",
1414
onChange: onChange,
15-
value: "Hello, world!"
15+
value: "Hello, world!",
1616
})
1717
);
1818

@@ -23,15 +23,15 @@ describe("userEvent.clear", () => {
2323

2424
it.each(["input", "textarea"])(
2525
"should not clear when <%s> is disabled",
26-
type => {
26+
(type) => {
2727
const text = "Hello, world!";
2828
const onChange = jest.fn();
2929
const { getByTestId } = render(
3030
React.createElement(type, {
3131
"data-testid": "input",
3232
onChange: onChange,
3333
value: text,
34-
disabled: true
34+
disabled: true,
3535
})
3636
);
3737

@@ -43,7 +43,7 @@ describe("userEvent.clear", () => {
4343

4444
it.each(["input", "textarea"])(
4545
"should not clear when <%s> is readOnly",
46-
type => {
46+
(type) => {
4747
const onChange = jest.fn();
4848
const onKeyDown = jest.fn();
4949
const onKeyUp = jest.fn();
@@ -56,7 +56,7 @@ describe("userEvent.clear", () => {
5656
onKeyDown,
5757
onKeyUp,
5858
value: text,
59-
readOnly: true
59+
readOnly: true,
6060
})
6161
);
6262

@@ -67,4 +67,28 @@ describe("userEvent.clear", () => {
6767
expect(input).toHaveProperty("value", text);
6868
}
6969
);
70+
71+
["email", "password", "number", "text"].forEach((type) => {
72+
it.each(["input", "textarea"])(
73+
`should clear when <%s> is of type="${type}"`,
74+
(inputType) => {
75+
const value = "12345";
76+
const placeholder = "Enter password";
77+
78+
const element = React.createElement(inputType, {
79+
value,
80+
placeholder,
81+
type,
82+
onChange: jest.fn(),
83+
});
84+
85+
const { getByPlaceholderText } = render(element);
86+
87+
const input = getByPlaceholderText(placeholder);
88+
expect(input.value).toBe(value);
89+
userEvent.clear(input);
90+
expect(input.value).toBe("");
91+
}
92+
);
93+
});
7094
});

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,20 @@ function backspace(element) {
134134

135135
function selectAll(element) {
136136
userEvent.dblClick(element); // simulate events (will not actually select)
137+
const elementType = element.type;
138+
// type is a readonly property on textarea, so check if element is an input before trying to modify it
139+
if (isInputElement(element)) {
140+
// setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
141+
element.type = "text";
142+
}
137143
element.setSelectionRange(0, element.value.length);
144+
if (isInputElement(element)) {
145+
element.type = elementType;
146+
}
147+
}
148+
149+
function isInputElement(element) {
150+
return element.tagName.toLowerCase() === "input";
138151
}
139152

140153
const userEvent = {

0 commit comments

Comments
 (0)