12
12
describe ( 'ReactDOMComponentTree' , ( ) => {
13
13
let React ;
14
14
let ReactDOM ;
15
- let AnotherReactDOM ;
16
15
let ReactDOMServer ;
17
16
18
17
function renderMarkupIntoDocument ( elt ) {
@@ -22,10 +21,6 @@ describe('ReactDOMComponentTree', () => {
22
21
return ReactDOM . hydrate ( elt , container ) ;
23
22
}
24
23
25
- function getTypeOf ( instance ) {
26
- return instance . type ;
27
- }
28
-
29
24
function simulateInput ( elem , value ) {
30
25
const inputEvent = new Event ( 'input' , {
31
26
bubbles : true ,
@@ -112,9 +107,9 @@ describe('ReactDOMComponentTree', () => {
112
107
simulateClick ( document . getElementById ( elemID ) ) ;
113
108
} ) ;
114
109
115
- it ( 'finds instances for nodes when events happen ' , done => {
110
+ it ( 'finds a controlled instance from node and gets its current fiber props ' , done => {
116
111
const inputID = 'inputID' ;
117
- const startValue = 'start' ;
112
+ const startValue = undefined ;
118
113
const finishValue = 'finish' ;
119
114
120
115
class Controlled extends React . Component {
@@ -124,19 +119,35 @@ describe('ReactDOMComponentTree', () => {
124
119
const node = e . currentTarget ;
125
120
expect ( node . value ) . toEqual ( finishValue ) ;
126
121
expect ( node . id ) . toBe ( inputID ) ;
127
- done ( ) ;
122
+ spyOn ( console , 'error' ) ;
123
+ expectDev ( console . error . calls . count ( ) ) . toBe ( 0 ) ;
124
+ this . setState (
125
+ {
126
+ value : node . value ,
127
+ } ,
128
+ ( ) => {
129
+ expectDev ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
130
+ expectDev ( console . error . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
131
+ 'Warning: A component is changing an uncontrolled input of ' +
132
+ 'type text to be controlled. Input elements should not ' +
133
+ 'switch from uncontrolled to controlled (or vice versa). ' +
134
+ 'Decide between using a controlled or uncontrolled input ' +
135
+ 'element for the lifetime of the component. More info: ' +
136
+ 'https://fb.me/react-controlled-components' ,
137
+ ) ;
138
+ done ( ) ;
139
+ } ,
140
+ ) ;
128
141
} ;
129
142
render ( ) {
130
143
return (
131
- < div >
132
- < input
133
- id = { inputID }
134
- type = "text"
135
- ref = { n => ( this . a = n ) }
136
- value = { this . state . value }
137
- onChange = { this . _onChange }
138
- />
139
- </ div >
144
+ < input
145
+ id = { inputID }
146
+ type = "text"
147
+ ref = { n => ( this . a = n ) }
148
+ value = { this . state . value }
149
+ onChange = { this . _onChange }
150
+ />
140
151
) ;
141
152
}
142
153
}
@@ -148,28 +159,34 @@ describe('ReactDOMComponentTree', () => {
148
159
simulateInput ( instance . a , finishValue ) ;
149
160
} ) ;
150
161
151
- it ( 'finds instance of node being unmounted' , ( ) => {
162
+ it ( 'finds instance of node that is attempted to be unmounted' , ( ) => {
152
163
spyOn ( console , 'error' ) ;
153
- const component = < div > </ div > ;
164
+ const component = < div / >;
154
165
const container = document . createElement ( 'div' ) ;
155
- const instance = ReactDOM . render ( component , container ) ;
156
- ReactDOM . unmountComponentAtNode ( container ) ;
157
- expectDev ( console . error . calls . count ( ) ) . toBe ( 0 ) ;
166
+ const node = ReactDOM . render ( < div > { component } </ div > , container ) ;
167
+ ReactDOM . unmountComponentAtNode ( node ) ;
168
+ expectDev ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
169
+ expectDev ( console . error . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
170
+ "unmountComponentAtNode(): The node you're attempting to unmount " +
171
+ 'was rendered by React and is not a top-level container. You may ' +
172
+ 'have accidentally passed in a React root node instead of its ' +
173
+ 'container.' ,
174
+ ) ;
158
175
} ) ;
159
176
160
177
it ( 'finds instance from node to stop rendering over other react rendered components' , ( ) => {
161
178
spyOn ( console , 'error' ) ;
162
179
const component = < div > < span > Hello</ span > </ div > ;
163
- const anotherComponent = < div > </ div > ;
180
+ const anotherComponent = < div / >;
164
181
const container = document . createElement ( 'div' ) ;
165
182
const instance = ReactDOM . render ( component , container ) ;
166
183
ReactDOM . render ( anotherComponent , instance ) ;
167
184
expectDev ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
168
185
expectDev ( console . error . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
169
186
'render(...): Replacing React-rendered children with a new root ' +
170
- 'component. If you intended to update the children of this node, ' +
171
- 'you should instead have the existing children update their state ' +
172
- 'and render the new components instead of calling ReactDOM.render.'
187
+ 'component. If you intended to update the children of this node, ' +
188
+ 'you should instead have the existing children update their state ' +
189
+ 'and render the new components instead of calling ReactDOM.render.' ,
173
190
) ;
174
191
} ) ;
175
192
} ) ;
0 commit comments