Skip to content

Commit

Permalink
[enzyme-adapter-react-16] [New] add getDerivedStateFromError support
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac authored and ljharb committed Mar 13, 2019
1 parent ebdcce7 commit e76ea4f
Show file tree
Hide file tree
Showing 4 changed files with 760 additions and 14 deletions.
16 changes: 14 additions & 2 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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) {
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

0 comments on commit e76ea4f

Please sign in to comment.