Files
RenewCMS/web/src/lib/__tests__/pagination.spec.ts
T

43 lines
1.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { pageItems, pageCountFor, rangeFor } from '@/lib/pagination'
describe('pageItems', () => {
it('lists every page when there are seven or fewer', () => {
expect(pageItems(3, 7)).toEqual([1, 2, 3, 4, 5, 6, 7])
expect(pageItems(1, 1)).toEqual([1])
})
it('marks the skipped range after the first page', () => {
expect(pageItems(1, 13)).toEqual([1, 2, 'gap', 13])
})
it('marks skipped ranges on both sides of a middle page', () => {
expect(pageItems(7, 13)).toEqual([1, 'gap', 6, 7, 8, 'gap', 13])
})
it('marks the skipped range before the last page', () => {
expect(pageItems(13, 13)).toEqual([1, 'gap', 12, 13])
})
it('does not mark a gap between adjacent pages', () => {
expect(pageItems(3, 13)).toEqual([1, 2, 3, 4, 'gap', 13])
})
})
describe('pageCountFor', () => {
it('rounds up and never drops below one', () => {
expect(pageCountFor(312, 25)).toBe(13)
expect(pageCountFor(6, 25)).toBe(1)
expect(pageCountFor(0, 25)).toBe(1)
})
})
describe('rangeFor', () => {
it('computes the shown range', () => {
expect(rangeFor(1, 25, 312)).toEqual({ from: 1, to: 25 })
expect(rangeFor(7, 25, 312)).toEqual({ from: 151, to: 175 })
expect(rangeFor(13, 25, 312)).toEqual({ from: 301, to: 312 })
expect(rangeFor(1, 25, 0)).toEqual({ from: 0, to: 0 })
})
})