Skip to content

Commit

Permalink
fix find forwardRef itself
Browse files Browse the repository at this point in the history
  • Loading branch information
cyan33 committed Jun 20, 2018
1 parent eafaed6 commit 43c32d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Expand Up @@ -210,6 +210,21 @@ describeWithDOM('mount', () => {

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

it('should find the forwardRef element itself', () => {
const testRef = () => {};
const OtherComponent = () => (
<div />
);
const SomeComponent = forwardRef((props, ref) => (
<OtherComponent className="dumb" ref={ref} />
));

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

expect(wrapper.find(SomeComponent)).to.have.length(1);
expect(wrapper.find(OtherComponent)).to.have.length(1);
});
});

describeIf(!REACT013, 'stateless components', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/enzyme/src/selectors.js
Expand Up @@ -13,7 +13,7 @@ import {
childrenOfNode,
hasClassName,
} from './RSTTraversal';
import { nodeHasType, propsOfNode } from './Utils';
import { nodeHasType, propsOfNode, typeOf, ForwardRef } from './Utils';
// our CSS selector parser instance
const parser = createParser();

Expand Down Expand Up @@ -227,6 +227,12 @@ export function buildPredicate(selector) {
if (hasUndefinedValues) {
throw new TypeError('Enzyme::Props can’t have `undefined` values. Try using ‘findWhere()’ instead.');
}
// the selector could also be a forwardRef
if (typeOf(selector) === ForwardRef) {
// re-build the predicate based on what is wrapped by forwardRef
// rather than the forwardRef itself
return buildPredicate(selector.render().props);
}
return node => nodeMatchesObjectProps(node, selector);
}
throw new TypeError('Enzyme::Selector does not support an array, null, or empty object as a selector');
Expand Down

0 comments on commit 43c32d9

Please sign in to comment.