Skip to content

Commit 4f03d9e

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

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

src/ReactWrapper.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import {
2121
mapNativeEventNames,
2222
containsChildrenSubArray,
23+
propsOfNode,
2324
} from './Utils';
2425
import {
2526
debugInsts,
@@ -381,7 +382,7 @@ export default class ReactWrapper {
381382
* @returns {Object}
382383
*/
383384
props() {
384-
return this.single(n => getNode(n).props || {});
385+
return this.single(propsOfNode);
385386
}
386387

387388
/**
@@ -484,7 +485,10 @@ export default class ReactWrapper {
484485
* @returns {String|Function}
485486
*/
486487
type() {
487-
return this.single(n => getNode(n).type);
488+
return this.single((n) => {
489+
const node = getNode(n);
490+
return node ? node.type : null;
491+
});
488492
}
489493

490494
/**

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)