diff --git a/__tests__/react/type.js b/__tests__/react/type.js index ca804e9b..e730d552 100644 --- a/__tests__/react/type.js +++ b/__tests__/react/type.js @@ -31,6 +31,24 @@ describe("userEvent.type", () => { expect(getByTestId("input")).toHaveProperty("value", "hello world"); }); + it.each(["input", "textarea"])( + "includes entire textstring with negative maxlength in <%s>", + type => { + const onChange = jest.fn(); + const { getByTestId } = render( + React.createElement(type, { + "data-testid": "input", + onChange: onChange, + maxLength: -1 + }) + ); + const text = "Hello, world!"; + userEvent.type(getByTestId("input"), text); + expect(onChange).toHaveBeenCalledTimes(text.length); + expect(getByTestId("input")).toHaveProperty("value", text); + } + ); + it("should append text all at once", () => { const onChange = jest.fn(); const { getByTestId } = render( diff --git a/__tests__/vue/type.js b/__tests__/vue/type.js index 4ce3fe7f..090d3610 100644 --- a/__tests__/vue/type.js +++ b/__tests__/vue/type.js @@ -27,6 +27,24 @@ describe("userEvent.type", () => { expect(getByTestId("input")).toHaveProperty("value", text); }); + it.each(["input", "textarea"])( + "includes entire textstring with negative maxlength in <%s>", + type => { + const input = jest.fn(); + + const { getByTestId } = renderComponent( + type, + { input }, + { maxLength: -1 } + ); + + const text = "Hello, world!"; + userEvent.type(getByTestId("input"), text); + expect(input).toHaveBeenCalledTimes(text.length); + expect(getByTestId("input")).toHaveProperty("value", text); + } + ); + it("should not type when event.preventDefault() is called", () => { const input = jest.fn(); const change = jest.fn(); diff --git a/src/index.js b/src/index.js index 352ce9e0..9b04b3a3 100644 --- a/src/index.js +++ b/src/index.js @@ -183,7 +183,8 @@ const userEvent = { }; const opts = Object.assign(defaultOpts, userOpts); - const computedText = text.slice(0, element.maxLength || text.length); + const maxLength = element.maxLength > 0 ? element.maxLength : text.length; + const computedText = text.slice(0, maxLength); const previousText = element.value;