Skip to content

Commit 42d25b2

Browse files
Prevent null or false nodes from being passed into tree traversal
1 parent 7f4bcd8 commit 42d25b2

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/ShallowTraversal.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export function hasClassName(node, className) {
2828
}
2929

3030
export function treeForEach(tree, fn) {
31-
fn(tree);
31+
if (tree !== null && tree !== false) {
32+
fn(tree);
33+
}
3234
childrenOfNode(tree).forEach(node => treeForEach(node, fn));
3335
}
3436

src/__tests__/ReactWrapper-spec.js

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

400+
it('should not pass in null or false nodes', () => {
401+
const wrapper = mount(
402+
<div>
403+
<div className="foo bar" />
404+
{null}
405+
{false}
406+
</div>
407+
);
408+
const stub = sinon.stub();
409+
stub.returns(true);
410+
const spy = sinon.spy(stub);
411+
wrapper.findWhere(spy);
412+
expect(spy.callCount).to.equal(2);
413+
});
414+
400415
});
401416

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

src/__tests__/ShallowWrapper-spec.js

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

487+
it('should not pass in null or false nodes', () => {
488+
const wrapper = shallow(
489+
<div>
490+
<div className="foo bar" />
491+
{null}
492+
{false}
493+
</div>
494+
);
495+
const stub = sinon.stub();
496+
stub.returns(true);
497+
const spy = sinon.spy(stub);
498+
wrapper.findWhere(spy);
499+
expect(spy.callCount).to.equal(2);
500+
});
501+
487502
});
488503

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

0 commit comments

Comments
 (0)