From 9e8b70d27fe6f1a2832d9fc4e95f6930bac686dd Mon Sep 17 00:00:00 2001 From: Chris Bacon Date: Fri, 20 Mar 2020 22:06:17 -0600 Subject: [PATCH 1/2] fix:type no longer clips final character with negative maxlength --- __tests__/react/type.js | 18 ++++++++++++++++++ __tests__/vue/type.js | 18 ++++++++++++++++++ src/index.js | 6 +++++- 3 files changed, 41 insertions(+), 1 deletion(-) 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 075e3edb..71caa88f 100644 --- a/src/index.js +++ b/src/index.js @@ -183,7 +183,11 @@ const userEvent = { }; const opts = Object.assign(defaultOpts, userOpts); - const computedText = text.slice(0, element.maxLength || text.length); + const maxLength = + element.maxLength && element.maxLength >= 0 + ? element.maxLength + : text.length; + const computedText = text.slice(0, maxLength); const previousText = element.value; From ebdb961edbeb801821a03d8dfa97a56d63163f3b Mon Sep 17 00:00:00 2001 From: Chris Bacon Date: Sat, 28 Mar 2020 11:47:54 -0600 Subject: [PATCH 2/2] clarify intent behind maxLength check --- src/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 71caa88f..a7e20093 100644 --- a/src/index.js +++ b/src/index.js @@ -183,10 +183,7 @@ const userEvent = { }; const opts = Object.assign(defaultOpts, userOpts); - const maxLength = - element.maxLength && element.maxLength >= 0 - ? element.maxLength - : text.length; + const maxLength = element.maxLength > 0 ? element.maxLength : text.length; const computedText = text.slice(0, maxLength); const previousText = element.value;