Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.9 KB
Raw Blame
import { act, renderHook } from '@testing-library/react'
import { router } from 'kea-router'

import { initKeaTests } from '~/test/init'

import { usePagination } from './usePagination'

const DATA = ['a', 'b', 'c']

describe('usePagination', () => {
    beforeEach(() => {
        initKeaTests()
        router.actions.push('/table')
    })

    it('setCurrentPage pushes the page into the URL by default', () => {
        const { result } = renderHook(() =>
            usePagination(DATA, { controlled: true, pageSize: 1, currentPage: 1, entryCount: 3 })
        )
        act(() => result.current.setCurrentPage(2))
        expect(router.values.searchParams.page).toBe(2)
    })

    it('carries the lower-bound flag through for a controlled, capped count', () => {
        const { result } = renderHook(() =>
            usePagination(DATA, {
                controlled: true,
                pageSize: 1,
                currentPage: 1,
                entryCount: 3,
                entryCountIsLowerBound: true,
            })
        )
        expect(result.current.entryCountIsLowerBound).toBe(true)
    })

    it('setCurrentPage leaves the URL alone when useUrl is false', () => {
        const { result } = renderHook(() =>
            usePagination(DATA, { controlled: true, pageSize: 1, currentPage: 1, entryCount: 3, useUrl: false })
        )
        act(() => result.current.setCurrentPage(2))
        expect(router.values.searchParams.page).toBeUndefined()
    })

    it('paginates local data without changing the URL when useUrl is false', () => {
        const { result } = renderHook(() => usePagination(DATA, { pageSize: 1, useUrl: false }))

        expect(result.current.dataSourcePage).toEqual(['a'])
        act(() => result.current.setCurrentPage(2))

        expect(result.current.dataSourcePage).toEqual(['b'])
        expect(router.values.searchParams.page).toBeUndefined()
    })
})