Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Found and fix a mount issue with find #230

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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