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 - Only typing in the first letter of my provided input on React 17

See original GitHub issue
  • @testing-library/user-event version: 12.6.0
  • Testing Framework and version: Jest 26.6.3
  • DOM Environment: jsdom 16.4.0

What you did: I set up a test to check the input value after typing with text of “Input”

What happened: The input value in the test was only set to “I”

Reproduction repository: https://codesandbox.io/s/peaceful-surf-fedc7?file=/src/App.spec.js

Problem description: The userEvent is not firing off completely and is only typing the first letter of my provided input on React 17. This is leading to cases where I can’t write complete tests currently. I am not totally sure if this will help but there are similar issues with fireEvent as well.

Suggested solution:

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
ph-fritschecommented, Dec 24, 2020

This one is tricky.

Remember variables are bound to their declaration. Properties are resolved when the expression is executed.

  const onChange = (event) => {
    setState((prevState) => ({
      ...prevState,
      // event is bound - event.target.value is resolved when the reducer is executed
      [event.target.name]: event.target.value
    }));
  };

  // this one works
  const onChange = (event) => {
    const value = event.target.value
    setState((prevState) => ({
      ...prevState,
      [event.target.name]: value
    }));
  };

Now React hooks are no language constructs and you can use them in any way, but I guess it doesn’t make life easier. While setState with a reducer works, I suggest you use useReducer for that.

  const [state, change] = useReducer(
    (prevState, newProps) => ({
      ...prevState,
      ...newProps
    }),
    {}
  );
  const onChange = (event) => {
    change({
      [event.target.name]: event.target.value
    });
  };

Regarding the issue, I think it should be closed as out of scope, but someone else should have a look at this first. While it does behave differently in the browser, the reasons lie deep down in how fireEvent creates the Event on an element controlled by React. I’m not sure there is an easy way to fix this.

1reaction
andreasdjscommented, Nov 5, 2021

@ph-fritsche

I had the same problem and your solution saved the day for me, thank you! 👍

Like you wrote, the value had to be resolved before putting it inside the prevState thingy, like this: const { value } = event.target;

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Testing Library userEvent.type recognizing only the first ...
As you see, it is executing the test without any useRouter errors but not recognizing entire text. It just takes the first letter...
Read more >
Only typing in the first letter of my provided input on React 17
userEvent.type - Only typing in the first letter of my provided input on React 17. ... @testing-library/user-event version: 12.6.0.
Read more >
user-event v13 - Testing Library
You should use userEvent.type if you just want to conveniently insert some text into an input field or textarea. Keystrokes can be described:....
Read more >
Simulate Typing into Form Fields in Tests with the User Event ...
Now, type userevent.clearinput. We're going to clear out whatever is in there. We're going to clear out the input field. Now that we...
Read more >
Common mistakes with React Testing Library - Kent C. Dodds
Despite our efforts to document the "better way" to use the utilities we provide, I still see blog posts and tests written following...
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