Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(ngRepeat): correctly re-create last block order when track by is …
Browse files Browse the repository at this point in the history
…an integer

Merge changes from Narretz@7dbf428.
  • Loading branch information
pondermatic committed Mar 21, 2017
1 parent dcc80c1 commit c969ae2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ng/directive/ngRepeat.js
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
51 changes: 51 additions & 0 deletions test/ng/directive/ngRepeatSpec.js
Expand Up @@ -1624,4 +1624,55 @@ 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 c969ae2

Please sign in to comment.