-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: make fireEvent mouseEnter/mouseLeave work with addEventListener #588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: make fireEvent mouseEnter/mouseLeave work with addEventListener #588
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
Codecov Report
@@ Coverage Diff @@
## master #588 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 3 3
Lines 95 103 +8
Branches 15 15
=====================================
+ Hits 95 103 +8
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Thanks! Seems like the right solution.
Maybe add the tests for the specific issue this solves. At least the one that would have previously failed?
test('fires native mouseEnter/mouseLeave events', () => {...}
They were enumerated here: #577 (comment)
Cool, thanks, @zbrogz, well done :D |
I'll update this with tests. |
Thank you so much for this. I would love a test as well. Is there a real world scenario where people would have to callbacks called unexpectedly? |
I added tests to verify that all native events can be triggered using
If the scope of this is too big, I'll remove this and just write a test limited to onMouseEnter/onMouseLeave, like mentioned earlier. Edit: I have a fix for native |
I don't believe so. This makes it so |
@kentcdodds Do you mind taking a look at this again? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm liking this. Just a few thoughts on how to change this slightly. Thanks!
src/__tests__/events.js
Outdated
@@ -128,6 +128,9 @@ const eventTypes = [ | |||
}, | |||
] | |||
|
|||
// native select event isn't passing | |||
const nonNativeEvents = {doubleClick: 'dblclick', select: true} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should doubleclick
even be in the events list?
Also, select
probably doesn't work because JSDOM, which is unfortunate.
I probably wouldn't implement things this way and instead I would just have if statements in the test itself for each of these individually with comments explaining why these are special cases. No reason to have this indirection/abstraction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kentcdodds I made some changes based on your feedback.
instead I would just have if statements in the test itself for each of these individually with comments explaining why these are special cases
Done
Should doubleclick even be in the events list?
I think it makes sense to test both the doubleClick (synthetic) and dblclick (native) events.
Also, select probably doesn't work because JSDOM, which is unfortunate.
I included a fix for this in the latest commit.
…zation of native events test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Terrific 👍👍
@all-contributors please add @zbrogz for code and tests |
I've put up a pull request to add @zbrogz! 🎉 |
🎉 This PR is included in version 9.4.1 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
(#577)
What:
Make
fireEvent.mouseEnter
andfireEvent.mouseLeave
work with events added usingaddEventListener
.Why:
There is no way to trigger a native mouseEnter/mouseLeave event using React Testing Library. A workaround is to manually use
dispatchEvent
, but it's not as convenient asfireEvent
. The reason for this limitation is due to how React handles mouseEnter/mouseLeave: facebook/react#12978How:
Instead of setting
fireEvent.mouseEnter/Leave
tofireEvent.mouseOver/Out
, setfireEvent.mouseEnter/Leave
to call bothmouseEnter/Leave
andfireEvent.mouseOver/Out
.This makes the behavior match other events that can be called with
fireEvent
. For example, if one click handler was added to an element withaddEventListener
and another was added to the component directly with<button onClick={handleClick}>...
,fireEvent.click(button)
calls both click handlers.Checklist:
docs site
Credit to @jessethomson