@@ -16,27 +16,28 @@ describe('ReactDOMInput', () => {
16
16
var ReactDOM ;
17
17
var ReactDOMServer ;
18
18
var ReactTestUtils ;
19
- var inputValueTracking ;
19
+ var setUntrackedValue ;
20
20
21
21
function normalizeCodeLocInfo ( str ) {
22
22
return str && str . replace ( / \( a t .+ ?: \d + \) / g, '(at **)' ) ;
23
23
}
24
24
25
- function setUntrackedValue ( elem , value ) {
26
- var tracker = inputValueTracking . _getTrackerFromNode ( elem ) ;
27
- var current = tracker . getValue ( ) ;
28
- elem . value = value ;
29
- tracker . setValue ( current ) ;
25
+ function dispatchEventOnNode ( node , type ) {
26
+ node . dispatchEvent ( new Event ( type , { bubbles : true , cancelable : true } ) ) ;
30
27
}
31
28
32
29
beforeEach ( ( ) => {
33
30
jest . resetModules ( ) ;
31
+
32
+ setUntrackedValue = Object . getOwnPropertyDescriptor (
33
+ HTMLInputElement . prototype ,
34
+ 'value' ,
35
+ ) . set ;
36
+
34
37
React = require ( 'react' ) ;
35
38
ReactDOM = require ( 'react-dom' ) ;
36
39
ReactDOMServer = require ( 'react-dom/server' ) ;
37
40
ReactTestUtils = require ( 'react-dom/test-utils' ) ;
38
- // TODO: can we express this test with only public API?
39
- inputValueTracking = require ( '../client/inputValueTracking' ) ;
40
41
spyOn ( console , 'error' ) ;
41
42
} ) ;
42
43
@@ -49,30 +50,18 @@ describe('ReactDOMInput', () => {
49
50
var node = ReactDOM . findDOMNode ( stub ) ;
50
51
expectDev ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
51
52
52
- // Simulate a native change event
53
- setUntrackedValue ( node , 'giraffe' ) ;
53
+ setUntrackedValue . call ( node , 'giraffe' ) ;
54
54
55
55
// This must use the native event dispatching. If we simulate, we will
56
56
// bypass the lazy event attachment system so we won't actually test this.
57
- var nativeEvent = document . createEvent ( 'Event' ) ;
58
- nativeEvent . initEvent ( 'change' , true , true ) ;
59
- node . dispatchEvent ( nativeEvent ) ;
57
+ dispatchEventOnNode ( node , 'change' ) ;
60
58
61
59
expect ( node . value ) . toBe ( 'lion' ) ;
62
60
63
61
document . body . removeChild ( container ) ;
64
62
} ) ;
65
63
66
64
it ( 'should control a value in reentrant events' , ( ) => {
67
- // This must use the native event dispatching. If we simulate, we will
68
- // bypass the lazy event attachment system so we won't actually test this.
69
- var inputEvent = document . createEvent ( 'Event' ) ;
70
- inputEvent . initEvent ( 'input' , true , true ) ;
71
- // This must use the native event dispatching. If we simulate, we will
72
- // bypass the lazy event attachment system so we won't actually test this.
73
- var changeEvent = document . createEvent ( 'Event' ) ;
74
- changeEvent . initEvent ( 'change' , true , true ) ;
75
-
76
65
class ControlledInputs extends React . Component {
77
66
state = { value : 'lion' } ;
78
67
a = null ;
@@ -82,8 +71,8 @@ describe('ReactDOMInput', () => {
82
71
this . setState ( { value : newValue } ) ;
83
72
// Calling focus here will blur the text box which causes a native
84
73
// change event. Ideally we shouldn't have to fire this ourselves.
85
- // I don 't know how to simulate a change event on a text box .
86
- this . a . dispatchEvent ( changeEvent ) ;
74
+ // Don 't remove unless you've verified the fix in #8240 is still covered .
75
+ dispatchEventOnNode ( this . a , 'change' ) ;
87
76
this . b . focus ( ) ;
88
77
}
89
78
blur ( currentValue ) {
@@ -114,11 +103,14 @@ describe('ReactDOMInput', () => {
114
103
// We need it to be in the body to test native event dispatching.
115
104
document . body . appendChild ( container ) ;
116
105
106
+ // Focus the field so we can later blur it.
107
+ // Don't remove unless you've verified the fix in #8240 is still covered.
117
108
instance . a . focus ( ) ;
118
- // Simulate a native keyup event
119
- setUntrackedValue ( instance . a , 'giraffe' ) ;
120
-
121
- instance . a . dispatchEvent ( inputEvent ) ;
109
+ setUntrackedValue . call ( instance . a , 'giraffe' ) ;
110
+ // This must use the native event dispatching. If we simulate, we will
111
+ // bypass the lazy event attachment system so we won't actually test this.
112
+ dispatchEventOnNode ( instance . a , 'change' ) ;
113
+ dispatchEventOnNode ( instance . a , 'blur' ) ;
122
114
123
115
expect ( instance . a . value ) . toBe ( 'giraffe' ) ;
124
116
expect ( instance . switchedFocus ) . toBe ( true ) ;
@@ -127,11 +119,6 @@ describe('ReactDOMInput', () => {
127
119
} ) ;
128
120
129
121
it ( 'should control values in reentrant events with different targets' , ( ) => {
130
- // This must use the native event dispatching. If we simulate, we will
131
- // bypass the lazy event attachment system so we won't actually test this.
132
- var inputEvent = document . createEvent ( 'Event' ) ;
133
- inputEvent . initEvent ( 'input' , true , true ) ;
134
-
135
122
class ControlledInputs extends React . Component {
136
123
state = { value : 'lion' } ;
137
124
a = null ;
@@ -164,11 +151,10 @@ describe('ReactDOMInput', () => {
164
151
// We need it to be in the body to test native event dispatching.
165
152
document . body . appendChild ( container ) ;
166
153
167
- // Simulate a native keyup event
168
- setUntrackedValue ( instance . a , 'giraffe' ) ;
169
- instance . a . dispatchEvent ( inputEvent ) ;
170
-
171
- // These should now both have been restored to their controlled value.
154
+ setUntrackedValue . call ( instance . a , 'giraffe' ) ;
155
+ // This must use the native event dispatching. If we simulate, we will
156
+ // bypass the lazy event attachment system so we won't actually test this.
157
+ dispatchEventOnNode ( instance . a , 'input' ) ;
172
158
173
159
expect ( instance . a . value ) . toBe ( 'lion' ) ;
174
160
expect ( instance . b . checked ) . toBe ( true ) ;
@@ -604,7 +590,7 @@ describe('ReactDOMInput', () => {
604
590
var container = document . createElement ( 'div' ) ;
605
591
var node = ReactDOM . render ( stub , container ) ;
606
592
607
- setUntrackedValue ( node , 'giraffe' ) ;
593
+ setUntrackedValue . call ( node , 'giraffe' ) ;
608
594
609
595
var fakeNativeEvent = function ( ) { } ;
610
596
fakeNativeEvent . target = node ;
0 commit comments