Skip to content

Commit

Permalink
test(midway): More invalid pagination states.
Browse files Browse the repository at this point in the history
  • Loading branch information
Droogans committed Feb 18, 2014
1 parent 1faf70c commit 326aa86
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/rxPaginate/docs/rxPaginate.midway.js
Expand Up @@ -44,9 +44,25 @@ describe('rxPaginate', function () {
});
});

it('should not allow navigating `next` the last page', function () {
expect(pagination.nextPage).to.throw(pagination.NoSuchPageException);
});

it('should not allow navigating using `last` past the last page', function () {
expect(pagination.lastPage).to.throw(pagination.NoSuchPageException);
});

it('should navigate to the first page', function () {
pagination.firstPage();
expect(pagination.getCurrentPageNumber()).to.eventually.equal(1);
});

it('should not allow navigating `prev` the first page', function () {
expect(pagination.previousPage).to.throw(pagination.NoSuchPageException);
});

it('should not allow navigating using `first` before the first page', function () {
expect(pagination.firstPage).to.throw(pagination.NoSuchPageException);
});

});
49 changes: 38 additions & 11 deletions src/rxPaginate/rxPaginate.page.js
Expand Up @@ -31,17 +31,10 @@ var page = {
// Try again.
page.jumpToPage(pageNumber);
} else {
return page.getCurrentPageNumber().then(function (currentPage) {
if (_.last(pageNumbers) == currentPage) {
// We are at the last page, and we still need to go higher.
var message = pageNumber + ' exceeds max page of ' + _.last(pageNumbers);
page.NoSuchPageException.thro(message);
} else {
page.jumpToHighestAvailablePage();
// Try again.
page.jumpToPage(pageNumber);
}
});
page.checkForInvalidLastPage(pageNumber);
page.jumpToHighestAvailablePage();
// Try again.
page.jumpToPage(pageNumber);
}
} else {
// Our target page is somewhere in the available pages list.
Expand All @@ -58,6 +51,7 @@ var page = {

firstPage: {
value: function () {
this.checkForInvalidFirstPage();
return this.tblPagination.then(function (pages) {
return pages[0].then(function (firstPage) {
firstPage.click();
Expand All @@ -68,6 +62,7 @@ var page = {

previousPage: {
value: function () {
this.checkForInvalidFirstPage();
return this.tblPagination.then(function (pages) {
return pages[1].then(function (previousPage) {
previousPage.click();
Expand All @@ -78,6 +73,7 @@ var page = {

nextPage: {
value: function () {
this.checkForInvalidLastPage();
return this.tblPagination.then(function (pages) {
return pages[pages.length - 2].then(function (nextPage) {
nextPage.click();
Expand All @@ -88,6 +84,7 @@ var page = {

lastPage: {
value: function () {
this.checkForInvalidLastPage();
return this.tblPagination.then(function (pages) {
return _.last(pages).then(function (lastPage) {
lastPage.click();
Expand Down Expand Up @@ -144,6 +141,36 @@ var page = {
}
},

checkForInvalidFirstPage: {
value: function () {
var page = this;
return this.getCurrentPageNumber().then(function (currentPage) {
if (currentPage === 1) {
page.NoSuchPageException.thro('cannot navigate back past the first page.');
}
});
}
},

checkForInvalidLastPage: {
// Accepts an optional `pageNumber` argument to print to the exception
// should the `NoSuchPageException` get triggered during this call.
// Otherwise, it defaults to a generic invalid page message.
value: function (pageNumber) {
var page = this;
return this.getCurrentPageNumber().then(function (currentPage) {
pageNumber = pageNumber || 'any higher number';
return page.getPageNumbers().then(function (pageNumbers) {
if (_.last(pageNumbers) == currentPage) {
// We are at the last page, and we still need to go higher.
var message = pageNumber + ' exceeds max page of ' + _.last(pageNumbers);
page.NoSuchPageException.thro(message);
}
});
});
}
},

NoSuchPageException: {
get: function () { return new exceptions.Exception('No such page'); }
}
Expand Down

0 comments on commit 326aa86

Please sign in to comment.