Skip to content

Commit

Permalink
Merge pull request #167 from mmarcuccio/mount-deep-exclude-forward-ref
Browse files Browse the repository at this point in the history
Exclude forward ref when using deep mount
  • Loading branch information
VincentLanglet committed May 26, 2020
2 parents fd3b939 + 356c30c commit 866547a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/mount.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import omitBy from 'lodash/omitBy';
import isNil from 'lodash/isNil';
import {ForwardRef} from 'react-is';

import {typeName} from 'enzyme/build/Debug';
import {childrenOfNode, propsOfNode} from 'enzyme/build/RSTTraversal';
Expand Down Expand Up @@ -48,7 +49,10 @@ function internalNodeToJson(node, options) {
return node.map(child => internalNodeToJson(child, options));
}

if (options.mode === 'deep' && typeof node.type === 'function') {
if (
options.mode === 'deep' &&
(typeof node.type === 'function' || node.type.$$typeof === ForwardRef)
) {
return internalNodeToJson(node.rendered, options);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/__snapshots__/mount-deep.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ exports[`converts pure mount with mixed children 1`] = `
</div>
`;

exports[`excludes forwardRef node but renders wrapped component 1`] = `
<div
className="basic-class undefined"
onClick={[Function]}
>
<div
className="group"
id="group-id"
>
<span />
<span
className="empty"
/>
</div>
</div>
`;

exports[`handles a component which returns null 1`] = `""`;

exports[`includes undefined props 1`] = `
Expand Down
7 changes: 7 additions & 0 deletions tests/mount-deep.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ClassWithNull,
ClassArrayRender,
} from './fixtures/class';
import {ForwardRefWithDefaultProps} from './fixtures/forwardRef';

Enzyme.configure({adapter: new Adapter()});
const deepOptions = {mode: 'deep'};
Expand Down Expand Up @@ -128,3 +129,9 @@ it('renders multiple elements as a result of find', () => {

expect(mountToJson(mounted.find('li'), deepOptions)).toMatchSnapshot();
});

it('excludes forwardRef node but renders wrapped component', () => {
const mounted = mount(<ForwardRefWithDefaultProps />);

expect(mountToJson(mounted, deepOptions)).toMatchSnapshot();
});

0 comments on commit 866547a

Please sign in to comment.