Skip to content

Commit

Permalink
Merge pull request #251 from nishitaniyuki/patch-1
Browse files Browse the repository at this point in the history
Prevent falsy nodes from being counted as children
lelandrichardson committed Mar 14, 2016
2 parents cb9591f + 0e5ace1 commit c711f13
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ShallowTraversal.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,11 @@ export function childrenOfNode(node) {
if (!node) return [];
const maybeArray = propsOfNode(node).children;
const result = [];
React.Children.forEach(maybeArray, child => result.push(child));
React.Children.forEach(maybeArray, child => {
if (child !== null && child !== false && typeof child !== 'undefined') {
result.push(child);
}
});
return result;
}

@@ -29,7 +33,7 @@ export function hasClassName(node, className) {
}

export function treeForEach(tree, fn) {
if (tree !== null && tree !== false) {
if (tree !== null && tree !== false && typeof tree !== 'undefined') {
fn(tree);
}
childrenOfNode(tree).forEach(node => treeForEach(node, fn));
25 changes: 25 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
@@ -1019,6 +1019,31 @@ describe('shallow', () => {
expect(wrapper.children().length).to.equal(0);
});

it('should skip the falsy children', () => {
const wrapper = shallow(
<div>
<div>
{false}
{[false, false]}
<p>foo</p>
</div>
<div>
{undefined}
{[undefined, undefined]}
<p>bar</p>
</div>
<div>
{null}
{[null, null]}
<p>baz</p>
</div>
</div>
);
expect(wrapper.childAt(0).children().length).to.equal(1);
expect(wrapper.childAt(1).children().length).to.equal(1);
expect(wrapper.childAt(2).children().length).to.equal(1);
});

it('should return the children nodes of the root', () => {
const wrapper = shallow(
<div>

0 comments on commit c711f13

Please sign in to comment.