Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(pagination): handle extreme values for total-items
Browse files Browse the repository at this point in the history
Closes #1104
  • Loading branch information
bekos authored and pkozlowski-opensource committed Oct 2, 2013
1 parent 53709f0 commit 8ecf93e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/pagination/pagination.js
Expand Up @@ -27,7 +27,8 @@ angular.module('ui.bootstrap.pagination', [])
};

this.calculateTotalPages = function() {
return this.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / this.itemsPerPage);
var totalPages = this.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / this.itemsPerPage);
return Math.max(totalPages || 0, 1);
};

this.getAttributeValue = function(attribute, defaultValue, interpolate) {
Expand All @@ -36,7 +37,9 @@ angular.module('ui.bootstrap.pagination', [])

this.render = function() {
this.page = parseInt($scope.page, 10) || 1;
$scope.pages = this.getPages(this.page, $scope.totalPages);
if (this.page > 0 && this.page <= $scope.totalPages) {
$scope.pages = this.getPages(this.page, $scope.totalPages);
}
};

$scope.selectPage = function(page) {
Expand Down
29 changes: 29 additions & 0 deletions src/pagination/test/pagination.spec.js
Expand Up @@ -95,6 +95,26 @@ describe('pagination directive', function () {
expect(getPaginationEl(-1).text()).toBe('Next');
});

it('does not "break" when `total-items` is undefined', function() {
$rootScope.total = undefined;
$rootScope.$digest();

expect(getPaginationBarSize()).toBe(3); // Previous, 1, Next
expect(getPaginationEl(0)).toHaveClass('disabled');
expect(getPaginationEl(1)).toHaveClass('active');
expect(getPaginationEl(2)).toHaveClass('disabled');
});

it('does not "break" when `total-items` is negative', function() {
$rootScope.total = -1;
$rootScope.$digest();

expect(getPaginationBarSize()).toBe(3); // Previous, 1, Next
expect(getPaginationEl(0)).toHaveClass('disabled');
expect(getPaginationEl(1)).toHaveClass('active');
expect(getPaginationEl(2)).toHaveClass('disabled');
});

it('does not change the current page when `total-items` changes but is valid', function() {
$rootScope.currentPage = 1;
$rootScope.total = 18; // 2 pages
Expand Down Expand Up @@ -495,6 +515,15 @@ describe('pagination directive', function () {
expect($rootScope.numpg).toBe(8);
});

it('shows minimun one page if total items are not defined and does not break binding', function() {
$rootScope.total = undefined;
$rootScope.$digest();
expect($rootScope.numpg).toBe(1);

$rootScope.total = 73; // 8 pages
$rootScope.$digest();
expect($rootScope.numpg).toBe(8);
});
});

describe('setting `paginationConfig`', function() {
Expand Down

0 comments on commit 8ecf93e

Please sign in to comment.