Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useOffsetPagination): min value for pageCount should be 1 #2001

Merged
merged 3 commits into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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