Unit tests for unmounted setState and isMounted
Sebastian Markbage committed
Jun 18, 2015 at 22:55 UTC
c1330dcfcd7f9bf9f850cbfd53b738bfbf8b13d6
2 files changed
+37
src/renderers/shared/reconciler/__tests__/ReactComponentLifeCycle-test.js
+26
@@ -257,6 +257,32 @@ describe('ReactComponentLifeCycle', function() {
257
);
258
});
259
260
+ it('should correctly determine if a null component is mounted', function() {
261
+ spyOn(console, 'error');
262
+ var Component = React.createClass({
263
+ componentWillMount: function() {
264
+ expect(this.isMounted()).toBeFalsy();
265
+ },
266
+ componentDidMount: function() {
267
+ expect(this.isMounted()).toBeTruthy();
268
+ },
269
+ render: function() {
270
+ expect(this.isMounted()).toBeFalsy();
271
+ return null;
272
+ },
273
+ });
274
+
275
+ var element = <Component />;
276
+
277
+ var instance = ReactTestUtils.renderIntoDocument(element);
278
+ expect(instance.isMounted()).toBeTruthy();
279
+
280
+ expect(console.error.argsForCall.length).toBe(1);
281
+ expect(console.error.argsForCall[0][0]).toContain(
282
+ 'Component is accessing isMounted inside its render()'
283
+ );
284
+ });
285
+
286
it('isMounted should return false when unmounted', function () {
287
var Component = React.createClass({
288
render: function() {
src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js
+11
@@ -286,11 +286,14 @@ describe('ReactCompositeComponent', function() {
286
var container = document.createElement('div');
287
document.body.appendChild(container);
288
289
+ var renders = 0;
290
+
291
var Component = React.createClass({
292
getInitialState: function() {
293
return {value: 0};
294
},
295
render: function() {
296
+ renders++;
297
return <div />;
298
},
299
});
@@ -299,12 +302,20 @@ describe('ReactCompositeComponent', function() {
302
expect(instance.setState).not.toBeDefined();
303
304
instance = React.render(instance, container);
305
+
306
+ expect(renders).toBe(1);
307
+
308
instance.setState({value: 1});
309
310
expect(console.error.calls.length).toBe(0);
311
312
+ expect(renders).toBe(2);
313
+
314
React.unmountComponentAtNode(container);
315
instance.setState({value: 2});
316
+
317
+ expect(renders).toBe(2);
318
+
319
expect(console.error.calls.length).toBe(1);
320
expect(console.error.argsForCall[0][0]).toBe(
321
'Warning: setState(...): Can only update a mounted or ' +