Skip to content

Commit

Permalink
fixup! correctly re-create last block order when track by is an integer
Browse files Browse the repository at this point in the history
  • Loading branch information
Narretz committed Mar 16, 2017
1 parent fc8ac23 commit 7dbf428
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ var ngRepeatDirective = ['$parse', '$animate', '$compile', function($parse, $ani
nextBlockOrder[index] = trackById;
}

// setup lastBlockOrder, used to determine if block moved
// Set up lastBlockOrder. Used to determine if a block moved.
for (key in lastBlockMap) {
lastBlockOrder.push(key);
lastBlockOrder[lastBlockMap[key].index] = key;
}

for (index = 0; index < nextLength; index++) {
Expand Down
52 changes: 52 additions & 0 deletions test/ng/directive/ngRepeatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1624,4 +1624,56 @@ describe('ngRepeat animations', function() {
expect(item.element.text()).toBe('2');
})
);

it('should maintain the order when the track by expression evaluates to an integer',
inject(function($compile, $rootScope, $animate, $document, $sniffer, $timeout) {
if (!$sniffer.transitions) return;

var item;
var ss = createMockStyleSheet($document);

var items = [
{id: 1, name: 'A'},
{id: 2, name: 'B'},
{id: 4, name: 'C'},
{id: 3, name: 'D'}
]

try {

$animate.enabled(true);

ss.addRule('.animate-me div',
'transition:1s linear all;');

element = $compile(html('<div class="animate-me">' +
'<div ng-repeat="item in items track by item.id">{{ item.name }}</div>' +
'</div>'))($rootScope);

$rootScope.items = [items[0], items[1], items[2]];
$rootScope.$digest();
expect(element.text()).toBe('ABC');

$rootScope.items.push(items[3]);
$rootScope.$digest();

expect(element.text()).toBe('ABCD'); // the original order should be preserved
$animate.flush();
$timeout.flush(1500); // 1s * 1.5 closing buffer
expect(element.text()).toBe('ABCD');

$rootScope.items = [items[0], items[1], items[3]];
$rootScope.$digest();

// The leaving item should maintain it's position until it is removed
expect(element.text()).toBe('ABCD');
$animate.flush();
$timeout.flush(1500); // 1s * 1.5 closing buffer
expect(element.text()).toBe('ABD');

} finally {
ss.destroy();
}
})
);
});

0 comments on commit 7dbf428

Please sign in to comment.