From 87d01e698c5fbca5316887a3270fbbe9292ab815 Mon Sep 17 00:00:00 2001 From: JD Solanki Date: Wed, 27 Jul 2022 11:01:25 +0530 Subject: [PATCH 1/3] fix(useOffsetPagination): min value for pageCount should be 1 --- packages/core/useOffsetPagination/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/core/useOffsetPagination/index.ts b/packages/core/useOffsetPagination/index.ts index 91ed045884f..93dd6416f42 100644 --- a/packages/core/useOffsetPagination/index.ts +++ b/packages/core/useOffsetPagination/index.ts @@ -3,6 +3,7 @@ import { computed, isRef, reactive, unref, watch } from 'vue-demi' import { noop, syncRef } from '@vueuse/shared' import type { MaybeRef } from '@vueuse/shared' import { useClamp } from '../../math/useClamp' +import { useMin } from '../../math/useMin' export interface UseOffsetPaginationOptions { /** @@ -64,7 +65,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(() => useMin( + 1, + Math.ceil((unref(total)) / unref(currentPageSize)), + ).value) const currentPage = useClamp(page, 1, pageCount) From 827cee0b403bf9094268558d36fef963fcad5bf3 Mon Sep 17 00:00:00 2001 From: JD Solanki <47495003+jd-solanki@users.noreply.github.com> Date: Wed, 27 Jul 2022 11:54:35 +0530 Subject: [PATCH 2/3] refactor(useOffsetPagination): use Math module instead of useMin Co-authored-by: Anthony Fu --- packages/core/useOffsetPagination/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/useOffsetPagination/index.ts b/packages/core/useOffsetPagination/index.ts index 93dd6416f42..758e7f80ce0 100644 --- a/packages/core/useOffsetPagination/index.ts +++ b/packages/core/useOffsetPagination/index.ts @@ -65,10 +65,10 @@ export function useOffsetPagination(options: UseOffsetPaginationOptions): UseOff const currentPageSize = useClamp(pageSize, 1, Infinity) - const pageCount = computed(() => useMin( + const pageCount = computed(() => Math.max( 1, Math.ceil((unref(total)) / unref(currentPageSize)), - ).value) + )) const currentPage = useClamp(page, 1, pageCount) From dc585b6c9edb6e483e2441df4d032c3254d68354 Mon Sep 17 00:00:00 2001 From: JD Solanki <47495003+jd-solanki@users.noreply.github.com> Date: Mon, 1 Aug 2022 12:06:40 +0530 Subject: [PATCH 3/3] test(useOffsetPagination): add tests for new low clamp behavior for currentPage --- .../core/useOffsetPagination/index.test.ts | 31 +++++++++++++++++-- packages/core/useOffsetPagination/index.ts | 1 - 2 files changed, 29 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 758e7f80ce0..c7c86862b05 100644 --- a/packages/core/useOffsetPagination/index.ts +++ b/packages/core/useOffsetPagination/index.ts @@ -3,7 +3,6 @@ import { computed, isRef, reactive, unref, watch } from 'vue-demi' import { noop, syncRef } from '@vueuse/shared' import type { MaybeRef } from '@vueuse/shared' import { useClamp } from '../../math/useClamp' -import { useMin } from '../../math/useMin' export interface UseOffsetPaginationOptions { /**