question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

userEvent.type does not replace selected input value

See original GitHub issue
  • @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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kentcdoddscommented, Jun 4, 2020

🎉 This issue has been resolved in version 11.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

0reactions
kentcdoddscommented, Jun 4, 2020

Sorry about that!

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

userEvent.type not updating value of input in test
I am testing this component, which is simply changing its state depending on new input into the input field and setting the value...
Read more >
user-event v13 - Testing Library
Simulates the keyboard events described by text . This is similar to userEvent.type() but without any clicking or changing the selection range.
Read more >
user-event-type-to-replace-issue - CodeSandbox
Clicking inside the input should automatically select the contents,. allowing you to type-to-replace without manually clearing the current. value.
Read more >
SuiteScript 2.x User Event Script Type - Oracle Help Center
User event scripts are executed on the NetSuite server. They are executed when users perform certain actions on records, such as create, load,...
Read more >
React Testing Library Tutorial - Robin Wieruch
React Testing Library is not an alternative to Jest, because they need ... <input /> ) element that we can retrieve with this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found