Skip to content

Commit

Permalink
Fix issue finding nested SFCs by constructor in mount
Browse files Browse the repository at this point in the history
This fixes an issue where calling `.find` with an SFC constructor/
function would fail to return instances of the the SFC in the tree.

Fixes #262
  • Loading branch information
iancmyers committed Mar 21, 2016
1 parent a71d736 commit 1a43f27
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/MountedTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
coercePropValue,
nodeEqual,
propsOfNode,
isFunctionalComponent,
isSimpleSelector,
splitSelector,
selectorError,
Expand Down Expand Up @@ -60,12 +61,17 @@ export function instHasId(inst, id) {
return instId === id;
}

function isFunctionalComponentWithType(inst, func) {
return isFunctionalComponent(inst) && getNode(inst).type === func;
}

export function instHasType(inst, type) {
switch (typeof type) {
case 'string':
return nodeHasType(getNode(inst), type);
case 'function':
return isCompositeComponentWithType(inst, type);
return isCompositeComponentWithType(inst, type) ||
isFunctionalComponentWithType(inst, type);
default:
return false;
}
Expand Down Expand Up @@ -249,8 +255,7 @@ function findAllInRenderedTreeInternal(inst, test) {
const internal = internalInstance(inst);
return findAllInRenderedTreeInternal(internal, test);
}

const publicInst = inst.getPublicInstance();
const publicInst = inst.getPublicInstance() || inst._instance;
let ret = test(publicInst) ? [publicInst] : [];
const currentElement = inst._currentElement;
if (isDOMComponent(publicInst)) {
Expand Down
4 changes: 4 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export function internalInstance(inst) {
inst[internalInstanceKey(inst)];
}

export function isFunctionalComponent(inst) {
return inst && inst.constructor && inst.constructor.name === 'StatelessComponent';
}

export function propsOfNode(node) {
if (REACT013 && node && node._store) {
return (node._store.props) || {};
Expand Down
2 changes: 2 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ describeWithDOM('mount', () => {
const children = wrapper.find('.box').children();
expect(children).to.have.length(3);
expect(children.at(0).props().test).to.equal('123');
expect(wrapper.find(TestItem)).to.have.length(3);
expect(wrapper.find(TestItem).first().props().test).to.equal('123');
})
});

Expand Down

0 comments on commit 1a43f27

Please sign in to comment.