Skip to content

Commit c7d0670

Browse files
committed
[Fix] ShallowWrapper#type(), ReactWrapper#type(), and ReactWrapper#props() should work with null nodes.
Fixes #113.
1 parent 7f4bcd8 commit c7d0670

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

src/ReactWrapper.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,10 @@ export default class ReactWrapper {
381381
* @returns {Object}
382382
*/
383383
props() {
384-
return this.single(n => getNode(n).props || {});
384+
return this.single((n) => {
385+
const node = getNode(n);
386+
return node ? node.props || {} : {};
387+
});
385388
}
386389

387390
/**
@@ -484,7 +487,10 @@ export default class ReactWrapper {
484487
* @returns {String|Function}
485488
*/
486489
type() {
487-
return this.single(n => getNode(n).type);
490+
return this.single((n) => {
491+
const node = getNode(n);
492+
return node ? node.type : null;
493+
});
488494
}
489495

490496
/**

src/ShallowWrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ export default class ShallowWrapper {
464464
* @returns {String|Function}
465465
*/
466466
type() {
467-
return this.single(n => n.type);
467+
return this.single(n => n ? n.type : null);
468468
}
469469

470470
/**

src/__tests__/ReactWrapper-spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,53 @@ describeWithDOM('mount', () => {
397397
expect(spy.args[3][0].hasClass('bux')).to.equal(true);
398398
});
399399

400+
describeIf(!REACT013, 'stateless functional components', () => {
401+
it('finds nodes', () => {
402+
const SFC = function SFC({ selector }) {
403+
return (
404+
<div>
405+
<span data-foo={selector} />
406+
<i data-foo={selector} />
407+
</div>
408+
);
409+
};
410+
411+
const selector = 'blah';
412+
const wrapper = mount(<SFC selector={selector} />);
413+
const foundSpan = wrapper.findWhere(n => (
414+
n.type() === 'span' && n.props()['data-foo'] === selector
415+
));
416+
expect(foundSpan.type()).to.equal('span');
417+
418+
const foundNotSpan = wrapper.findWhere(n => (
419+
n.type() !== 'span' && n.props()['data-foo'] === selector
420+
));
421+
expect(foundNotSpan.type()).to.equal('i');
422+
});
423+
424+
it('finds nodes when conditionally rendered', () => {
425+
const SFC = function SFC({ selector }) {
426+
return (
427+
<div>
428+
<span data-foo={selector} />
429+
{selector === 'baz' ? <i data-foo={selector} /> : null}
430+
</div>
431+
);
432+
};
433+
434+
const selector = 'blah';
435+
const wrapper = mount(<SFC selector={selector} />);
436+
const foundSpan = wrapper.findWhere(n => (
437+
n.type() === 'span' && n.props()['data-foo'] === selector
438+
));
439+
expect(foundSpan.type()).to.equal('span');
440+
441+
const foundNotSpan = wrapper.findWhere(n => (
442+
n.type() !== 'span' && n.props()['data-foo'] === selector
443+
));
444+
expect(foundNotSpan).to.have.length(0);
445+
});
446+
});
400447
});
401448

402449
describe('.setProps(newProps)', () => {

src/__tests__/ShallowWrapper-spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,53 @@ describe('shallow', () => {
484484
expect(spy.args[3][0].hasClass('bux')).to.equal(true);
485485
});
486486

487+
describeIf(!REACT013, 'stateless functional components', () => {
488+
it('finds nodes', () => {
489+
const SFC = function SFC({ selector }) {
490+
return (
491+
<div>
492+
<span data-foo={selector} />
493+
<i data-foo={selector} />
494+
</div>
495+
);
496+
};
497+
498+
const selector = 'blah';
499+
const wrapper = shallow(<SFC selector={selector} />);
500+
const foundSpan = wrapper.findWhere(n => (
501+
n.type() === 'span' && n.props()['data-foo'] === selector
502+
));
503+
expect(foundSpan.type()).to.equal('span');
504+
505+
const foundNotSpan = wrapper.findWhere(n => (
506+
n.type() !== 'span' && n.props()['data-foo'] === selector
507+
));
508+
expect(foundNotSpan.type()).to.equal('i');
509+
});
510+
511+
it('finds nodes when conditionally rendered', () => {
512+
const SFC = function SFC({ selector }) {
513+
return (
514+
<div>
515+
<span data-foo={selector} />
516+
{selector === 'baz' ? <i data-foo={selector} /> : null}
517+
</div>
518+
);
519+
};
520+
521+
const selector = 'blah';
522+
const wrapper = shallow(<SFC selector={selector} />);
523+
const foundSpan = wrapper.findWhere(n => (
524+
n.type() === 'span' && n.props()['data-foo'] === selector
525+
));
526+
expect(foundSpan.type()).to.equal('span');
527+
528+
const foundNotSpan = wrapper.findWhere(n => (
529+
n.type() !== 'span' && n.props()['data-foo'] === selector
530+
));
531+
expect(foundNotSpan).to.have.length(0);
532+
});
533+
});
487534
});
488535

489536
describe('.setProps(newProps)', () => {

0 commit comments

Comments
 (0)