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
@@ -45,8 +45,34 @@ function cleanupAtContainer(container) {
45
45
mountedContainers . delete ( container )
46
46
}
47
47
48
+ const originalChange = fireEvent . change
49
+ fireEvent . change = function reactChange ( node , init ) {
50
+ if ( init && init . target && init . target . hasOwnProperty ( 'value' ) ) {
51
+ setNativeValue ( node , init . target . value )
52
+ }
53
+ return originalChange ( node , init )
54
+ }
55
+
56
+ // function written after some investigation here:
57
+ // https://github.com/facebook/react/issues/10135#issuecomment-401496776
58
+ function setNativeValue ( element , value ) {
59
+ const { set : valueSetter } =
60
+ Object . getOwnPropertyDescriptor ( element , 'value' ) || { }
61
+ const prototype = Object . getPrototypeOf ( element )
62
+ const { set : prototypeValueSetter } =
63
+ Object . getOwnPropertyDescriptor ( prototype , 'value' ) || { }
64
+
65
+ if ( prototypeValueSetter && valueSetter !== prototypeValueSetter ) {
66
+ prototypeValueSetter . call ( element , value )
67
+ } else if ( valueSetter ) {
68
+ valueSetter . call ( element , value )
69
+ } else {
70
+ throw new Error ( 'The given element does not have a value setter' )
71
+ }
72
+ }
73
+
48
74
// fallback to synthetic events for React events that the DOM doesn't support
49
- const syntheticEvents = [ 'change' , ' select', 'mouseEnter' , 'mouseLeave' ]
75
+ const syntheticEvents = [ 'select' , 'mouseEnter' , 'mouseLeave' ]
50
76
syntheticEvents . forEach ( eventName => {
51
77
document . addEventListener ( eventName . toLowerCase ( ) , e => {
52
78
Simulate [ eventName ] ( e . target , e )
@@ -56,3 +82,5 @@ syntheticEvents.forEach(eventName => {
56
82
// just re-export everything from dom-testing-library
57
83
export * from 'dom-testing-library'
58
84
export { render , cleanup }
85
+
86
+ /* eslint complexity:0, func-name-matching:0 */
0 commit comments