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

Fix simulateError() on Memo component #2525

Merged
merged 1 commit into from
Jun 16, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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