Skip to content

Commit

Permalink
feat: 🎸 add allAtOnce optiont to type()
Browse files Browse the repository at this point in the history
  • Loading branch information
Gpx committed Sep 23, 2018
1 parent f0d9e3f commit 51112d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
18 changes: 18 additions & 0 deletions __tests__/type.js
Expand Up @@ -17,4 +17,22 @@ describe("userEvent.type", () => {
expect(onChange).toHaveBeenCalledTimes(text.length);
expect(getByTestId("input")).toHaveProperty("value", text);
});

it.each(["input", "textarea"])(
"should type text in <%s> all at once",
type => {
const onChange = jest.fn();
const { getByTestId } = render(
React.createElement(type, {
"data-testid": "input",
onChange: onChange
})
);
const text = "Hello, world!";
userEvent.type(getByTestId("input"), text, { allAtOnce: true });

expect(onChange).toHaveBeenCalledTimes(1);
expect(getByTestId("input")).toHaveProperty("value", text);
}
);
});
19 changes: 13 additions & 6 deletions src/index.js
Expand Up @@ -72,13 +72,20 @@ const userEvent = {

wasAnotherElementFocused && focusedElement.blur();
},
type(element, text) {
type(element, text, userOpts = {}) {
const defaultOpts = { allAtOnce: false };
const opts = Object.assign(defaultOpts, userOpts);

this.click(element);
text
.split("")
.forEach((_, i) =>
fireEvent.change(element, { target: { value: text.slice(0, i + 1) } })
);
if (opts.allAtOnce) {
fireEvent.change(element, { target: { value: text } });
} else {
text
.split("")
.forEach((_, i) =>
fireEvent.change(element, { target: { value: text.slice(0, i + 1) } })
);
}
}
};

Expand Down

0 comments on commit 51112d6

Please sign in to comment.