diff --git a/__tests__/type.js b/__tests__/type.js index 05981758..9a7c98fa 100644 --- a/__tests__/type.js +++ b/__tests__/type.js @@ -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); + } + ); }); diff --git a/src/index.js b/src/index.js index 3ed53e1f..f2134d5c 100644 --- a/src/index.js +++ b/src/index.js @@ -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) } }) + ); + } } };