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.

Test fails when using userEvent.type for an input element of type 'number'

See original GitHub issue

Hello, I recently updated the versions for the following dependencies, and I’m now encountering an issue when using userEvent.type to type a number: “@testing-library/jest-dom”: “^5.11.1”, // Used to be 4.2.4 “@testing-library/react”: “^10.4.7”, // Used to be 9.5.0 “@testing-library/user-event”: “^12.0.13”, //Used to be 11.4.2

Here are all of the dependencies in my project: “@testing-library/jest-dom”: “^5.11.1”, “@testing-library/react”: “^10.4.7”, “@testing-library/user-event”: “^12.0.13”, “prop-types”: “^15.7.2”, “react”: “^16.13.1”, “react-dom”: “^16.13.1”, “react-scripts”: “3.4.1”, “react-test-renderer”: “^16.13.1”

Here’s the test that is failing:

let mockCallback;
beforeEach(() => {
  mockCallback = jest.fn();
  render(<CreateProfileForm label='+' callback={mockCallback}/>);
});

it('weight input gets updated as user types in weight', async () => {
  const weightInput = screen.getByLabelText('Weight(lb)');
  await userEvent.type(weightInput, '200');
  expect(weightInput).toHaveAttribute('value', '200');
});

The test fails because the weight input box was not updated:

    expect(element).toHaveAttribute("value", "200") // element.getAttribute("value") === "200"

    Expected the element to have attribute:
      value="200"
    Received:
      value=""

      38 |   const weightInput = screen.getByLabelText('Weight(lb)');
      39 |   await userEvent.type(weightInput, '200');
    > 40 |   expect(weightInput).toHaveAttribute('value', '200');
         |                       ^
      41 | });

The ‘weightInput’ element is of type ‘number’. The issue however, does not occur when using userEvent.type for an input of type ‘text’.

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
robertairdcommented, Nov 11, 2020

I ran into this same issue, and managed to get it working by setting { delay: 1 } as the third argument. Probably not ideal, but it’ll do for now

1reaction
maariyadiminskycommented, Dec 24, 2021

@kentcdodds Correct me if I’m wrong but userEvent still does not handle onChange. For example, I have a input with type number and userEvent.click wouldn’t work for me. I had to resort to using fireEvent.

userEvent uses fireEvent under the code, yet I don’t see any fireEvent.change within userEvent when checking the code. Will there be an update supporting this or maybe I’m just not seeing another way to use userEvent?

I’m my case I’m using findByLabelText but I’ve also tested with getByLabelText as shown here, yet it still doesn’t work.

This is what I have for now, but it would be great to use userEvent instead as I’m using it everywhere else.

const newEvent = {
 ...someDateHere,
  price: 70
};

const priceInput = await findByLabelText('Price');

// doesn't work, a float is expected in Apollo. I wonder if this is related.
// errors is result = ""
// userEvent.type(priceInput, newEvent.price); 

fireEvent.change(priceInput, { target: { value: newEvent.price }}); // works perfect
expect(priceInput.value).toBe(`${newEvent.price}`);

Thanks so much 🙏🏻

Read more comments on GitHub >

github_iconTop Results From Across the Web

userEvent.type not updating value of input in test
test ('type into an input field', async () => { render(<input defaultValue="Hello," />) const input = screen.getByRole('textbox') await userEvent ...
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.
Read more >
Simulate Typing into Form Fields in Tests with the User Event ...
The interface is fairly straight forward in most cases you simply say userEvent["eventName"] and then pass in an element returned from a findBy...
Read more >
Mimicking User Interactions - Learn React Testing
npm install --save-dev @testing-library/user-event@12.0.4. Note: There is a known issue with the userEvent.type() method that was resolved in issue 12.0.4.
Read more >
@testing-library/user-event.type JavaScript and Node.js code ...
How to use. type. function. in. @testing-library/user-event ... const input = getByLabelText(/favorite number/i) //fireEvent.change(input, {target: {value: ...
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