Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Fix simulateError() on Memo component
Browse files Browse the repository at this point in the history
From enzymejs/enzyme#2525

Co-Authored-By: Henry Q. Dineen <682132+henryqdineen@users.noreply.github.com>
  • Loading branch information
wojtekmaj and henryqdineen committed Nov 4, 2022
1 parent 9067f76 commit 146158d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ReactSeventeenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class ReactSeventeenAdapter extends EnzymeAdapter {
rootNode,
nodeHierarchy,
nodeTypeFromType,
adapter.displayNameOfNode,
adapter.displayNameOfNode.bind(adapter),
catchingType,
);
},
Expand Down Expand Up @@ -708,7 +708,7 @@ class ReactSeventeenAdapter extends EnzymeAdapter {
cachedNode,
nodeHierarchy.concat(cachedNode),
nodeTypeFromType,
adapter.displayNameOfNode,
adapter.displayNameOfNode.bind(adapter),
cachedNode.type,
);
},
Expand Down
38 changes: 37 additions & 1 deletion test/test/shared/lifecycles/componentDidCatch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ export default function describeCDC({ Wrap, isShallow }) {
}

render() {
const { ThrowerComponent } = this.props;
const { didThrow, throws } = this.state;
return (
<div>
<MaybeFragment>
<span>
<Thrower throws={throws} />
<ThrowerComponent throws={throws} />
<div>{didThrow ? 'HasThrown' : 'HasNotThrown'}</div>
</span>
</MaybeFragment>
Expand All @@ -60,6 +61,10 @@ export default function describeCDC({ Wrap, isShallow }) {
}
}

ErrorBoundary.defaultProps = {
ThrowerComponent: Thrower,
};

function ErrorSFC(props) {
return <ErrorBoundary {...props} />;
}
Expand Down Expand Up @@ -114,6 +119,37 @@ export default function describeCDC({ Wrap, isShallow }) {
});
});

it('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 146158d

Please sign in to comment.