Merge pull request #2458 from leebyron/fix-iterable

[traverseAllChildren] fix out-of-scope var use.
This commit is contained in:
Lee Byron
2014-11-03 21:25:01 -05:00
2 changed files with 58 additions and 8 deletions
@@ -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: <div />, 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 = (
<div>
{threeDivIterable}
</div>
);
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;
+6 -7
View File
@@ -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)
);