Skip to content

Commit

Permalink
[fixed] compatibility with unstable_handleError.
Browse files Browse the repository at this point in the history
in current React version componentWillUnmount could be called without calling componentDidMount
if errors during rendering are handled via unstable_handleError method
  • Loading branch information
mshustov authored and diasbruno committed Jun 13, 2017
1 parent bb15b16 commit 28ecc0b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ var Modal = createReactClass({
},

componentWillUnmount: function() {
if (!this.node) return;

refCount.remove(this);

if (this.props.ariaHideApp) {
Expand Down
35 changes: 34 additions & 1 deletion specs/Modal.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */
import sinon from 'sinon';
import expect from 'expect';
import React from 'react';
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import Modal from '../lib/components/Modal';
Expand Down Expand Up @@ -308,4 +308,37 @@ describe('State', () => {
done();
}, closeTimeoutMS);
});

it('shouldn\'t throw if forcibly unmounted during mounting', () => {
/* eslint-disable camelcase, react/prop-types */
class Wrapper extends Component {
constructor (props) {
super(props);
this.state = { error: false };
}
unstable_handleError () {
this.setState({ error: true });
}
render () {
return this.state.error ? null : <div>{ this.props.children }</div>;
}
}
/* eslint-enable camelcase, react/prop-types */

const Throw = () => { throw new Error('reason'); };
const TestCase = () => (
<Wrapper>
<Modal />
<Throw />
</Wrapper>
);

const currentDiv = document.createElement('div');
document.body.appendChild(currentDiv);

const mount = () => ReactDOM.render(<TestCase />, currentDiv);
expect(mount).toNotThrow();

document.body.removeChild(currentDiv);
});
});

0 comments on commit 28ecc0b

Please sign in to comment.