Skip to content

Commit

Permalink
[enzyme-adapter-react-{16,16.3}] [fix] forwardRef: respect `.displa…
Browse files Browse the repository at this point in the history
…yName` on the forwardRef

Fixes #1810.
  • Loading branch information
ljharb committed Sep 11, 2018
1 parent ca054b9 commit d2c06d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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>`
));
});
});
});

0 comments on commit d2c06d9

Please sign in to comment.