-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fireEvent): automatically configure
fireEvent
to be wrapped in act
Now not only will React Testing Library's `fireEvent` be wrapped in `act`, but so will DOM Testing Library's `fireEvent` (if `@testing-library/react` is imported). It works very similar to async act for the `asyncWrapper` config. Closes: testing-library/user-event#188 Closes: testing-library/user-event#255 Reference: https://github.com/testing-library/user-event/issues/277
- Loading branch information
1 parent
7b7460a
commit 7bd8be8
Showing
2 changed files
with
53 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {fireEvent as dtlFireEvent} from '@testing-library/dom' | ||
|
||
// react-testing-library's version of fireEvent will call | ||
// dom-testing-library's version of fireEvent. The reason | ||
// we make this distinction however is because we have | ||
// a few extra events that work a bit differently | ||
const fireEvent = (...args) => dtlFireEvent(...args) | ||
|
||
Object.keys(dtlFireEvent).forEach(key => { | ||
fireEvent[key] = (...args) => dtlFireEvent[key](...args) | ||
}) | ||
|
||
// React event system tracks native mouseOver/mouseOut events for | ||
// running onMouseEnter/onMouseLeave handlers | ||
// @link https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/events/EnterLeaveEventPlugin.js#L24-L31 | ||
const mouseEnter = fireEvent.mouseEnter | ||
const mouseLeave = fireEvent.mouseLeave | ||
fireEvent.mouseEnter = (...args) => { | ||
mouseEnter(...args) | ||
return fireEvent.mouseOver(...args) | ||
} | ||
fireEvent.mouseLeave = (...args) => { | ||
mouseLeave(...args) | ||
return fireEvent.mouseOut(...args) | ||
} | ||
|
||
const select = fireEvent.select | ||
fireEvent.select = (node, init) => { | ||
select(node, init) | ||
// React tracks this event only on focused inputs | ||
node.focus() | ||
|
||
// React creates this event when one of the following native events happens | ||
// - contextMenu | ||
// - mouseUp | ||
// - dragEnd | ||
// - keyUp | ||
// - keyDown | ||
// so we can use any here | ||
// @link https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/events/SelectEventPlugin.js#L203-L224 | ||
fireEvent.keyUp(node, init) | ||
} | ||
|
||
export {fireEvent} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters