From 6bc84f3208ad3fd4152d2a2e50a82aac3b65ff86 Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Thu, 21 Dec 2017 09:52:00 -0800 Subject: [PATCH] Add isPageable method to Pager. (#242) * Change Pager to use -1 to reflect a 'no items' state. * Add isPageable method to Pager. --- CHANGELOG.md | 1 + src/services/paging/pager.js | 6 ++++-- src/services/paging/pager.test.js | 30 +++++++++++++++++++++--------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a31c8b61547..46ac302c23d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ **Bug fixes** - Fix bug in `Pager` service which occurred when there were no items. [(#237)[https://github.com/elastic/eui/pull/237]] +- Add `isPageable` method to `Pager` service and set first and last page index to -1 when there are no pages. [(#242)[https://github.com/elastic/eui/pull/242]] # [`0.0.9`](https://github.com/elastic/eui/tree/v0.0.9) diff --git a/src/services/paging/pager.js b/src/services/paging/pager.js index 5342ff5ee3e..c9c5de8e14c 100644 --- a/src/services/paging/pager.js +++ b/src/services/paging/pager.js @@ -29,6 +29,8 @@ export class Pager { this.update(); }; + isPageable = () => this.firstItemIndex !== -1; + getTotalPages = () => this.totalPages; getCurrentPageIndex = () => this.currentPageIndex; @@ -58,8 +60,8 @@ export class Pager { if (this.totalItems <= 0) { this.totalPages = 0; this.currentPageIndex = 0; - this.firstItemIndex = 0; - this.lastItemIndex = 0; + this.firstItemIndex = -1; + this.lastItemIndex = -1; return; } diff --git a/src/services/paging/pager.test.js b/src/services/paging/pager.test.js index 9613fed141d..aab3b143819 100644 --- a/src/services/paging/pager.test.js +++ b/src/services/paging/pager.test.js @@ -39,16 +39,37 @@ describe('Pager', () => { }); }); + describe('isPageable', () => { + test('returns true when there are pages', () => { + expect(pager.isPageable()).toBe(true); + }); + + test('returns false when there no pages', () => { + pager.setTotalItems(0); + expect(pager.isPageable()).toBe(false); + }); + }); + describe('getFirstItemIndex', () => { test('returns first item index', () => { expect(pager.getFirstItemIndex()).toBe(3); }); + + test('defaults to -1 when there are no items', () => { + pager.setTotalItems(0); + expect(pager.getFirstItemIndex()).toBe(-1); + }); }); describe('getLastItemIndex', () => { test('returns last item index', () => { expect(pager.getLastItemIndex()).toBe(5); }); + + test('defaults to -1 when there are no items', () => { + pager.setTotalItems(0); + expect(pager.getLastItemIndex()).toBe(-1); + }); }); describe('hasNextPage', () => { @@ -140,15 +161,6 @@ describe('Pager', () => { describe('behavior', () => { describe('when there are no items', () => { - test('getFirstItemIndex defaults to 0', () => { - const pager = new Pager(0, 20); - expect(pager.getFirstItemIndex()).toBe(0); - }); - - test('getLastItemIndex defaults to 0', () => { - const pager = new Pager(0, 20); - expect(pager.getLastItemIndex()).toBe(0); - }); }); }); });