diff --git a/src/__tests__/operations/paginate.test.ts b/src/__tests__/operations/paginate.test.ts index 172c704..90ebd97 100644 --- a/src/__tests__/operations/paginate.test.ts +++ b/src/__tests__/operations/paginate.test.ts @@ -15,6 +15,8 @@ test('default pagination', () => { test('should reset the currentPageIndex to 1 when out of range', () => { const items = generateItems(25); + + // Page number is above the maximum let { items: processed, pagesCount, @@ -25,6 +27,7 @@ test('should reset the currentPageIndex to 1 when out of range', () => { expect(processed).toHaveLength(10); expect(processed[0]).toEqual(items[0]); + // Page number is below the minimum ({ items: processed, pagesCount, @@ -34,6 +37,17 @@ test('should reset the currentPageIndex to 1 when out of range', () => { expect(pagesCount).toEqual(3); expect(processed).toHaveLength(10); expect(processed[0]).toEqual(items[0]); + + // Page number is NaN + ({ + items: processed, + pagesCount, + actualPageIndex, + } = processItems(items, { currentPageIndex: NaN }, { pagination: {} })); + expect(actualPageIndex).toEqual(1); + expect(pagesCount).toEqual(3); + expect(processed).toHaveLength(10); + expect(processed[0]).toEqual(items[0]); }); test('displays all items of it is less than the page size', () => { diff --git a/src/operations/pagination.ts b/src/operations/pagination.ts index 34da23f..2d74d56 100644 --- a/src/operations/pagination.ts +++ b/src/operations/pagination.ts @@ -16,7 +16,7 @@ export function createPageProps( const pageSize = pagination.pageSize ?? DEFAULT_PAGE_SIZE; const pagesCount = Math.ceil(items.length / pageSize); let pageIndex = currentPageIndex ?? 1; - if (pageIndex < 1 || pageIndex > pagesCount) { + if (pageIndex < 1 || pageIndex > pagesCount || Number.isNaN(pageIndex)) { pageIndex = 1; } return { pageSize, pagesCount, pageIndex };