@@ -47,6 +47,7 @@ describe('ReactDOMInput', function() {
47
47
stub = ReactTestUtils . renderIntoDocument ( stub ) ;
48
48
var node = ReactDOM . findDOMNode ( stub ) ;
49
49
50
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( '0' ) ;
50
51
expect ( node . value ) . toBe ( '0' ) ;
51
52
} ) ;
52
53
@@ -66,6 +67,30 @@ describe('ReactDOMInput', function() {
66
67
expect ( node . value ) . toBe ( 'false' ) ;
67
68
} ) ;
68
69
70
+ it ( 'should update `defaultValue` for uncontrolled input' , function ( ) {
71
+ var container = document . createElement ( 'div' ) ;
72
+
73
+ var node = ReactDOM . render ( < input type = "text" defaultValue = "0" /> , container ) ;
74
+
75
+ expect ( node . value ) . toBe ( '0' ) ;
76
+
77
+ ReactDOM . render ( < input type = "text" defaultValue = "1" /> , container ) ;
78
+
79
+ expect ( node . value ) . toBe ( '1' ) ;
80
+ } ) ;
81
+
82
+ it ( 'should take `defaultValue` when changing to uncontrolled input' , function ( ) {
83
+ var container = document . createElement ( 'div' ) ;
84
+
85
+ var node = ReactDOM . render ( < input type = "text" value = "0" readOnly = "true" /> , container ) ;
86
+
87
+ expect ( node . value ) . toBe ( '0' ) ;
88
+
89
+ ReactDOM . render ( < input type = "text" defaultValue = "1" /> , container ) ;
90
+
91
+ expect ( node . value ) . toBe ( '1' ) ;
92
+ } ) ;
93
+
69
94
it ( 'should display "foobar" for `defaultValue` of `objToString`' , function ( ) {
70
95
var objToString = {
71
96
toString : function ( ) {
@@ -91,8 +116,7 @@ describe('ReactDOMInput', function() {
91
116
it ( 'should allow setting `value` to `true`' , function ( ) {
92
117
var container = document . createElement ( 'div' ) ;
93
118
var stub = < input type = "text" value = "yolo" onChange = { emptyFunction } /> ;
94
- stub = ReactDOM . render ( stub , container ) ;
95
- var node = ReactDOM . findDOMNode ( stub ) ;
119
+ var node = ReactDOM . render ( stub , container ) ;
96
120
97
121
expect ( node . value ) . toBe ( 'yolo' ) ;
98
122
@@ -106,8 +130,7 @@ describe('ReactDOMInput', function() {
106
130
it ( 'should allow setting `value` to `false`' , function ( ) {
107
131
var container = document . createElement ( 'div' ) ;
108
132
var stub = < input type = "text" value = "yolo" onChange = { emptyFunction } /> ;
109
- stub = ReactDOM . render ( stub , container ) ;
110
- var node = ReactDOM . findDOMNode ( stub ) ;
133
+ var node = ReactDOM . render ( stub , container ) ;
111
134
112
135
expect ( node . value ) . toBe ( 'yolo' ) ;
113
136
@@ -121,8 +144,7 @@ describe('ReactDOMInput', function() {
121
144
it ( 'should allow setting `value` to `objToString`' , function ( ) {
122
145
var container = document . createElement ( 'div' ) ;
123
146
var stub = < input type = "text" value = "foo" onChange = { emptyFunction } /> ;
124
- stub = ReactDOM . render ( stub , container ) ;
125
- var node = ReactDOM . findDOMNode ( stub ) ;
147
+ var node = ReactDOM . render ( stub , container ) ;
126
148
127
149
expect ( node . value ) . toBe ( 'foo' ) ;
128
150
@@ -138,6 +160,29 @@ describe('ReactDOMInput', function() {
138
160
expect ( node . value ) . toEqual ( 'foobar' ) ;
139
161
} ) ;
140
162
163
+ it ( 'should not incur unnecessary DOM mutations' , function ( ) {
164
+ var container = document . createElement ( 'div' ) ;
165
+ ReactDOM . render ( < input value = "a" /> , container ) ;
166
+
167
+ var node = container . firstChild ;
168
+ var nodeValue = 'a' ; // node.value always returns undefined
169
+ var nodeValueSetter = jest . genMockFn ( ) ;
170
+ Object . defineProperty ( node , 'value' , {
171
+ get : function ( ) {
172
+ return nodeValue ;
173
+ } ,
174
+ set : nodeValueSetter . mockImplementation ( function ( newValue ) {
175
+ nodeValue = newValue ;
176
+ } ) ,
177
+ } ) ;
178
+
179
+ ReactDOM . render ( < input value = "a" /> , container ) ;
180
+ expect ( nodeValueSetter . mock . calls . length ) . toBe ( 0 ) ;
181
+
182
+ ReactDOM . render ( < input value = "b" /> , container ) ;
183
+ expect ( nodeValueSetter . mock . calls . length ) . toBe ( 1 ) ;
184
+ } ) ;
185
+
141
186
it ( 'should properly control a value of number `0`' , function ( ) {
142
187
var stub = < input type = "text" value = { 0 } onChange = { emptyFunction } /> ;
143
188
stub = ReactTestUtils . renderIntoDocument ( stub ) ;
0 commit comments