From 20042fb3af09e7d89616c7ac9893699a88a5ad00 Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Fri, 4 Nov 2022 12:52:50 +0100 Subject: [PATCH] Fix simulateError() on Memo component From https://github.com/enzymejs/enzyme/pull/2525 Co-Authored-By: Henry Q. Dineen <682132+henryqdineen@users.noreply.github.com> --- src/ReactSeventeenAdapter.js | 4 +- .../shared/lifecycles/componentDidCatch.jsx | 38 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/ReactSeventeenAdapter.js b/src/ReactSeventeenAdapter.js index 4bddddd..cfe46ce 100644 --- a/src/ReactSeventeenAdapter.js +++ b/src/ReactSeventeenAdapter.js @@ -475,7 +475,7 @@ class ReactSeventeenAdapter extends EnzymeAdapter { rootNode, nodeHierarchy, nodeTypeFromType, - adapter.displayNameOfNode, + adapter.displayNameOfNode.bind(adapter), catchingType, ); }, @@ -708,7 +708,7 @@ class ReactSeventeenAdapter extends EnzymeAdapter { cachedNode, nodeHierarchy.concat(cachedNode), nodeTypeFromType, - adapter.displayNameOfNode, + adapter.displayNameOfNode.bind(adapter), cachedNode.type, ); }, diff --git a/test/test/shared/lifecycles/componentDidCatch.jsx b/test/test/shared/lifecycles/componentDidCatch.jsx index a28ff06..21ffe62 100644 --- a/test/test/shared/lifecycles/componentDidCatch.jsx +++ b/test/test/shared/lifecycles/componentDidCatch.jsx @@ -46,12 +46,13 @@ export default function describeCDC({ Wrap, isShallow }) { } render() { + const { ThrowerComponent } = this.props; const { didThrow, throws } = this.state; return (
- +
{didThrow ? 'HasThrown' : 'HasNotThrown'}
@@ -60,6 +61,10 @@ export default function describeCDC({ Wrap, isShallow }) { } } + ErrorBoundary.defaultProps = { + ThrowerComponent: Thrower, + }; + function ErrorSFC(props) { return ; } @@ -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(); + + 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();