Skip to content

Commit

Permalink
fix(useOffsetPagination): min value for pageCount should be 1 (#2001)
Browse files Browse the repository at this point in the history
Co-authored-by: Curt Grimes <curtgrimes@users.noreply.github.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people committed Aug 4, 2022
1 parent 2b869ad commit b6503dc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
31 changes: 29 additions & 2 deletions 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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/useOffsetPagination/index.ts
Expand Up @@ -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)

Expand Down

0 comments on commit b6503dc

Please sign in to comment.