Skip to content

Commit

Permalink
[enzyme-adapter-react-16] [fix] Ensure recursive calls to `displayNam…
Browse files Browse the repository at this point in the history
…eOfNode` uses the adapter's version of the method

Fixes #2481
  • Loading branch information
mejackreed authored and ljharb committed Dec 7, 2020
1 parent bdb618e commit 75e8963
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
displayNameOfNode(node) {
if (!node) return null;
const { type, $$typeof } = node;
const adapter = this;

const nodeType = type || $$typeof;

Expand All @@ -886,13 +887,13 @@ class ReactSixteenAdapter extends EnzymeAdapter {
case ContextProvider || NaN: return 'ContextProvider';
case Memo || NaN: {
const nodeName = displayNameOfNode(node);
return typeof nodeName === 'string' ? nodeName : `Memo(${displayNameOfNode(type)})`;
return typeof nodeName === 'string' ? nodeName : `Memo(${adapter.displayNameOfNode(type)})`;
}
case ForwardRef || NaN: {
if (type.displayName) {
return type.displayName;
}
const name = displayNameOfNode({ type: type.render });
const name = adapter.displayNameOfNode({ type: type.render });
return name ? `ForwardRef(${name})` : 'ForwardRef';
}
case Lazy || NaN: {
Expand Down
35 changes: 35 additions & 0 deletions packages/enzyme-test-suite/test/Utils-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,41 @@ describe('Utils', () => {
expect(displayNameOfNode(<div />)).to.equal('div');
});
});

describeIf(is('>= 16.6'), 'given an inner displayName in Memo', () => {
it('returns the displayName', () => {
const adapter = getAdapter();
const Foo = () => <div />;
Foo.displayName = 'CustomWrapper';

const MemoFoo = React.memo(Foo);

expect(adapter.displayNameOfNode(<MemoFoo />)).to.equal('Memo(CustomWrapper)');
});
});

describeIf(is('>= 16.6'), 'given an inner displayName in forwardedRef', () => {
it('returns the displayName', () => {
const adapter = getAdapter();
const Foo = () => <div />;
Foo.displayName = 'CustomWrapper';

const ForwardedFoo = React.forwardRef(Foo);

expect(adapter.displayNameOfNode(<ForwardedFoo />)).to.equal('ForwardRef(CustomWrapper)');
});
});

describeIf(is('>= 16.6'), 'given an inner displayName wrapped in Memo and forwardRef', () => {
it('returns the displayName', () => {
const adapter = getAdapter();
const Foo = () => <div />;
Foo.displayName = 'CustomWrapper';

const MemoForwardFoo = React.memo(React.forwardRef(Foo));
expect(adapter.displayNameOfNode(<MemoForwardFoo />)).to.equal('Memo(ForwardRef(CustomWrapper))');
});
});
});

describe('childrenToSimplifiedArray', () => {
Expand Down

0 comments on commit 75e8963

Please sign in to comment.