Skip to content
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
6 changes: 4 additions & 2 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
import {
mapNativeEventNames,
containsChildrenSubArray,
propsOfNode,
typeOfNode,
} from './Utils';
import {
debugInsts,
Expand Down Expand Up @@ -381,7 +383,7 @@ export default class ReactWrapper {
* @returns {Object}
*/
props() {
return this.single(n => getNode(n).props || {});
return this.single(propsOfNode);
}

/**
Expand Down Expand Up @@ -484,7 +486,7 @@ export default class ReactWrapper {
* @returns {String|Function}
*/
type() {
return this.single(n => getNode(n).type);
return this.single(n => typeOfNode(getNode(n)));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
propFromEvent,
withSetStateAllowed,
propsOfNode,
typeOfNode,
} from './Utils';
import {
debugNodes,
Expand Down Expand Up @@ -464,7 +465,7 @@ export default class ShallowWrapper {
* @returns {String|Function}
*/
type() {
return this.single(n => n.type);
return this.single(typeOfNode);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export function propsOfNode(node) {
return (node && node.props) || {};
}

export function typeOfNode(node) {
return node ? node.type : null;
}

export function onPrototype(Component, lifecycle, method) {
const proto = Component.prototype;
Object.getOwnPropertyNames(proto).forEach((name) => {
Expand Down
47 changes: 47 additions & 0 deletions src/__tests__/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,53 @@ describeWithDOM('mount', () => {
expect(spy.args[3][0].hasClass('bux')).to.equal(true);
});

describeIf(!REACT013, 'stateless functional components', () => {
it('finds nodes', () => {
const SFC = function SFC({ selector }) {
return (
<div>
<span data-foo={selector} />
<i data-foo={selector} />
</div>
);
};

const selector = 'blah';
const wrapper = mount(<SFC selector={selector} />);
const foundSpan = wrapper.findWhere(n => (
n.type() === 'span' && n.props()['data-foo'] === selector
));
expect(foundSpan.type()).to.equal('span');

const foundNotSpan = wrapper.findWhere(n => (
n.type() !== 'span' && n.props()['data-foo'] === selector
));
expect(foundNotSpan.type()).to.equal('i');
});

it('finds nodes when conditionally rendered', () => {
const SFC = function SFC({ selector }) {
return (
<div>
<span data-foo={selector} />
{selector === 'baz' ? <i data-foo={selector} /> : null}
</div>
);
};

const selector = 'blah';
const wrapper = mount(<SFC selector={selector} />);
const foundSpan = wrapper.findWhere(n => (
n.type() === 'span' && n.props()['data-foo'] === selector
));
expect(foundSpan.type()).to.equal('span');

const foundNotSpan = wrapper.findWhere(n => (
n.type() !== 'span' && n.props()['data-foo'] === selector
));
expect(foundNotSpan).to.have.length(0);
});
});
});

describe('.setProps(newProps)', () => {
Expand Down
47 changes: 47 additions & 0 deletions src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,53 @@ describe('shallow', () => {
expect(spy.args[3][0].hasClass('bux')).to.equal(true);
});

describeIf(!REACT013, 'stateless functional components', () => {
it('finds nodes', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests don't really seem to correspond with the behavior that was changed in this PR.... right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first "it" block is just a sanity check (and passed prior to my change) - the second one is the one that changed in this PR (and failed prior to my change)

same in the ReactWrapper specs

const SFC = function SFC({ selector }) {
return (
<div>
<span data-foo={selector} />
<i data-foo={selector} />
</div>
);
};

const selector = 'blah';
const wrapper = shallow(<SFC selector={selector} />);
const foundSpan = wrapper.findWhere(n => (
n.type() === 'span' && n.props()['data-foo'] === selector
));
expect(foundSpan.type()).to.equal('span');

const foundNotSpan = wrapper.findWhere(n => (
n.type() !== 'span' && n.props()['data-foo'] === selector
));
expect(foundNotSpan.type()).to.equal('i');
});

it('finds nodes when conditionally rendered', () => {
const SFC = function SFC({ selector }) {
return (
<div>
<span data-foo={selector} />
{selector === 'baz' ? <i data-foo={selector} /> : null}
</div>
);
};

const selector = 'blah';
const wrapper = shallow(<SFC selector={selector} />);
const foundSpan = wrapper.findWhere(n => (
n.type() === 'span' && n.props()['data-foo'] === selector
));
expect(foundSpan.type()).to.equal('span');

const foundNotSpan = wrapper.findWhere(n => (
n.type() !== 'span' && n.props()['data-foo'] === selector
));
expect(foundNotSpan).to.have.length(0);
});
});
});

describe('.setProps(newProps)', () => {
Expand Down