Skip to content

Commit

Permalink
Adds support for static getDerivedStateFromError(error). Should res…
Browse files Browse the repository at this point in the history
…olve preactjs#65. Includes 3 tests.
  • Loading branch information
johnhaitas committed Nov 2, 2018
1 parent e67a016 commit 64b1361
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ function renderToString(vnode, context, opts, inner, isSvgMode) {
rendered = c.render(c.props, c.state, c.context);
}
catch (error) {
if (c.componentDidCatch) c.componentDidCatch(error);
if (nodeName.getDerivedStateFromError) c.state = assign(assign({}, c.state), nodeName.getDerivedStateFromError(error));
else if (c.componentDidCatch) c.componentDidCatch(error);
else throw error;
}

Expand Down
76 changes: 76 additions & 0 deletions test/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,5 +672,81 @@ describe('render', () => {
expect(Test.prototype.render)
.to.throw();
});

it('should invoke getDerivedStateFromError from an error thrown in getDerivedStateFromProps', () => {
class Test extends Component {
static getDerivedStateFromProps() {
throw new Error();
}
static getDerivedStateFromError(error) {}
componentDidCatch(error) {}
}
spy(Test.prototype.constructor, 'getDerivedStateFromProps');
spy(Test.prototype.constructor, 'getDerivedStateFromError');
spy(Test.prototype, 'componentDidCatch');

render(<Test />);

expect(Test.prototype.constructor.getDerivedStateFromProps)
.to.have.been.calledOnce
.and.to.have.been.calledBefore(Test.prototype.constructor.getDerivedStateFromError);

expect(Test.prototype.componentDidCatch)
.to.not.have.been.called;

expect(Test.prototype.constructor.getDerivedStateFromProps)
.to.throw();
});

it('should invoke getDerivedStateFromError from an error thrown in componentWillMount', () => {
class Test extends Component {
static getDerivedStateFromError(error) {}
componentWillMount() {
throw new Error();
}
componentDidCatch(error) {}
}
spy(Test.prototype.constructor, 'getDerivedStateFromError');
spy(Test.prototype, 'componentWillMount');
spy(Test.prototype, 'componentDidCatch');

render(<Test />);

expect(Test.prototype.componentWillMount)
.to.have.been.calledOnce
.and.to.have.been.calledBefore(Test.prototype.constructor.getDerivedStateFromError);

expect(Test.prototype.componentDidCatch)
.to.not.have.been.called;

expect(Test.prototype.componentWillMount)
.to.throw();
});

it('should invoke getDerivedStateFromError from an error thrown in render', () => {
class Test extends Component {
static getDerivedStateFromError(error) {}
componentDidCatch(error) {}
render(props) {
throw new Error('Error in render() method');
return <div {...props} />; // eslint-disable-line
}
}
spy(Test.prototype.constructor, 'getDerivedStateFromError');
spy(Test.prototype, 'componentDidCatch');
spy(Test.prototype, 'render');

render(<Test />);

expect(Test.prototype.render)
.to.have.been.calledOnce
.and.to.have.been.calledBefore(Test.prototype.constructor.getDerivedStateFromError);

expect(Test.prototype.componentDidCatch)
.to.not.have.been.called;

expect(Test.prototype.render)
.to.throw();
});
});
});

0 comments on commit 64b1361

Please sign in to comment.