@@ -38,6 +38,7 @@ describe('ReactDOMInput', function() {
38
38
stub = ReactTestUtils . renderIntoDocument ( stub ) ;
39
39
var node = ReactDOM . findDOMNode ( stub ) ;
40
40
41
+ expect ( node . getAttribute ( 'value' ) ) . toBe ( '0' ) ;
41
42
expect ( node . value ) . toBe ( '0' ) ;
42
43
} ) ;
43
44
@@ -57,6 +58,30 @@ describe('ReactDOMInput', function() {
57
58
expect ( node . value ) . toBe ( 'false' ) ;
58
59
} ) ;
59
60
61
+ it ( 'should update `defaultValue` for uncontrolled input' , function ( ) {
62
+ var container = document . createElement ( 'div' ) ;
63
+
64
+ var node = ReactDOM . render ( < input type = "text" defaultValue = "0" /> , container ) ;
65
+
66
+ expect ( node . value ) . toBe ( '0' ) ;
67
+
68
+ ReactDOM . render ( < input type = "text" defaultValue = "1" /> , container ) ;
69
+
70
+ expect ( node . value ) . toBe ( '1' ) ;
71
+ } ) ;
72
+
73
+ it ( 'should take `defaultValue` when changing to uncontrolled input' , function ( ) {
74
+ var container = document . createElement ( 'div' ) ;
75
+
76
+ var node = ReactDOM . render ( < input type = "text" value = "0" readOnly = "true" /> , container ) ;
77
+
78
+ expect ( node . value ) . toBe ( '0' ) ;
79
+
80
+ ReactDOM . render ( < input type = "text" defaultValue = "1" /> , container ) ;
81
+
82
+ expect ( node . value ) . toBe ( '1' ) ;
83
+ } ) ;
84
+
60
85
it ( 'should display "foobar" for `defaultValue` of `objToString`' , function ( ) {
61
86
var objToString = {
62
87
toString : function ( ) {
@@ -82,8 +107,7 @@ describe('ReactDOMInput', function() {
82
107
it ( 'should allow setting `value` to `true`' , function ( ) {
83
108
var container = document . createElement ( 'div' ) ;
84
109
var stub = < input type = "text" value = "yolo" onChange = { emptyFunction } /> ;
85
- stub = ReactDOM . render ( stub , container ) ;
86
- var node = ReactDOM . findDOMNode ( stub ) ;
110
+ var node = ReactDOM . render ( stub , container ) ;
87
111
88
112
expect ( node . value ) . toBe ( 'yolo' ) ;
89
113
@@ -97,8 +121,7 @@ describe('ReactDOMInput', function() {
97
121
it ( 'should allow setting `value` to `false`' , function ( ) {
98
122
var container = document . createElement ( 'div' ) ;
99
123
var stub = < input type = "text" value = "yolo" onChange = { emptyFunction } /> ;
100
- stub = ReactDOM . render ( stub , container ) ;
101
- var node = ReactDOM . findDOMNode ( stub ) ;
124
+ var node = ReactDOM . render ( stub , container ) ;
102
125
103
126
expect ( node . value ) . toBe ( 'yolo' ) ;
104
127
@@ -112,8 +135,7 @@ describe('ReactDOMInput', function() {
112
135
it ( 'should allow setting `value` to `objToString`' , function ( ) {
113
136
var container = document . createElement ( 'div' ) ;
114
137
var stub = < input type = "text" value = "foo" onChange = { emptyFunction } /> ;
115
- stub = ReactDOM . render ( stub , container ) ;
116
- var node = ReactDOM . findDOMNode ( stub ) ;
138
+ var node = ReactDOM . render ( stub , container ) ;
117
139
118
140
expect ( node . value ) . toBe ( 'foo' ) ;
119
141
@@ -129,6 +151,29 @@ describe('ReactDOMInput', function() {
129
151
expect ( node . value ) . toEqual ( 'foobar' ) ;
130
152
} ) ;
131
153
154
+ it ( 'should not incur unnecessary DOM mutations' , function ( ) {
155
+ var container = document . createElement ( 'div' ) ;
156
+ ReactDOM . render ( < input value = "a" /> , container ) ;
157
+
158
+ var node = container . firstChild ;
159
+ var nodeValue = 'a' ; // node.value always returns undefined
160
+ var nodeValueSetter = jest . genMockFn ( ) ;
161
+ Object . defineProperty ( node , 'value' , {
162
+ get : function ( ) {
163
+ return nodeValue ;
164
+ } ,
165
+ set : nodeValueSetter . mockImplementation ( function ( newValue ) {
166
+ nodeValue = newValue ;
167
+ } ) ,
168
+ } ) ;
169
+
170
+ ReactDOM . render ( < input value = "a" /> , container ) ;
171
+ expect ( nodeValueSetter . mock . calls . length ) . toBe ( 0 ) ;
172
+
173
+ ReactDOM . render ( < input value = "b" /> , container ) ;
174
+ expect ( nodeValueSetter . mock . calls . length ) . toBe ( 1 ) ;
175
+ } ) ;
176
+
132
177
it ( 'should properly control a value of number `0`' , function ( ) {
133
178
var stub = < input type = "text" value = { 0 } onChange = { emptyFunction } /> ;
134
179
stub = ReactTestUtils . renderIntoDocument ( stub ) ;
0 commit comments