Skip to content

Commit 0632c59

Browse files
Prevent null or false nodes from being passed into tree traversal
1 parent 4bf5b71 commit 0632c59

3 files changed

Lines changed: 35 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,22 @@ describeWithDOM('mount', () => {
444444
expect(foundNotSpan).to.have.length(0);
445445
});
446446
});
447+
448+
it('should not pass in null or false nodes', () => {
449+
const wrapper = mount(
450+
<div>
451+
<div className="foo bar" />
452+
{null}
453+
{false}
454+
</div>
455+
);
456+
const stub = sinon.stub();
457+
stub.returns(true);
458+
const spy = sinon.spy(stub);
459+
wrapper.findWhere(spy);
460+
expect(spy.callCount).to.equal(2);
461+
});
462+
447463
});
448464

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

src/__tests__/ShallowWrapper-spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,22 @@ describe('shallow', () => {
531531
expect(foundNotSpan).to.have.length(0);
532532
});
533533
});
534+
535+
it('should not pass in null or false nodes', () => {
536+
const wrapper = shallow(
537+
<div>
538+
<div className="foo bar" />
539+
{null}
540+
{false}
541+
</div>
542+
);
543+
const stub = sinon.stub();
544+
stub.returns(true);
545+
const spy = sinon.spy(stub);
546+
wrapper.findWhere(spy);
547+
expect(spy.callCount).to.equal(2);
548+
});
549+
534550
});
535551

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

0 commit comments

Comments
 (0)