1
1
import ReactDOM from 'react-dom'
2
- import { Simulate } from 'react-dom/test-utils'
3
2
import { getQueriesForElement , prettyDOM , fireEvent } from 'dom-testing-library'
4
3
5
4
const mountedContainers = new Set ( )
@@ -53,6 +52,25 @@ fireEvent.change = function reactChange(node, init) {
53
52
return originalChange ( node , init )
54
53
}
55
54
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
+
56
74
// function written after some investigation here:
57
75
// https://github.com/facebook/react/issues/10135#issuecomment-401496776
58
76
function setNativeValue ( element , value ) {
@@ -74,9 +92,7 @@ function setNativeValue(element, value) {
74
92
// fallback to synthetic events for React events that the DOM doesn't support
75
93
const syntheticEvents = [ 'select' , 'mouseEnter' , 'mouseLeave' ]
76
94
syntheticEvents . forEach ( eventName => {
77
- document . addEventListener ( eventName . toLowerCase ( ) , e => {
78
- Simulate [ eventName ] ( e . target , e )
79
- } )
95
+ fireEvent [ eventName ] = createReactEventSimulator ( eventName )
80
96
} )
81
97
82
98
// just re-export everything from dom-testing-library
0 commit comments