Skip to content

Commit 0b3d0c0

Browse files
Merge pull request #174 from airbnb/lmr--prevent-null-in-traversal-methods
Prevent null or false nodes from being passed into tree traversal
2 parents 0b7c31d + 0632c59 commit 0b3d0c0

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
@@ -466,6 +466,22 @@ describeWithDOM('mount', () => {
466466
expect(foundNotSpan).to.have.length(0);
467467
});
468468
});
469+
470+
it('should not pass in null or false nodes', () => {
471+
const wrapper = mount(
472+
<div>
473+
<div className="foo bar" />
474+
{null}
475+
{false}
476+
</div>
477+
);
478+
const stub = sinon.stub();
479+
stub.returns(true);
480+
const spy = sinon.spy(stub);
481+
wrapper.findWhere(spy);
482+
expect(spy.callCount).to.equal(2);
483+
});
484+
469485
});
470486

471487
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)