@@ -225,6 +225,58 @@ describe('ReactCompositeComponent', () => {
225
225
expect ( inputProps . prop ) . not . toBeDefined ( ) ;
226
226
} ) ;
227
227
228
+ it ( 'should warn about `forceUpdate` on not-yet-mounted components' , ( ) => {
229
+ class MyComponent extends React . Component {
230
+ constructor ( props ) {
231
+ super ( props ) ;
232
+ this . forceUpdate ( ) ;
233
+ }
234
+ render ( ) {
235
+ return < div /> ;
236
+ }
237
+ }
238
+
239
+ const container = document . createElement ( 'div' ) ;
240
+ expect ( ( ) => ReactDOM . render ( < MyComponent /> , container ) ) . toWarnDev (
241
+ "Warning: Can't call forceUpdate on a component that is not yet mounted. " +
242
+ 'This is a no-op, but it might indicate a bug in your application.\n\n' +
243
+ 'To fix, assign the initial state in the MyComponent constructor. ' +
244
+ 'If the state needs to reflect an external data source, ' +
245
+ 'you may also add a componentDidMount lifecycle hook to MyComponent ' +
246
+ 'and call setState there if the external data has changed.' ,
247
+ ) ;
248
+
249
+ // No additional warning should be recorded
250
+ const container2 = document . createElement ( 'div' ) ;
251
+ ReactDOM . render ( < MyComponent /> , container2 ) ;
252
+ } ) ;
253
+
254
+ it ( 'should warn about `setState` on not-yet-mounted components' , ( ) => {
255
+ class MyComponent extends React . Component {
256
+ constructor ( props ) {
257
+ super ( props ) ;
258
+ this . setState ( ) ;
259
+ }
260
+ render ( ) {
261
+ return < div /> ;
262
+ }
263
+ }
264
+
265
+ const container = document . createElement ( 'div' ) ;
266
+ expect ( ( ) => ReactDOM . render ( < MyComponent /> , container ) ) . toWarnDev (
267
+ "Warning: Can't call setState on a component that is not yet mounted. " +
268
+ 'This is a no-op, but it might indicate a bug in your application.\n\n' +
269
+ 'To fix, assign the initial state in the MyComponent constructor. ' +
270
+ 'If the state needs to reflect an external data source, ' +
271
+ 'you may also add a componentDidMount lifecycle hook to MyComponent ' +
272
+ 'and call setState there if the external data has changed.' ,
273
+ ) ;
274
+
275
+ // No additional warning should be recorded
276
+ const container2 = document . createElement ( 'div' ) ;
277
+ ReactDOM . render ( < MyComponent /> , container2 ) ;
278
+ } ) ;
279
+
228
280
it ( 'should warn about `forceUpdate` on unmounted components' , ( ) => {
229
281
const container = document . createElement ( 'div' ) ;
230
282
document . body . appendChild ( container ) ;
0 commit comments