Skip to content

Commit

Permalink
Merge pull request #11 from Gpx/type
Browse files Browse the repository at this point in the history
feat: 🎸 add userEvent.type
  • Loading branch information
Gpx committed Sep 23, 2018
2 parents 46ab69b + a38293d commit f0d9e3f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
File renamed without changes.
20 changes: 20 additions & 0 deletions __tests__/type.js
@@ -0,0 +1,20 @@
import React from "react";
import { render, cleanup } from "react-testing-library";
import "jest-dom/extend-expect";
import userEvent from "../src";

afterEach(cleanup);

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

expect(onChange).toHaveBeenCalledTimes(text.length);
expect(getByTestId("input")).toHaveProperty("value", text);
});
});
8 changes: 8 additions & 0 deletions src/index.js
Expand Up @@ -71,6 +71,14 @@ const userEvent = {
}

wasAnotherElementFocused && focusedElement.blur();
},
type(element, text) {
this.click(element);
text
.split("")
.forEach((_, i) =>
fireEvent.change(element, { target: { value: text.slice(0, i + 1) } })
);
}
};

Expand Down

0 comments on commit f0d9e3f

Please sign in to comment.