Skip to content

Commit

Permalink
Add isPageable method to Pager. (#242)
Browse files Browse the repository at this point in the history
* Change Pager to use -1 to reflect a 'no items' state.
* Add isPageable method to Pager.
  • Loading branch information
cjcenizal authored Dec 21, 2017
1 parent 4922884 commit 6bc84f3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions src/services/paging/pager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class Pager {
this.update();
};

isPageable = () => this.firstItemIndex !== -1;

getTotalPages = () => this.totalPages;

getCurrentPageIndex = () => this.currentPageIndex;
Expand Down Expand Up @@ -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;
}

Expand Down
30 changes: 21 additions & 9 deletions src/services/paging/pager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});
});

0 comments on commit 6bc84f3

Please sign in to comment.