Skip to content

Commit

Permalink
[enzyme-adapter-react-16] [fix] fix simulateError() on Memo component
Browse files Browse the repository at this point in the history
  • Loading branch information
henryqdineen authored and ljharb committed Jun 15, 2021
1 parent f046079 commit 8c55734
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
rootNode,
nodeHierarchy,
nodeTypeFromType,
adapter.displayNameOfNode,
adapter.displayNameOfNode.bind(adapter),
is166 ? catchingType : undefined,
);
},
Expand Down Expand Up @@ -787,7 +787,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
cachedNode,
nodeHierarchy.concat(cachedNode),
nodeTypeFromType,
adapter.displayNameOfNode,
adapter.displayNameOfNode.bind(adapter),
is166 ? cachedNode.type : undefined,
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default function describeCDC({
}

render() {
const { ThrowerComponent } = this.props;
const {
didThrow,
throws,
Expand All @@ -62,7 +63,7 @@ export default function describeCDC({
<div>
<MaybeFragment>
<span>
<Thrower throws={throws} />
{<ThrowerComponent throws={throws} />}
<div>
{didThrow ? 'HasThrown' : 'HasNotThrown'}
</div>
Expand All @@ -73,6 +74,10 @@ export default function describeCDC({
}
}

ErrorBoundary.defaultProps = {
ThrowerComponent: Thrower,
};

function ErrorSFC(props) {
return <ErrorBoundary {...props} />;
}
Expand Down Expand Up @@ -121,6 +126,39 @@ export default function describeCDC({
});
});

itIf(
is('>= 16.6'),
'catches a simulated error on memo() component',
() => {
const MemoThrower = React.memo(Thrower);
const spy = sinon.spy();
const wrapper = Wrap(
<ErrorBoundary spy={spy} ThrowerComponent={MemoThrower} />,
);

expect(spy).to.have.property('callCount', 0);

expect(() => wrapper.find(Thrower).simulateError(errorToThrow)).not.to.throw();

expect(spy).to.have.property('callCount', 1);

expect(spy.args).to.be.an('array').and.have.lengthOf(1);
const [[actualError, info]] = spy.args;
expect(() => {
throw actualError;
}).to.throw(errorToThrow);
expect(info).to.deep.equal({
componentStack: `
in Memo(Thrower) (created by ErrorBoundary)
in span (created by ErrorBoundary)${hasFragments ? '' : `
in main (created by ErrorBoundary)`}
in div (created by ErrorBoundary)
in ErrorBoundary (created by WrapperComponent)
in WrapperComponent`,
});
},
);

it('rerenders on a simulated error', () => {
const wrapper = Wrap(<ErrorBoundary spy={sinon.stub()} />);

Expand Down

0 comments on commit 8c55734

Please sign in to comment.