Skip to content

Commit

Permalink
Ensure recurrsive calls to displayNameOfNode uses this version of t…
Browse files Browse the repository at this point in the history
…he method fixes #2481
  • Loading branch information
mejackreed committed Dec 7, 2020
1 parent bdb618e commit a6c671a
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 a6c671a

Please sign in to comment.