diff --git a/src/utils/__tests__/traverseAllChildren-test.js b/src/utils/__tests__/traverseAllChildren-test.js index 625f89f5b8..b14ff2bdfc 100644 --- a/src/utils/__tests__/traverseAllChildren-test.js +++ b/src/utils/__tests__/traverseAllChildren-test.js @@ -288,7 +288,58 @@ describe('traverseAllChildren', function() { ); }); - it('should be called for each child in an iterable', function() { + it('should be called for each child in an iterable without keys', function() { + var threeDivIterable = { + '@@iterator': function() { + var i = 0; + return { + next: function() { + if (i++ < 3) { + return { value:
, done: false }; + } else { + return { value: undefined, done: true }; + } + } + }; + } + }; + + var traverseContext = []; + var traverseFn = + jasmine.createSpy().andCallFake(function(context, kid, key, index) { + context.push(kid); + }); + + var instance = ( +
+ {threeDivIterable} +
+ ); + + traverseAllChildren(instance.props.children, traverseFn, traverseContext); + expect(traverseFn.calls.length).toBe(3); + + expect(traverseFn).toHaveBeenCalledWith( + traverseContext, + traverseContext[0], + '.0', + 0 + ); + expect(traverseFn).toHaveBeenCalledWith( + traverseContext, + traverseContext[1], + '.1', + 1 + ); + expect(traverseFn).toHaveBeenCalledWith( + traverseContext, + traverseContext[2], + '.2', + 2 + ); + }); + + it('should be called for each child in an iterable with keys', function() { var threeDivIterable = { '@@iterator': function() { var i = 0; diff --git a/src/utils/traverseAllChildren.js b/src/utils/traverseAllChildren.js index 9120c6fdd4..042f48008c 100644 --- a/src/utils/traverseAllChildren.js +++ b/src/utils/traverseAllChildren.js @@ -125,8 +125,7 @@ function traverseAllChildrenImpl( for (var i = 0; i < children.length; i++) { child = children[i]; nextName = ( - nameSoFar + - (nameSoFar ? SUBSEPARATOR : SEPARATOR) + + (nameSoFar !== '' ? nameSoFar + SUBSEPARATOR : SEPARATOR) + getComponentKey(child, i) ); nextIndex = indexSoFar + subtreeCount; @@ -144,12 +143,12 @@ function traverseAllChildrenImpl( var iterator = iteratorFn.call(children); var step; if (iteratorFn !== children.entries) { + var ii = 0; while (!(step = iterator.next()).done) { child = step.value; nextName = ( - nameSoFar + - (nameSoFar ? SUBSEPARATOR : SEPARATOR) + - getComponentKey(child, i) + (nameSoFar !== '' ? nameSoFar + SUBSEPARATOR : SEPARATOR) + + getComponentKey(child, ii++) ); nextIndex = indexSoFar + subtreeCount; subtreeCount += traverseAllChildrenImpl( @@ -167,7 +166,7 @@ function traverseAllChildrenImpl( if (entry) { child = entry[1]; nextName = ( - nameSoFar + (nameSoFar ? SUBSEPARATOR : SEPARATOR) + + (nameSoFar !== '' ? nameSoFar + SUBSEPARATOR : SEPARATOR) + wrapUserProvidedKey(entry[0]) + SUBSEPARATOR + getComponentKey(child, 0) ); @@ -192,7 +191,7 @@ function traverseAllChildrenImpl( if (children.hasOwnProperty(key)) { child = children[key]; nextName = ( - nameSoFar + (nameSoFar ? SUBSEPARATOR : SEPARATOR) + + (nameSoFar !== '' ? nameSoFar + SUBSEPARATOR : SEPARATOR) + wrapUserProvidedKey(key) + SUBSEPARATOR + getComponentKey(child, 0) );