Skip to content

Commit

Permalink
Testing that the thrown error is passed into the componentDidCatch
Browse files Browse the repository at this point in the history
…method correctly
  • Loading branch information
johnhaitas committed Nov 3, 2018
1 parent 6acfb67 commit c7aba79
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions test/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,12 @@ describe('render', () => {
});

describe('Error Handling', () => {
it('should invoke ErrorBoundry\'s componentDidCatch from an error thrown in ComponentThatThrows\'s getDerivedStateFromProps', () => {
it('should invoke ErrorBoundry\'s componentDidCatch from an error thrown in ComponentThatThrows\' getDerivedStateFromProps', () => {
const ERROR_MESSAGE = 'getDerivedStateFromProps error',
THE_ERROR = new Error(ERROR_MESSAGE);
class ComponentThatThrows extends Component {
static getDerivedStateFromProps() {
throw new Error();
throw THE_ERROR;
}
componentDidCatch(error) {}
render(props) {
Expand Down Expand Up @@ -664,7 +666,7 @@ describe('render', () => {
// ComponentThatThrows
expect(ComponentThatThrows.prototype.constructor.getDerivedStateFromProps)
.to.have.been.calledOnce
.and.to.throw();
.and.to.throw(Error, ERROR_MESSAGE);

expect(ComponentThatThrows.prototype.componentDidCatch)
.to.not.have.been.called;
Expand All @@ -682,8 +684,9 @@ describe('render', () => {

// ErrorBoundry
expect(ErrorBoundry.prototype.componentDidCatch)
.to.have.been.calledOnce;

.to.have.been.calledOnce
.and.calledWithExactly(THE_ERROR);

expect(ErrorBoundry.prototype.render)
.to.have.been.calledTwice;

Expand All @@ -695,10 +698,12 @@ describe('render', () => {
.to.not.have.been.called;
});

it('should invoke ErrorBoundry\'s componentDidCatch from an error thrown in ComponentThatThrows\'s componentWillMount', () => {
it('should invoke ErrorBoundry\'s componentDidCatch from an error thrown in ComponentThatThrows\' componentWillMount', () => {
const ERROR_MESSAGE = 'componentWillMount error',
THE_ERROR = new Error(ERROR_MESSAGE);
class ComponentThatThrows extends Component {
componentWillMount() {
throw new Error();
throw THE_ERROR;
}
componentDidCatch(error) {}
render(props) {
Expand Down Expand Up @@ -750,7 +755,7 @@ describe('render', () => {

expect(ComponentThatThrows.prototype.componentWillMount)
.to.have.been.calledOnce
.and.to.throw();
.and.to.throw(Error, ERROR_MESSAGE);

expect(ComponentThatThrows.prototype.render)
.to.not.have.been.called;
Expand All @@ -765,7 +770,8 @@ describe('render', () => {

// ErrorBoundry
expect(ErrorBoundry.prototype.componentDidCatch)
.to.have.been.calledOnce;
.to.have.been.calledOnce
.and.calledWithExactly(THE_ERROR);

expect(ErrorBoundry.prototype.render)
.to.have.been.calledTwice;
Expand All @@ -778,11 +784,13 @@ describe('render', () => {
.to.not.have.been.called;
});

it('should invoke ErrorBoundry\'s componentDidCatch from an error thrown in ComponentThatThrows\'s render', () => {
it('should invoke ErrorBoundry\'s componentDidCatch from an error thrown in ComponentThatThrows\' render', () => {
const ERROR_MESSAGE = 'render error',
THE_ERROR = new Error(ERROR_MESSAGE);
class ComponentThatThrows extends Component {
componentDidCatch(error) {}
render(props) {
throw new Error();
throw THE_ERROR;
return <div {...props} />; // eslint-disable-line
}
}
Expand Down Expand Up @@ -830,7 +838,7 @@ describe('render', () => {

expect(ComponentThatThrows.prototype.render)
.to.have.been.calledOnce
.and.to.throw();
.and.to.throw(Error, ERROR_MESSAGE);

// ComponentThatRenders
expect(ComponentThatRenders.prototype.componentDidCatch)
Expand All @@ -842,7 +850,8 @@ describe('render', () => {

// ErrorBoundry
expect(ErrorBoundry.prototype.componentDidCatch)
.to.have.been.calledOnce;
.to.have.been.calledOnce
.and.calledWithExactly(THE_ERROR);

expect(ErrorBoundry.prototype.render)
.to.have.been.calledTwice;
Expand Down

0 comments on commit c7aba79

Please sign in to comment.