Skip to content

Commit

Permalink
Testing that the thrown error is passed into the `getDerivedStateFrom…
Browse files Browse the repository at this point in the history
…Error` method correctly
  • Loading branch information
johnhaitas committed Nov 3, 2018
1 parent 73d64a6 commit 03971f9
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions test/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,11 @@ describe('render', () => {
});

it('should invoke ErrorBoundry\'s getDerivedStateFromError 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;
}
static getDerivedStateFromError(error) {}
componentDidCatch(error) {}
Expand Down Expand Up @@ -926,7 +928,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.constructor.getDerivedStateFromError)
.to.not.have.been.called;
Expand All @@ -950,7 +952,8 @@ describe('render', () => {

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

expect(ErrorBoundry.prototype.componentDidCatch)
.to.not.have.been.called;
Expand All @@ -970,10 +973,12 @@ describe('render', () => {
});

it('should invoke ErrorBoundry\'s getDerivedStateFromError from an error thrown in ComponentThatThrows\' componentWillMount', () => {
const ERROR_MESSAGE = 'componentWillMount error',
THE_ERROR = new Error(ERROR_MESSAGE);
class ComponentThatThrows extends Component {
static getDerivedStateFromError(error) {}
componentWillMount() {
throw new Error();
throw THE_ERROR;
}
componentDidCatch(error) {}
render(props) {
Expand Down Expand Up @@ -1037,7 +1042,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 @@ -1055,7 +1060,8 @@ describe('render', () => {

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

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

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

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

// ComponentThatRenders
expect(ComponentThatRenders.prototype.constructor.getDerivedStateFromError)
Expand All @@ -1154,7 +1162,8 @@ describe('render', () => {

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

expect(ErrorBoundry.prototype.componentDidCatch)
.to.not.have.been.called;
Expand Down

0 comments on commit 03971f9

Please sign in to comment.