Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support getDerivedStateFromError #2036

Merged
merged 3 commits into from Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
barmac marked this conversation as resolved.
Show resolved Hide resolved
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;
barmac marked this conversation as resolved.
Show resolved Hide resolved
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
9 changes: 5 additions & 4 deletions env.js
Expand Up @@ -126,10 +126,11 @@ Promise.resolve()
const peerDeps = adapterJson.peerDependencies;
const installs = Object.keys(peerDeps)
.filter(key => !key.startsWith('enzyme'))
.map((key) => {
const peerVersion = (key === 'react-test-renderer' && process.env.RENDERER) ? process.env.RENDERER : peerDeps[key];
return `${key}@${key.startsWith('react') ? reactVersion : peerVersion}`;
});
.map(key => `${key}@${key.startsWith('react') ? reactVersion : peerDeps[key]}`);

if (process.env.RENDERER) {
installs.push(`react-test-renderer@${process.env.RENDERER}`);
}

// eslint-disable-next-line no-param-reassign
testJson.dependencies[adapterName] = adapterJson.version;
Expand Down
16 changes: 14 additions & 2 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Expand Up @@ -316,6 +316,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
getChildContext: {
calledByRenderer: false,
},
getDerivedStateFromError: is166,
},
};
}
Expand Down Expand Up @@ -362,8 +363,17 @@ class ReactSixteenAdapter extends EnzymeAdapter {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateError(nodeHierarchy, rootNode, error) {
const { instance: catchingInstance } = nodeHierarchy
.find(x => x.instance && x.instance.componentDidCatch) || {};
const isErrorBoundary = ({ instance: elInstance, type }) => {
if (is166 && type && type.getDerivedStateFromError) {
return true;
}
return elInstance && elInstance.componentDidCatch;
};

const {
instance: catchingInstance,
type: catchingType,
} = nodeHierarchy.find(isErrorBoundary) || {};

simulateError(
error,
Expand All @@ -372,6 +382,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
nodeHierarchy,
nodeTypeFromType,
adapter.displayNameOfNode,
is166 ? catchingType : undefined,
);
},
simulateEvent(node, event, mock) {
Expand Down Expand Up @@ -482,6 +493,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
nodeHierarchy.concat(cachedNode),
nodeTypeFromType,
adapter.displayNameOfNode,
is166 ? cachedNode.type : undefined,
);
},
simulateEvent(node, event, ...args) {
Expand Down
24 changes: 16 additions & 8 deletions packages/enzyme-adapter-utils/src/Utils.js
Expand Up @@ -266,19 +266,27 @@ export function simulateError(
hierarchy,
getNodeType = nodeTypeFromType,
getDisplayName = displayNameOfNode,
catchingType = {},
) {
const { componentDidCatch } = catchingInstance || {};
if (!componentDidCatch) {
const instance = catchingInstance || {};

const { componentDidCatch } = instance;

const { getDerivedStateFromError } = catchingType;

if (!componentDidCatch && !getDerivedStateFromError) {
ljharb marked this conversation as resolved.
Show resolved Hide resolved
throw error;
}

const componentStack = getComponentStack(
hierarchy,
getNodeType,
getDisplayName,
);
if (getDerivedStateFromError) {
const stateUpdate = getDerivedStateFromError.call(catchingType, error);
instance.setState(stateUpdate);
}

componentDidCatch.call(catchingInstance, error, { componentStack });
if (componentDidCatch) {
const componentStack = getComponentStack(hierarchy, getNodeType, getDisplayName);
componentDidCatch.call(instance, error, { componentStack });
}
}

export function getMaskedContext(contextTypes, unmaskedContext) {
Expand Down