Skip to content

Commit ea6d40c

Browse files
committed
Quick solution for #206
1 parent afeaa14 commit ea6d40c

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()
@@ -67,6 +66,25 @@ fireEvent.change = function reactChange(node, init) {
6766
return originalChange(node, init)
6867
}
6968

69+
// probably there is a better way to get access to a component instance
70+
const [
71+
getInstanceFromNode,
72+
] = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Events
73+
const createReactEventSimulator = eventName => (node, init) => {
74+
const propName = `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`
75+
const simulate = getInstanceFromNode(node).memoizedProps[propName]
76+
77+
if (init && init.target && init.target.hasOwnProperty('value')) {
78+
setNativeValue(node, init.target.value)
79+
}
80+
81+
return simulate({
82+
target: node,
83+
type: eventName,
84+
...init,
85+
})
86+
}
87+
7088
// function written after some investigation here:
7189
// https://github.com/facebook/react/issues/10135#issuecomment-401496776
7290
function setNativeValue(element, value) {
@@ -88,9 +106,7 @@ function setNativeValue(element, value) {
88106
// fallback to synthetic events for React events that the DOM doesn't support
89107
const syntheticEvents = ['select', 'mouseEnter', 'mouseLeave']
90108
syntheticEvents.forEach(eventName => {
91-
document.addEventListener(eventName.toLowerCase(), e => {
92-
Simulate[eventName](e.target, e)
93-
})
109+
fireEvent[eventName] = createReactEventSimulator(eventName)
94110
})
95111

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

0 commit comments

Comments
 (0)