Skip to content

Commit 3185bdd

Browse files
committed
Quick solution for #206
1 parent 799d6ca commit 3185bdd

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ReactDOM from 'react-dom'
2-
import {Simulate} from 'react-dom/test-utils'
32
import {getQueriesForElement, prettyDOM, fireEvent} from 'dom-testing-library'
43

54
const mountedContainers = new Set()
@@ -53,6 +52,25 @@ fireEvent.change = function reactChange(node, init) {
5352
return originalChange(node, init)
5453
}
5554

55+
// probably there is a better way to get access to a component instance
56+
const [
57+
getInstanceFromNode,
58+
] = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events
59+
const createReactEventSimulator = eventName => (node, init) => {
60+
const propName = `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`
61+
const simulate = getInstanceFromNode(node).memoizedProps[propName]
62+
63+
if (init && init.target && init.target.hasOwnProperty('value')) {
64+
setNativeValue(node, init.target.value)
65+
}
66+
67+
return simulate({
68+
target: node,
69+
type: eventName,
70+
...init,
71+
})
72+
}
73+
5674
// function written after some investigation here:
5775
// https://github.com/facebook/react/issues/10135#issuecomment-401496776
5876
function setNativeValue(element, value) {
@@ -74,9 +92,7 @@ function setNativeValue(element, value) {
7492
// fallback to synthetic events for React events that the DOM doesn't support
7593
const syntheticEvents = ['select', 'mouseEnter', 'mouseLeave']
7694
syntheticEvents.forEach(eventName => {
77-
document.addEventListener(eventName.toLowerCase(), e => {
78-
Simulate[eventName](e.target, e)
79-
})
95+
fireEvent[eventName] = createReactEventSimulator(eventName)
8096
})
8197

8298
// just re-export everything from dom-testing-library

0 commit comments

Comments
 (0)