Skip to content

Commit 8c55734

Browse files
henryqdineenljharb
authored andcommitted
[enzyme-adapter-react-16] [fix] fix simulateError() on Memo component
1 parent f046079 commit 8c55734

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
527527
rootNode,
528528
nodeHierarchy,
529529
nodeTypeFromType,
530-
adapter.displayNameOfNode,
530+
adapter.displayNameOfNode.bind(adapter),
531531
is166 ? catchingType : undefined,
532532
);
533533
},
@@ -787,7 +787,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
787787
cachedNode,
788788
nodeHierarchy.concat(cachedNode),
789789
nodeTypeFromType,
790-
adapter.displayNameOfNode,
790+
adapter.displayNameOfNode.bind(adapter),
791791
is166 ? cachedNode.type : undefined,
792792
);
793793
},

packages/enzyme-test-suite/test/shared/lifecycles/componentDidCatch.jsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default function describeCDC({
5454
}
5555

5656
render() {
57+
const { ThrowerComponent } = this.props;
5758
const {
5859
didThrow,
5960
throws,
@@ -62,7 +63,7 @@ export default function describeCDC({
6263
<div>
6364
<MaybeFragment>
6465
<span>
65-
<Thrower throws={throws} />
66+
{<ThrowerComponent throws={throws} />}
6667
<div>
6768
{didThrow ? 'HasThrown' : 'HasNotThrown'}
6869
</div>
@@ -73,6 +74,10 @@ export default function describeCDC({
7374
}
7475
}
7576

77+
ErrorBoundary.defaultProps = {
78+
ThrowerComponent: Thrower,
79+
};
80+
7681
function ErrorSFC(props) {
7782
return <ErrorBoundary {...props} />;
7883
}
@@ -121,6 +126,39 @@ export default function describeCDC({
121126
});
122127
});
123128

129+
itIf(
130+
is('>= 16.6'),
131+
'catches a simulated error on memo() component',
132+
() => {
133+
const MemoThrower = React.memo(Thrower);
134+
const spy = sinon.spy();
135+
const wrapper = Wrap(
136+
<ErrorBoundary spy={spy} ThrowerComponent={MemoThrower} />,
137+
);
138+
139+
expect(spy).to.have.property('callCount', 0);
140+
141+
expect(() => wrapper.find(Thrower).simulateError(errorToThrow)).not.to.throw();
142+
143+
expect(spy).to.have.property('callCount', 1);
144+
145+
expect(spy.args).to.be.an('array').and.have.lengthOf(1);
146+
const [[actualError, info]] = spy.args;
147+
expect(() => {
148+
throw actualError;
149+
}).to.throw(errorToThrow);
150+
expect(info).to.deep.equal({
151+
componentStack: `
152+
in Memo(Thrower) (created by ErrorBoundary)
153+
in span (created by ErrorBoundary)${hasFragments ? '' : `
154+
in main (created by ErrorBoundary)`}
155+
in div (created by ErrorBoundary)
156+
in ErrorBoundary (created by WrapperComponent)
157+
in WrapperComponent`,
158+
});
159+
},
160+
);
161+
124162
it('rerenders on a simulated error', () => {
125163
const wrapper = Wrap(<ErrorBoundary spy={sinon.stub()} />);
126164

0 commit comments

Comments
 (0)