From b6503dcc7f10024496c140037343e299aaf9cba4 Mon Sep 17 00:00:00 2001 From: JD Solanki <47495003+jd-solanki@users.noreply.github.com> Date: Thu, 4 Aug 2022 11:28:10 +0530 Subject: [PATCH] fix(useOffsetPagination): min value for pageCount should be 1 (#2001) Co-authored-by: Curt Grimes Co-authored-by: Anthony Fu --- .../core/useOffsetPagination/index.test.ts | 31 +++++++++++++++++-- packages/core/useOffsetPagination/index.ts | 5 ++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/core/useOffsetPagination/index.test.ts b/packages/core/useOffsetPagination/index.test.ts index b1b02213270..39b99b7ea79 100644 --- a/packages/core/useOffsetPagination/index.test.ts +++ b/packages/core/useOffsetPagination/index.test.ts @@ -1,5 +1,5 @@ import { isRef, nextTick, ref } from 'vue-demi' -import type { UseOffsetPaginationReturn } from '.' +import type { UseOffsetPaginationOptions, UseOffsetPaginationReturn } from '.' import { useOffsetPagination } from '.' describe('useOffsetPagination', () => { @@ -91,22 +91,49 @@ describe('useOffsetPagination', () => { }) }) + describe('when total is 0', () => { + let currentPage: UseOffsetPaginationReturn['currentPage'] + + beforeEach(() => { + ({ + currentPage, + } = useOffsetPagination({ + total: 0, + })) + }) + + it('returns a currentPage of 1', () => { + expect(currentPage.value).toBe(1) + }) + }) + describe('when the page is outside of the range of possible pages', () => { let currentPage: UseOffsetPaginationReturn['currentPage'] + const page: UseOffsetPaginationOptions['page'] = ref(0) beforeEach(() => { ({ currentPage, } = useOffsetPagination({ total: 40, - page: 123456, // outside range + page, pageSize: 10, })) }) it('returns the maximum page number possible', () => { + page.value = 123456 // outside maximum range expect(currentPage.value).toBe(4) }) + + it('clamps the lower end of the range to 1', () => { + page.value = 1 + expect(currentPage.value).toBe(1) + page.value = 0 + expect(currentPage.value).toBe(1) + page.value = -1234 + expect(currentPage.value).toBe(1) + }) }) describe('when the page is a ref', () => { diff --git a/packages/core/useOffsetPagination/index.ts b/packages/core/useOffsetPagination/index.ts index 91ed045884f..c7c86862b05 100644 --- a/packages/core/useOffsetPagination/index.ts +++ b/packages/core/useOffsetPagination/index.ts @@ -64,7 +64,10 @@ export function useOffsetPagination(options: UseOffsetPaginationOptions): UseOff const currentPageSize = useClamp(pageSize, 1, Infinity) - const pageCount = computed(() => Math.ceil((unref(total)) / unref(currentPageSize))) + const pageCount = computed(() => Math.max( + 1, + Math.ceil((unref(total)) / unref(currentPageSize)), + )) const currentPage = useClamp(page, 1, pageCount)