Skip to content

Commit

Permalink
[Fix] ShallowWrapper#type(), ReactWrapper#type(), and `ReactWrapp…
Browse files Browse the repository at this point in the history
…er#props()` should work with `null` nodes.

Fixes #113.
  • Loading branch information
ljharb committed Feb 3, 2016
1 parent 7f4bcd8 commit c7d0670
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,10 @@ export default class ReactWrapper {
* @returns {Object}
*/
props() {
return this.single(n => getNode(n).props || {});
return this.single((n) => {
const node = getNode(n);
return node ? node.props || {} : {};
});
}

/**
Expand Down Expand Up @@ -484,7 +487,10 @@ export default class ReactWrapper {
* @returns {String|Function}
*/
type() {
return this.single(n => getNode(n).type);
return this.single((n) => {
const node = getNode(n);
return node ? node.type : null;
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export default class ShallowWrapper {
* @returns {String|Function}
*/
type() {
return this.single(n => n.type);
return this.single(n => n ? n.type : null);
}

/**
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', () => {
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

0 comments on commit c7d0670

Please sign in to comment.