Skip to content

Commit

Permalink
fix: Fix handling of NaN for currentPageIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
timogasda committed Jan 3, 2024
1 parent 3c73594 commit bc4c109
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/__tests__/operations/paginate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/operations/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createPageProps<T>(
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 };
Expand Down

0 comments on commit bc4c109

Please sign in to comment.