mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Unit tests for unmounted setState and isMounted
This commit is contained in:
@@ -257,6 +257,32 @@ describe('ReactComponentLifeCycle', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('should correctly determine if a null component is mounted', function() {
|
||||
spyOn(console, 'error');
|
||||
var Component = React.createClass({
|
||||
componentWillMount: function() {
|
||||
expect(this.isMounted()).toBeFalsy();
|
||||
},
|
||||
componentDidMount: function() {
|
||||
expect(this.isMounted()).toBeTruthy();
|
||||
},
|
||||
render: function() {
|
||||
expect(this.isMounted()).toBeFalsy();
|
||||
return null;
|
||||
},
|
||||
});
|
||||
|
||||
var element = <Component />;
|
||||
|
||||
var instance = ReactTestUtils.renderIntoDocument(element);
|
||||
expect(instance.isMounted()).toBeTruthy();
|
||||
|
||||
expect(console.error.argsForCall.length).toBe(1);
|
||||
expect(console.error.argsForCall[0][0]).toContain(
|
||||
'Component is accessing isMounted inside its render()'
|
||||
);
|
||||
});
|
||||
|
||||
it('isMounted should return false when unmounted', function () {
|
||||
var Component = React.createClass({
|
||||
render: function() {
|
||||
|
||||
@@ -286,11 +286,14 @@ describe('ReactCompositeComponent', function() {
|
||||
var container = document.createElement('div');
|
||||
document.body.appendChild(container);
|
||||
|
||||
var renders = 0;
|
||||
|
||||
var Component = React.createClass({
|
||||
getInitialState: function() {
|
||||
return {value: 0};
|
||||
},
|
||||
render: function() {
|
||||
renders++;
|
||||
return <div />;
|
||||
},
|
||||
});
|
||||
@@ -299,12 +302,20 @@ describe('ReactCompositeComponent', function() {
|
||||
expect(instance.setState).not.toBeDefined();
|
||||
|
||||
instance = React.render(instance, container);
|
||||
|
||||
expect(renders).toBe(1);
|
||||
|
||||
instance.setState({value: 1});
|
||||
|
||||
expect(console.error.calls.length).toBe(0);
|
||||
|
||||
expect(renders).toBe(2);
|
||||
|
||||
React.unmountComponentAtNode(container);
|
||||
instance.setState({value: 2});
|
||||
|
||||
expect(renders).toBe(2);
|
||||
|
||||
expect(console.error.calls.length).toBe(1);
|
||||
expect(console.error.argsForCall[0][0]).toBe(
|
||||
'Warning: setState(...): Can only update a mounted or ' +
|
||||
|
||||
Reference in New Issue
Block a user