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.clear()` does not fire `onChange` handlers

See original GitHub issue

Reproduction: 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:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, May 18, 2020

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!

1reaction
justinanastoscommented, May 18, 2020

Thanks @kentcdodds . First, I verified performing .type('{selectall}{backspace}') both clears the input and fires the onChange handler in this demo repo: https://github.com/justinanastos/user-event-clear-bug-cypress

The clear command adds an event handler for blur 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.

diff --git a/src/index.js b/src/index.js
index 352ce9e..c9e23c8 100644
--- a/src/index.js
+++ b/src/index.js
@@ -100,11 +100,6 @@ function selectOption(select, option) {
   fireEvent.change(select);
 }
 
-function fireChangeEvent(event) {
-  fireEvent.change(event.target);
-  event.target.removeEventListener("blur", fireChangeEvent);
-}
-
 const userEvent = {
   click(element) {
     const focusedElement = element.ownerDocument.activeElement;
@@ -237,7 +232,7 @@ const userEvent = {
         });
       }
     }
-    element.addEventListener("blur", fireChangeEvent);
+    fireEvent.change(event);
   },
 
   tab({ shift = false, focusTrap = document } = {}) {
Read more comments on GitHub >

github_iconTop 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 >

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