Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Ensure recursive calls to displayNameOfNode uses the adapter's version of the method #2482

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that wasn't clear to me was when/why we should use the adapter here or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think if we did, it would be an infinite loop.

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