import { act, render, waitFor } from '@testing-library/react'
import { createRef } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import PaginatedList, { type PaginatedListHandle } from '@/components/paginated-list/PaginatedListImpl'
const renderWithQuery = (ui: React.ReactElement) => {
const client = new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
})
return render({ui})
}
const fetchPaginatedList = vi.fn()
const replace = vi.fn()
vi.mock('next/navigation', () => ({
usePathname: () => '/test',
useRouter: () => ({ replace }),
}))
vi.mock('@/hooks/useQueryParams', () => ({ default: () => ({}) }))
vi.mock('@/components/paginated-list/PaginatedListFilterModal', () => ({ default: () => null }))
vi.mock('@/components/paginated-list/PaginatedListPagination', () => ({
default: () => null,
ROWS_PER_PAGE_OPTIONS: [{ code: '20', name: '20' }],
}))
vi.mock('@/components/paginated-list/PaginatedListTable', () => ({ default: () => null }))
vi.mock('@/components/paginated-list/PaginatedListToolbar', () => ({ default: () => null }))
vi.mock('@/components/paginated-list/paginatedListApi', () => ({
fetchPaginatedList: (...args: unknown[]) => fetchPaginatedList(...args),
fetchPaginatedListExport: vi.fn(),
}))
describe('PaginatedList refresh handle', () => {
beforeEach(() => {
replace.mockClear()
fetchPaginatedList.mockReset()
fetchPaginatedList.mockResolvedValue({ items: [], pagination: { page: 1, pageSize: 20, totalItems: 0 } })
})
it('fetches the current list again when refresh is called', async () => {
const ref = createRef()
renderWithQuery(
)
await waitFor(() => {
expect(fetchPaginatedList).toHaveBeenCalled()
})
const initialCalls = fetchPaginatedList.mock.calls.length
act(() => ref.current?.refresh())
await waitFor(() => {
expect(fetchPaginatedList.mock.calls.length).toBeGreaterThan(initialCalls)
})
})
})