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

[enzyme-adapter-react-{16,16.3}] [fix] forwardRef: respect .displayName #1817

Merged
merged 1 commit into from
Sep 12, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
case ContextConsumer || NaN: return 'ContextConsumer';
case ContextProvider || NaN: return 'ContextProvider';
case ForwardRef || NaN: {
if (type.displayName) {
return type.displayName;
}
const name = type.render.displayName || functionName(type.render);
return name ? `ForwardRef(${name})` : 'ForwardRef';
}
Expand Down
3 changes: 3 additions & 0 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ class ReactSixteenAdapter extends EnzymeAdapter {
case ContextConsumer || NaN: return 'ContextConsumer';
case ContextProvider || NaN: return 'ContextProvider';
case ForwardRef || NaN: {
if (type.displayName) {
return type.displayName;
}
const name = type.render.displayName || functionName(type.render);
return name ? `ForwardRef(${name})` : 'ForwardRef';
}
Expand Down
24 changes: 24 additions & 0 deletions packages/enzyme-test-suite/test/Debug-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -849,14 +849,20 @@ describe('debug', () => {

describeIf(is('>= 16.3'), 'forwarded ref Components', () => {
let Parent;
let ParentOfNamed;
let SomeComponent;
let NamedComponent;
beforeEach(() => {
SomeComponent = forwardRef((props, ref) => (
<div ref={ref}>
<span className="child1" />
</div>
));
Parent = () => <span><SomeComponent foo="hello" /></span>;

NamedComponent = forwardRef((props, ref) => (<div />));
NamedComponent.displayName = 'a named forward ref!';
ParentOfNamed = () => <NamedComponent />;
});

it('works with a `mount` wrapper', () => {
Expand Down Expand Up @@ -885,5 +891,23 @@ describe('debug', () => {
</ForwardRef>`
));
});

it('works with a displayName with shallow', () => {
const wrapper = shallow(<ParentOfNamed />);
expect(wrapper.debug()).to.equal((
`<${NamedComponent.displayName} />`
));
});

it('works with a displayName with mount', () => {
const wrapper = mount(<ParentOfNamed />);
expect(wrapper.debug()).to.equal((
`<ParentOfNamed>
<${NamedComponent.displayName}>
<div />
</${NamedComponent.displayName}>
</ParentOfNamed>`
));
});
});
});