Skip to content

Commit

Permalink
fix: clear function on non text inputs (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meemaw committed May 14, 2020
1 parent cc01536 commit e2272ab
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
36 changes: 30 additions & 6 deletions __tests__/react/clear.js
Expand Up @@ -6,13 +6,13 @@ import userEvent from "../../src";
afterEach(cleanup);

describe("userEvent.clear", () => {
it.each(["input", "textarea"])("should clear text in <%s>", type => {
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!"
value: "Hello, world!",
})
);

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

it.each(["input", "textarea"])(
"should not clear when <%s> is disabled",
type => {
(type) => {
const text = "Hello, world!";
const onChange = jest.fn();
const { getByTestId } = render(
React.createElement(type, {
"data-testid": "input",
onChange: onChange,
value: text,
disabled: true
disabled: true,
})
);

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

it.each(["input", "textarea"])(
"should not clear when <%s> is readOnly",
type => {
(type) => {
const onChange = jest.fn();
const onKeyDown = jest.fn();
const onKeyUp = jest.fn();
Expand All @@ -56,7 +56,7 @@ describe("userEvent.clear", () => {
onKeyDown,
onKeyUp,
value: text,
readOnly: true
readOnly: true,
})
);

Expand All @@ -67,4 +67,28 @@ describe("userEvent.clear", () => {
expect(input).toHaveProperty("value", text);
}
);

["email", "password", "number", "text"].forEach((type) => {
it.each(["input", "textarea"])(
`should clear when <%s> is of type="${type}"`,
(inputType) => {
const value = "12345";
const placeholder = "Enter password";

const element = React.createElement(inputType, {
value,
placeholder,
type,
onChange: jest.fn(),
});

const { getByPlaceholderText } = render(element);

const input = getByPlaceholderText(placeholder);
expect(input.value).toBe(value);
userEvent.clear(input);
expect(input.value).toBe("");
}
);
});
});
13 changes: 13 additions & 0 deletions src/index.js
Expand Up @@ -134,7 +134,20 @@ function backspace(element) {

function selectAll(element) {
userEvent.dblClick(element); // simulate events (will not actually select)
const elementType = element.type;
// type is a readonly property on textarea, so check if element is an input before trying to modify it
if (isInputElement(element)) {
// setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
element.type = "text";
}
element.setSelectionRange(0, element.value.length);
if (isInputElement(element)) {
element.type = elementType;
}
}

function isInputElement(element) {
return element.tagName.toLowerCase() === "input";
}

const userEvent = {
Expand Down

0 comments on commit e2272ab

Please sign in to comment.