`userEvent.clear()` does not fire `onChange` handlers
See original GitHub issueReproduction: https://github.com/justinanastos/user-event-clear-bug
I’m finding that onChange
handlers are not being fired when using userEvent.clear(getBy...)
.
This is both a bug report and a request for help. I’m not sure what I’m doing wrong here:
import '@testing-library/jest-dom/extend-expect';
import { render, screen } from '@testing-library/react';
import React from 'react';
import userEvent from '@testing-library/user-event';
it('calling userEvent.clear() should call onChange on an input', () => {
const onChange = jest.fn();
render(
<label>
example
<input defaultValue="default value" onChange={onChange} />
</label>,
);
expect(screen.getByLabelText('example')).toHaveValue('default value');
userEvent.clear(screen.getByLabelText('example'));
expect(screen.getByLabelText('example')).toHaveValue('');
// This test fails
expect(onChange).toHaveBeenCalled();
});
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Why clear method not exist on @testing-library/user-event
As the file shows, there is no clear() method on userEvent . But the documentation points out to clear() method. Doc => Doc....
Read more >user-event v13 - Testing Library
If they are not closed explicitly, then events will be fired to close them automatically (to disable this, set the skipAutoClose option to...
Read more >React-testing-library: fireEvent vs userEvent - mimacom blog
In this case, the click() method won't only click the element, but it will also simulate the previous hover that a real user...
Read more >React Testing Library Tutorial - Robin Wieruch
At the time of writing this, userEvent doesn't include all the features of fireEvent, however, this may change in the future. React Testing ......
Read more >pygame.event — pygame v2.1.4 documentation
clear() ) and process them. Not handling events may cause your system to decide your program has locked up. To speed up queue...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d love to know why that code existed in the first place. But as the original maintainer of this project seems to have stopped maintaining it, I think we should just go forward with what makes the most sense.
Go ahead and make the PR. Thank you!
Thanks @kentcdodds . First, I verified performing
.type('{selectall}{backspace}')
both clears theinput
and fires theonChange
handler in this demo repo: https://github.com/justinanastos/user-event-clear-bug-cypressThe
clear
command adds an event handler forblur
in which it will execute the on change handler; I’m not seeing this fired even when adding a blur event to the test, which probably shouldn’t be necessary if this is attempting to mirror the behavior of Cypress.https://github.com/testing-library/user-event/blob/ee8aa5c2a7457132de6ab06a13df7e37697a99be/src/index.js#L215
I’m happy to open a PR to immediately fire an
onChange
event if that’s a path you’d be willing to accept.