Skip to content

Commit

Permalink
[enzyme-adapter-utils, enzyme-adapter-react-*] Add support for forwar…
Browse files Browse the repository at this point in the history
…dRef
  • Loading branch information
jquense authored and ljharb committed Mar 23, 2018
1 parent be1f272 commit 006bc35
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/enzyme-adapter-react-16.3/src/ReactSixteenThreeAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const HostText = 6;
const Mode = 11;
const ContextConsumerType = 12;
const ContextProviderType = 13;
const ForwardRefType = 14;

function nodeAndSiblingsArray(nodeWithSibling) {
const array = [];
Expand Down Expand Up @@ -133,6 +134,17 @@ function toTree(vnode) {
case ContextProviderType: // 13
case ContextConsumerType: // 12
return childrenToTree(node.child);
case ForwardRefType: {
return {
nodeType: 'function',
type: node.type,
props: { ...node.memoizedProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: null,
rendered: childrenToTree(node.child),
};
}
default:
throw new Error(`Enzyme Internal Error: unknown node with tag ${node.tag}`);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const HostText = 6;
const Mode = 11;
const ContextConsumerType = 12;
const ContextProviderType = 13;
const ForwardRefType = 14;

function nodeAndSiblingsArray(nodeWithSibling) {
const array = [];
Expand Down Expand Up @@ -111,6 +112,7 @@ function toTree(vnode) {
instance: null,
rendered: childrenToTree(node.child),
};

case HostComponent: { // 5
let renderedNodes = flatten(nodeAndSiblingsArray(node.child).map(toTree));
if (renderedNodes.length === 0) {
Expand All @@ -133,6 +135,17 @@ function toTree(vnode) {
case ContextProviderType: // 13
case ContextConsumerType: // 12
return childrenToTree(node.child);
case ForwardRefType: {
return {
nodeType: 'function',
type: node.type,
props: { ...node.memoizedProps },
key: ensureKeyOrUndefined(node.key),
ref: node.ref,
instance: null,
rendered: childrenToTree(node.child),
};
}
default:
throw new Error(`Enzyme Internal Error: unknown node with tag ${node.tag}`);
}
Expand Down
43 changes: 43 additions & 0 deletions packages/enzyme-test-suite/test/Debug-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
} from 'enzyme/build/Debug';

import './_helpers/setupAdapters';
import {
forwardRef,
} from './_helpers/react-compat';
import {
describeWithDOM,
describeIf,
Expand Down Expand Up @@ -837,4 +840,44 @@ describe('debug', () => {
));
});
});

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

it('works with a `mount` wrapper', () => {
const wrapper = mount(<Parent foo="hello" />);
expect(wrapper.debug()).to.equal((
`<Parent foo="hello">
<span>
<ForwardRef foo="hello">
<div>
<span className="child1" />
</div>
</ForwardRef>
</span>
</Parent>`
));
});

it('works with a `mount` `.find` wrapper', () => {
const wrapper = mount(<Parent foo="hello" />);
const results = wrapper.find(SomeComponent);
expect(results.debug()).to.equal((
`<ForwardRef foo="hello">
<div>
<span className="child1" />
</div>
</ForwardRef>`
));
});
});
});
40 changes: 40 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
createPortal,
createRef,
Fragment,
forwardRef,
} from './_helpers/react-compat';
import {
describeWithDOM,
Expand Down Expand Up @@ -210,6 +211,45 @@ describeWithDOM('mount', () => {
expect(wrapper.find('span').text()).to.equal('foo');
});

describeIf(is('>= 16.3'), 'forwarded ref Components', () => {
wrap().withConsoleThrows().it('should mount without complaint', () => {
const SomeComponent = forwardRef((props, ref) => (
<div {...props} ref={ref} />
));

expect(() => mount(<SomeComponent />)).not.to.throw();
});

it('should find elements through forwardedRef elements', () => {
const testRef = () => {};
const SomeComponent = forwardRef((props, ref) => (
<div ref={ref}>
<span className="child1" />
<span className="child2" />
</div>
));

const wrapper = mount(<div><SomeComponent ref={testRef} /></div>);

expect(wrapper.find('.child2')).to.have.lengthOf(1);
});

it('should find forwardRef element', () => {
const SomeComponent = forwardRef((props, ref) => (
<div ref={ref}>
<span className="child1" />
</div>
));
const Parent = () => <span><SomeComponent foo="hello" /></span>;

const wrapper = mount(<Parent foo="hello" />);
const results = wrapper.find(SomeComponent);

expect(results).to.have.lengthOf(1);
expect(results.props()).to.eql({ foo: 'hello' });
});
});

describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('can pass in context', () => {
const SimpleComponent = (props, context) => (
Expand Down
13 changes: 13 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
createContext,
createRef,
Fragment,
forwardRef,
} from './_helpers/react-compat';
import {
describeIf,
Expand Down Expand Up @@ -145,7 +146,19 @@ describe('shallow', () => {

expect(shallow(<Consumes />).find('span')).to.have.lengthOf(1);
expect(shallow(<Provides />).find(Consumes)).to.have.lengthOf(1);
});

itIf(is('>= 16.3'), 'should find elements through forwarded refs elements', () => {
const SomeComponent = forwardRef((props, ref) => (
<div ref={ref}>
<span className="child1" />
<span className="child2" />
</div>
));

const wrapper = shallow(<SomeComponent />);

expect(wrapper.find('.child2')).to.have.length(1);
});

describeIf(is('> 0.13'), 'stateless function components', () => {
Expand Down

0 comments on commit 006bc35

Please sign in to comment.