1
1
import ReactDOM from 'react-dom'
2
2
import { Simulate } from 'react-dom/test-utils'
3
- import { getQueriesForElement , prettyDOM } from 'dom-testing-library'
3
+ import { getQueriesForElement , prettyDOM , fireEvent } from 'dom-testing-library'
4
4
5
5
const mountedContainers = new Set ( )
6
6
@@ -59,6 +59,32 @@ function cleanupAtContainer(container) {
59
59
mountedContainers . delete ( container )
60
60
}
61
61
62
+ const originalChange = fireEvent . change
63
+ fireEvent . change = function reactChange ( node , init ) {
64
+ if ( init && init . target && init . target . hasOwnProperty ( 'value' ) ) {
65
+ setNativeValue ( node , init . target . value )
66
+ }
67
+ return originalChange ( node , init )
68
+ }
69
+
70
+ // function written after some investigation here:
71
+ // https://github.com/facebook/react/issues/10135#issuecomment-401496776
72
+ function setNativeValue ( element , value ) {
73
+ const { set : valueSetter } =
74
+ Object . getOwnPropertyDescriptor ( element , 'value' ) || { }
75
+ const prototype = Object . getPrototypeOf ( element )
76
+ const { set : prototypeValueSetter } =
77
+ Object . getOwnPropertyDescriptor ( prototype , 'value' ) || { }
78
+
79
+ if ( prototypeValueSetter && valueSetter !== prototypeValueSetter ) {
80
+ prototypeValueSetter . call ( element , value )
81
+ } else if ( valueSetter ) {
82
+ valueSetter . call ( element , value )
83
+ } else {
84
+ throw new Error ( 'The given element does not have a value setter' )
85
+ }
86
+ }
87
+
62
88
// fallback to synthetic events for React events that the DOM doesn't support
63
89
const syntheticEvents = [ 'select' , 'mouseEnter' , 'mouseLeave' ]
64
90
syntheticEvents . forEach ( eventName => {
@@ -70,3 +96,5 @@ syntheticEvents.forEach(eventName => {
70
96
// just re-export everything from dom-testing-library
71
97
export * from 'dom-testing-library'
72
98
export { render , cleanup }
99
+
100
+ /* eslint complexity:0, func-name-matching:0 */
0 commit comments