Skip to content

Commit f0d9e3f

Browse files
authored
Merge pull request #11 from Gpx/type
feat: 🎸 add userEvent.type
2 parents 46ab69b + a38293d commit f0d9e3f

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
File renamed without changes.

__tests__/type.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from "react";
2+
import { render, cleanup } from "react-testing-library";
3+
import "jest-dom/extend-expect";
4+
import userEvent from "../src";
5+
6+
afterEach(cleanup);
7+
8+
describe("userEvent.type", () => {
9+
it.each(["input", "textarea"])("should type text in <%s>", type => {
10+
const onChange = jest.fn();
11+
const { getByTestId } = render(
12+
React.createElement(type, { "data-testid": "input", onChange: onChange })
13+
);
14+
const text = "Hello, world!";
15+
userEvent.type(getByTestId("input"), text);
16+
17+
expect(onChange).toHaveBeenCalledTimes(text.length);
18+
expect(getByTestId("input")).toHaveProperty("value", text);
19+
});
20+
});

src/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ const userEvent = {
7171
}
7272

7373
wasAnotherElementFocused && focusedElement.blur();
74+
},
75+
type(element, text) {
76+
this.click(element);
77+
text
78+
.split("")
79+
.forEach((_, i) =>
80+
fireEvent.change(element, { target: { value: text.slice(0, i + 1) } })
81+
);
7482
}
7583
};
7684

0 commit comments

Comments
 (0)