From a38293d772b8885edcc8f4ba112c626d63800d17 Mon Sep 17 00:00:00 2001 From: Gpx Date: Sun, 23 Sep 2018 11:56:02 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20userEvent.type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/{events.js => click.js} | 0 __tests__/type.js | 20 ++++++++++++++++++++ src/index.js | 8 ++++++++ 3 files changed, 28 insertions(+) rename __tests__/{events.js => click.js} (100%) create mode 100644 __tests__/type.js diff --git a/__tests__/events.js b/__tests__/click.js similarity index 100% rename from __tests__/events.js rename to __tests__/click.js diff --git a/__tests__/type.js b/__tests__/type.js new file mode 100644 index 00000000..05981758 --- /dev/null +++ b/__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); + }); +}); diff --git a/src/index.js b/src/index.js index dfb06e63..3ed53e1f 100644 --- a/src/index.js +++ b/src/index.js @@ -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) } }) + ); } };