Skip to content

Commit

Permalink
[Docs] update simulateError with getDerivedStateFromError
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac authored and ljharb committed Mar 11, 2019
1 parent e76ea4f commit dfd1289
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 15 additions & 2 deletions docs/api/ReactWrapper/simulateError.md
Expand Up @@ -2,7 +2,7 @@

Simulate a component throwing an error as part of its rendering lifecycle.

This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` lifecycle method).
This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` and `static getDerivedStateFromError` lifecycle methods).


#### Arguments
Expand All @@ -26,16 +26,28 @@ function Something() {
}

class ErrorBoundary extends React.Component {
static getDerivedStateFromError(error) {
return {
hasError: true,
};
}

constructor(props) {
super(props);
this.state = { hasError: false };
}

componentDidCatch(error, info) {
const { spy } = this.props;
spy(error, info);
}

render() {
const { children } = this.props;
const { hasError } = this.state;
return (
<React.Fragment>
{children}
{hasError ? 'Error' : children}
</React.Fragment>
);
}
Expand All @@ -50,6 +62,7 @@ const wrapper = mount(<ErrorBoundary spy={spy}><Something /></ErrorBoundary>);
const error = new Error('hi!');
wrapper.find(Something).simulateError(error);

expect(wrapper.state()).to.have.property('hasError', true);
expect(spy).to.have.property('callCount', 1);
expect(spy.args).to.deep.equal([
error,
Expand Down
17 changes: 15 additions & 2 deletions docs/api/ShallowWrapper/simulateError.md
Expand Up @@ -2,7 +2,7 @@

Simulate a component throwing an error as part of its rendering lifecycle.

This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` lifecycle method).
This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` and `static getDerivedStateFromError` lifecycle methods).


#### Arguments
Expand All @@ -26,16 +26,28 @@ function Something() {
}

class ErrorBoundary extends React.Component {
static getDerivedStateFromError(error) {
return {
hasError: true,
};
}

constructor(props) {
super(props);
this.state = { hasError: false };
}

componentDidCatch(error, info) {
const { spy } = this.props;
spy(error, info);
}

render() {
const { children } = this.props;
const { hasError } = this.state;
return (
<React.Fragment>
{children}
{hasError ? 'Error' : children}
</React.Fragment>
);
}
Expand All @@ -50,6 +62,7 @@ const wrapper = shallow(<ErrorBoundary spy={spy}><Something /></ErrorBoundary>);
const error = new Error('hi!');
wrapper.find(Something).simulateError(error);

expect(wrapper.state()).to.have.property('hasError', true);
expect(spy).to.have.property('callCount', 1);
expect(spy.args).to.deep.equal([
error,
Expand Down

0 comments on commit dfd1289

Please sign in to comment.