Skip to content

Commit

Permalink
fix(usePagination): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen committed Mar 2, 2024
1 parent e0fd740 commit bdd360e
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 6 deletions.
138 changes: 138 additions & 0 deletions packages/core/usePagination/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,142 @@ describe('usePagination', () => {
test('should be defined', () => {
expect(usePagination).toBeDefined()
})

test('total', () => {
const { pages } = usePagination({ total: 20 })
expect(pages.value.length).toEqual(2)
})

test('pageSize', () => {
const { pages } = usePagination({ total: 20, pageSize: 5 })
expect(pages.value.length).toEqual(4)
})

test('init currentPage', () => {
const { currentPage } = usePagination({
total: 5,
pageSize: 1,
currentPage: 2,
})
expect(currentPage.value).toEqual(2)
})

test('maxPageCount', () => {
const { pages } = usePagination({ total: 5, pageSize: 1, maxPageCount: 3 })
expect(pages.value.length).toEqual(3)
})

test('ellipsis', () => {
const { pages } = usePagination({
total: 10,
pageSize: 1,
maxPageCount: 3,
ellipsis: true,
})
expect(pages.value[1].text).toEqual('...')
})

test('currentPage', () => {
const { currentPage, nextPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(currentPage.value).toEqual(1)
nextPage()
expect(currentPage.value).toEqual(2)
})

test('isFirstPage', () => {
const { isFirstPage, nextPage, previousPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(isFirstPage.value).toBeTruthy()
nextPage()
expect(isFirstPage.value).toBeFalsy()
previousPage()
expect(isFirstPage.value).toBeTruthy()
})

test('isLastPage', () => {
const { isLastPage, nextPage, previousPage, gotoPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(isLastPage.value).toBeFalsy()
gotoPage(5)
expect(isLastPage.value).toBeTruthy()
previousPage()
expect(isLastPage.value).toBeFalsy()
nextPage()
expect(isLastPage.value).toBeTruthy()
})

test('totalPage', () => {
const { totalPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(totalPage.value).toEqual(5)
})

test('gotoPage', () => {
const { gotoPage, currentPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(currentPage.value).toEqual(1)
gotoPage(3)
expect(currentPage.value).toEqual(3)
})

test('nextPage', () => {
const { gotoPage, currentPage, nextPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(currentPage.value).toEqual(1)
gotoPage(4)
expect(currentPage.value).toEqual(4)
nextPage()
expect(currentPage.value).toEqual(5)
nextPage()
expect(currentPage.value).toEqual(5)
})

test('previousPage', () => {
const { currentPage, nextPage, previousPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(currentPage.value).toEqual(1)
nextPage()
expect(currentPage.value).toEqual(2)
previousPage()
expect(currentPage.value).toEqual(1)
previousPage()
expect(currentPage.value).toEqual(1)
})

test('setPageSize', () => {
const { setPageSize, totalPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(totalPage.value).toEqual(5)
setPageSize(2)
expect(totalPage.value).toEqual(3)
})

test('gotoFirstPage and gotoLastPage', () => {
const { currentPage, gotoFirstPage, gotoLastPage } = usePagination({
total: 5,
pageSize: 1,
})
expect(currentPage.value).toEqual(1)
gotoLastPage()
expect(currentPage.value).toEqual(5)
gotoFirstPage()
expect(currentPage.value).toEqual(1)
})
})
13 changes: 7 additions & 6 deletions packages/core/usePagination/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ref, computed, type ComputedRef, type Ref } from 'vue'

interface PageItem {
text: string | number;
value: number
isCurrent: boolean
type: 'page' | 'ellipsis'
Expand All @@ -18,7 +19,7 @@ interface UsePaginationReturn {
currentPage: Ref<number>
isFirstPage: ComputedRef<boolean>
isLastPage: ComputedRef<boolean>
pages: ComputedRef<Page[]>
pages: ComputedRef<PageItem[]>
totalPage: ComputedRef<number>
gotoPage: (page: number) => void
nextPage: () => void
Expand Down Expand Up @@ -50,7 +51,7 @@ export const usePagination = (
value: index + 1,
isCurrent: index + 1 === currentPage.value,
type: 'page',
}
} as PageItem
}
if (maxPageCount % 2 === 0) {
if (
Expand All @@ -62,29 +63,29 @@ export const usePagination = (
value: index + 1,
isCurrent: index + 1 === currentPage.value,
type: 'page',
}
} as PageItem
}
return {
text: '...',
value: 0,
isCurrent: false,
type: 'ellipsis',
}
} as PageItem
}
if (Math.floor(maxPageCount / 2) + 1 === index + 1) {
return {
text: '...',
value: 0,
isCurrent: false,
type: 'ellipsis',
}
} as PageItem
}
return {
text: index + 1,
value: index + 1,
isCurrent: index + 1 === currentPage.value,
type: 'page',
}
} as PageItem
})
)

Expand Down

0 comments on commit bdd360e

Please sign in to comment.