Skip to content

Commit ca725b9

Browse files
authored
Merge pull request #12 from Gpx/type
feat: 🎸 add allAtOnce optiont to type()
2 parents f0d9e3f + 51112d6 commit ca725b9

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

__tests__/type.js

+18
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,22 @@ describe("userEvent.type", () => {
1717
expect(onChange).toHaveBeenCalledTimes(text.length);
1818
expect(getByTestId("input")).toHaveProperty("value", text);
1919
});
20+
21+
it.each(["input", "textarea"])(
22+
"should type text in <%s> all at once",
23+
type => {
24+
const onChange = jest.fn();
25+
const { getByTestId } = render(
26+
React.createElement(type, {
27+
"data-testid": "input",
28+
onChange: onChange
29+
})
30+
);
31+
const text = "Hello, world!";
32+
userEvent.type(getByTestId("input"), text, { allAtOnce: true });
33+
34+
expect(onChange).toHaveBeenCalledTimes(1);
35+
expect(getByTestId("input")).toHaveProperty("value", text);
36+
}
37+
);
2038
});

src/index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,20 @@ const userEvent = {
7272

7373
wasAnotherElementFocused && focusedElement.blur();
7474
},
75-
type(element, text) {
75+
type(element, text, userOpts = {}) {
76+
const defaultOpts = { allAtOnce: false };
77+
const opts = Object.assign(defaultOpts, userOpts);
78+
7679
this.click(element);
77-
text
78-
.split("")
79-
.forEach((_, i) =>
80-
fireEvent.change(element, { target: { value: text.slice(0, i + 1) } })
81-
);
80+
if (opts.allAtOnce) {
81+
fireEvent.change(element, { target: { value: text } });
82+
} else {
83+
text
84+
.split("")
85+
.forEach((_, i) =>
86+
fireEvent.change(element, { target: { value: text.slice(0, i + 1) } })
87+
);
88+
}
8289
}
8390
};
8491

0 commit comments

Comments
 (0)