Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isPageable method to Pager. #242

Merged
merged 3 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});
});