Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:type no longer clips final character with negative maxlength #239

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions __tests__/react/type.js
Expand Up @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions __tests__/vue/type.js
Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Expand Up @@ -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
Copy link

@apalaniuk apalaniuk Mar 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

element.maxLength && element.maxLength >= 0 seems slightly misleading because >= 0 makes it look like element.maxLength will be applied if it's 0, which isn't true. Particularly relevant in that jsdom seems to use 0 as its default/unlimited maxlength value for HTMLTextArea, which deviates from specs/browser implementations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a really good point that I overlooked. I've updated this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context, I opened jsdom/jsdom#2927 since that's the real source of deviation

? element.maxLength
: text.length;
const computedText = text.slice(0, maxLength);

const previousText = element.value;

Expand Down