From 4d15df2b4853c25a32d244c5131ee9c260ecf4cf Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Wed, 6 Mar 2019 14:30:21 -0500 Subject: [PATCH] fix pagination text when items displayed matches number of items per page --- __tests__/components/Pagination.test.jsx | 57 ++++++++++++++++++++++++ src/components/Pagination/Pagination.jsx | 9 +++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/__tests__/components/Pagination.test.jsx b/__tests__/components/Pagination.test.jsx index b2ca6727e8..552642665c 100644 --- a/__tests__/components/Pagination.test.jsx +++ b/__tests__/components/Pagination.test.jsx @@ -119,6 +119,63 @@ describe('', () => { pageSizeDropdownItems.at(1).simulate('click'); }); + test('itemCount displays correctly', () => { + const onSetPage = jest.fn(); + + pagination = mount( + + + + ); + const itemCount = pagination.find('.awx-pagination__item-count'); + expect(itemCount.text()).toEqual('Items 1 – 5 of 7'); + }); + + test('itemCount matching pageSize displays correctly', () => { + const onSetPage = jest.fn(); + + pagination = mount( + + + + ); + const itemCount = pagination.find('.awx-pagination__item-count'); + expect(itemCount.text()).toEqual('Items 1 – 5 of 5'); + }); + + test('itemCount less than pageSize displays correctly', () => { + const onSetPage = jest.fn(); + + pagination = mount( + + + + ); + const itemCount = pagination.find('.awx-pagination__item-count'); + expect(itemCount.text()).toEqual('Items 1 – 3 of 3'); + }); + test('submit a new page by typing in input works', () => { const textInputSelector = '.awx-pagination__page-input.pf-c-form-control'; const submitFormSelector = '.awx-pagination__page-input-form'; diff --git a/src/components/Pagination/Pagination.jsx b/src/components/Pagination/Pagination.jsx index 98a03b8e8c..a3eba1ca3e 100644 --- a/src/components/Pagination/Pagination.jsx +++ b/src/components/Pagination/Pagination.jsx @@ -116,7 +116,12 @@ class Pagination extends Component { const isOnFirst = page === 1; const isOnLast = page === pageCount; - const itemCount = isOnLast ? count % page_size : page_size; + let itemCount; + if (!isOnLast || count === page_size) { + itemCount = page_size; + } else { + itemCount = count % page_size; + } const itemMin = ((page - 1) * page_size) + 1; const itemMax = itemMin + itemCount - 1; @@ -154,7 +159,7 @@ class Pagination extends Component { )}
- {`Items ${itemMin} - ${itemMax} of ${count}`} + {`Items ${itemMin} – ${itemMax} of ${count}`}
{pageCount !== 1 && (