Skip to content

Commit

Permalink
Merge pull request #230 from CurtisHumphrey/mount_find_component_by_name
Browse files Browse the repository at this point in the history
[mount] Fix find by component name
  • Loading branch information
lelandrichardson committed Mar 7, 2016
2 parents f04a131 + 2d69c0d commit 907fe34
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/MountedTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isCompoundSelector,
AND,
SELECTOR,
nodeHasType,
} from './Utils';
import {
isDOMComponent,
Expand Down Expand Up @@ -62,8 +63,7 @@ export function instHasId(inst, id) {
export function instHasType(inst, type) {
switch (typeof type) {
case 'string':
return isDOMComponent(inst) &&
inst.tagName.toUpperCase() === type.toUpperCase();
return nodeHasType(getNode(inst), type);
case 'function':
return isCompositeComponentWithType(inst, type);
default:
Expand Down
9 changes: 1 addition & 8 deletions src/ShallowTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
selectorType,
AND,
SELECTOR,
nodeHasType,
} from './Utils';


Expand Down Expand Up @@ -91,14 +92,6 @@ export function nodeHasProperty(node, propKey, stringifiedPropValue) {
return nodeProps.hasOwnProperty(propKey);
}


export function nodeHasType(node, type) {
if (!type || !node) return false;
if (!node.type) return false;
if (typeof node.type === 'string') return node.type === type;
return node.type.name === type || node.type.displayName === type;
}

export function nodeMatchesObjectProps(node, props) {
return isSubset(propsOfNode(node), props);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export function getNode(node) {
return isDOMComponent(node) ? findDOMNode(node) : node;
}

export function nodeHasType(node, type) {
if (!type || !node) return false;
if (!node.type) return false;
if (typeof node.type === 'string') return node.type === type;
return node.type.name === type || node.type.displayName === type;
}

export function childrenEqual(a, b) {
if (a === b) return true;
if (!Array.isArray(a) && !Array.isArray(b)) {
Expand Down
12 changes: 12 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ describeWithDOM('mount', () => {
expect(wrapper.find(Foo).type()).to.equal(Foo);
});

it('should find a component based on a component displayName', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = mount(
<div>
<Foo className="foo" />
</div>
);
expect(wrapper.find('Foo').type()).to.equal(Foo);
});

it('should find component based on a react prop', () => {
const wrapper = mount(
<div>
Expand Down

0 comments on commit 907fe34

Please sign in to comment.