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.

onBlur and onFocus events do not fire when running against React 17.x

See original GitHub issue
  • @testing-library/user-event version:
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.0.0",
"@testing-library/user-event": "^13.0.1",
  • Testing Framework and version:
"jest": "^26.0.1",
  • DOM Environment: Default jest env using tsdx test

Relevant code or config

          // Act
          expect(document.body).toHaveFocus();

          // Tab in
          userEvent.tab();
          expect(element).toHaveFocus();

          // Tab out
          userEvent.tab();

          // Should have triggered "onBlur"
          expect(handleBlur).toHaveBeenCalledTimes(1);

What you did: I updated from react 16 to react 17. user-event functions that used to trigger blur and focus events no longer trigger those events. I’ve got onBlur and onFocus listeners, so I’d expect them to still be triggered, for example userEvent.tab().

What happened:

● DateTime › end-to-end › events › onBlur › should trigger onBlur with no value when tabbed out with no value

    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 1
    Received number of calls: 0

      2809 |
      2810 |           // Should have triggered "onBlur"
    > 2811 |           expect(handleBlur).toHaveBeenCalledTimes(1);
           |                              ^
      2812 |           expect(element).not.toHaveFocus();
      2813 |           expect(handleBlur).toHaveBeenCalledWith(undefined);
      2814 |         });

      at src/DateTime.test.tsx:2811:30
      at step (node_modules/tslib/tslib.js:143:27)
      at Object.next (node_modules/tslib/tslib.js:124:57)
      at fulfilled (node_modules/tslib/tslib.js:114:62)

Reproduction repository: https://github.com/NateRadebaugh/react-datetime/pull/1789/commits/f058eb1ca2ffc45f556bf7b5cff00883ac878c16

Problem description:

Suggested solution: Tab and other focus/blur-related user events should trigger the appropriate focusin and focusout bubbled events.

https://reactjs.org/blog/2020/08/10/react-v17-rc.html#aligning-with-browsers

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
ph-fritschecommented, Apr 9, 2021

@oriolcp Thanks for clearing this up.

jsdom was the culprit. There was this comment on another issue:

The problem we were seeing was that jsdom 16.2.2 (what we were using at the time) actually didn’t fire focusin or focusout events when those calls were made, as browsers do. But it turns out jsdom actually fixed that in 16.3.0, so just had to upgrade.

1reaction
believeinalexcommented, Apr 5, 2021

I just tried to upgrade to React 17 and encountered the same issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React 17 internally uses the browser's focusin and focusout ...
In React, the onFocus event is called when the element receives focus and onBlur event is called when focus has left the element....
Read more >
onblur event is not firing - javascript - Stack Overflow
To have Onblur on an element it should receive focus first, Div elements don't receive focus by default. You can add tabindex="0"
Read more >
SyntheticEvent - React
This reference guide documents the SyntheticEvent wrapper that forms part of React's Event System. See the Handling Events guide to learn more.
Read more >
Client-side Checking with React.js JSX Events Using onBlur
onBlur triggers when focus is lost from the input element in context. In React.js, we bind event handlers by passing a method defined...
Read more >
Upgrading to React 17: How to Fix the Issues and Breaking ...
First, jsdom <16.3.0 only fires focus and blur events when element.focus() and element.blur() are called. This is ...
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