Test fails when using userEvent.type for an input element of type 'number'
See original GitHub issueHello, 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:
- Created 3 years ago
- Reactions:2
- Comments:16 (7 by maintainers)
Top GitHub Comments
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@kentcdodds Correct me if I’m wrong but
userEvent
still does not handleonChange
. For example, I have a input with typenumber
anduserEvent.click
wouldn’t work for me. I had to resort to usingfireEvent
.userEvent
usesfireEvent
under the code, yet I don’t see anyfireEvent.change
withinuserEvent
when checking the code. Will there be an update supporting this or maybe I’m just not seeing another way to useuserEvent
?I’m my case I’m using
findByLabelText
but I’ve also tested withgetByLabelText
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.Thanks so much 🙏🏻