Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear function on non text inputs #262

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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("");
}
);
});
});
19 changes: 16 additions & 3 deletions src/index.js
Expand Up @@ -106,29 +106,42 @@ function fireChangeEvent(event) {
}

const Keys = {
Backspace: { keyCode: 8, code: "Backspace", key: "Backspace" }
Backspace: { keyCode: 8, code: "Backspace", key: "Backspace" },
};

function backspace(element) {
const eventOptions = {
key: Keys.Backspace.key,
keyCode: Keys.Backspace.keyCode,
which: Keys.Backspace.keyCode
which: Keys.Backspace.keyCode,
};
fireEvent.keyDown(element, eventOptions);
fireEvent.keyUp(element, eventOptions);

if (!element.readOnly) {
fireEvent.input(element, {
inputType: "deleteContentBackward"
inputType: "deleteContentBackward",
});
element.value = ""; // when we add special keys to API, will need to respect selected range
}
}

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)) {
Meemaw marked this conversation as resolved.
Show resolved Hide resolved
// 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