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 all commits
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
3 changes: 2 additions & 1 deletion src/index.js
Expand Up @@ -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;

Choose a reason for hiding this comment

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

From the MDN documentation:

The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an or <textarea>. This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.

So it seems like we wouldn't want to check for maxLength > 0, because 0 is a valid value for maxLength. It needs to check for >= 0 instead, which necessitates another test for maxLength: 0

@a-bates

const computedText = text.slice(0, maxLength);

const previousText = element.value;

Expand Down