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

userEvent.type does not replace selected input value #301

Closed
wKovacs64 opened this issue May 28, 2020 · 7 comments · Fixed by #308
Closed

userEvent.type does not replace selected input value #301

wKovacs64 opened this issue May 28, 2020 · 7 comments · Fixed by #308
Labels

Comments

@wKovacs64
Copy link

  • @testing-library/user-event version: 10.4.0
  • Testing Framework and version: jest 24.9.0
  • DOM Environment: jsdom 14.1.0, 16.2.2

Relevant code or config

test('click, auto-focus, type-to-replace', async () => {
  render(<InputThatSelectsOwnValueOnFocus initialValue="whatever" />);
  const input = screen.getByLabelText(/input/i);

  input.focus();
  await user.type(input, 'works'); // should replace selected text

  expect(input).toHaveValue('works'); // but it doesn't, it appends
});
Expected the element to have value:
  works
Received:
  whateverworks

What you did:

I'm trying to test behavior of an input that automatically selects its contents on focus (so the user can immediately start typing to replace the value, rather than having to clear it manually).

What happened:

Typing after focusing the input is appending to the value rather than replacing it (while actual behavior is working as intended).

Reproduction repository:

https://codesandbox.io/s/user-event-type-to-replace-issue-9wu97?file=/src/__tests__/App.test.js

Note: it's unclear if CSB is properly using jest-environment-jsdom-sixteen, but the error is the same locally for jsdom v14 and v16.

Problem description:

Calling input.focus() on an "auto-select-value-on-focus" input followed by await userEvent.type(input, 'whatever') just appends whatever to the existing input value rather than replacing it.

Suggested solution:

Not sure. Is this just a slightly different manifestation of #236?

@transmissions11
Copy link

Pretty sure this is intentional, you can clear the field manually.

@wKovacs64
Copy link
Author

Pretty sure this is intentional, you can clear the field manually.

As the goal of user-event is to "fire events the same way the user does", I wouldn't expect to have to manually clear the field when the existing content has already been selected automatically. The user doesn't clear the field manually, the user simply starts typing.

@kentcdodds
Copy link
Member

Yes, this should be supported. Would you be interested in opening a PR?

@wKovacs64
Copy link
Author

Might be a little out of my zone, but I could give it a shot.

@wKovacs64
Copy link
Author

Well... I had something going for this, but #299 totally wrecked it, haha... 😅

@kentcdodds
Copy link
Member

Sorry about that!

Luckily, changing code is the easy part, knowing what needs to be changed is the challenging part.

kentcdodds added a commit that referenced this issue Jun 4, 2020
kentcdodds added a commit that referenced this issue Jun 4, 2020
@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 11.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants