import { assertType, describe, expectTypeOf, it } from 'vitest' import { QueryClient, skipToken } from '@tanstack/query-core' import { queryKey } from '@tanstack/query-test-utils' import { createInfiniteQuery, infiniteQueryOptions } from '../src/index.js' import type { InfiniteData } from '@tanstack/query-core' describe('infiniteQueryOptions', () => { it('should not allow excess properties', () => { const key = queryKey() assertType( infiniteQueryOptions({ queryKey: key, queryFn: () => Promise.resolve('data'), getNextPageParam: () => 1, initialPageParam: 1, // @ts-expect-error this is a good error, because stallTime does not exist! stallTime: 1000, }), ) }) it('should infer types for callbacks', () => { const key = queryKey() infiniteQueryOptions({ queryKey: key, queryFn: () => Promise.resolve('data'), staleTime: 1000, getNextPageParam: () => 1, initialPageParam: 1, select: (data) => { expectTypeOf(data).toEqualTypeOf>() }, }) }) it('should work when passed to createInfiniteQuery', () => { const key = queryKey() const options = infiniteQueryOptions({ queryKey: key, queryFn: () => Promise.resolve('string'), getNextPageParam: () => 1, initialPageParam: 1, }) const query = createInfiniteQuery(() => options) // known issue: type of pageParams is unknown when returned from useInfiniteQuery expectTypeOf(query.data).toEqualTypeOf< InfiniteData | undefined >() }) it('should work when passed to createInfiniteQuery with initialData', () => { const key = queryKey() const options = infiniteQueryOptions({ queryKey: key, queryFn: () => Promise.resolve('string'), getNextPageParam: () => 1, initialPageParam: 1, initialData: { pages: ['string'], pageParams: [1] }, }) const query = createInfiniteQuery(() => options) // known issue: type of pageParams is unknown when returned from createInfiniteQuery expectTypeOf(query.data).toEqualTypeOf>() }) it('should work when passed to infiniteQuery', async () => { const options = infiniteQueryOptions({ queryKey: ['key'], queryFn: () => Promise.resolve('string'), getNextPageParam: () => 1, initialPageParam: 1, }) const data = await new QueryClient().infiniteQuery(options) expectTypeOf(data).toEqualTypeOf>() }) it('should work when passed to infiniteQuery with select', async () => { const options = infiniteQueryOptions({ queryKey: ['key'], queryFn: () => Promise.resolve('string'), getNextPageParam: () => 1, initialPageParam: 1, select: (data) => data.pages, }) const data = await new QueryClient().infiniteQuery(options) expectTypeOf(data).toEqualTypeOf>() }) it('should work when passed to infiniteQuery with enabled: false', async () => { const options = infiniteQueryOptions({ queryKey: ['key'], queryFn: () => Promise.resolve('string'), getNextPageParam: () => 1, initialPageParam: 1, enabled: false, }) const data = await new QueryClient().infiniteQuery(options) expectTypeOf(data).toEqualTypeOf>() }) it('should work when passed to infiniteQuery with skipToken', async () => { const options = infiniteQueryOptions({ queryKey: ['key'], queryFn: skipToken, getNextPageParam: () => 1, initialPageParam: 1, }) const data = await new QueryClient().infiniteQuery(options) expectTypeOf(data).toEqualTypeOf>() }) it('should work when passed to fetchInfiniteQuery', async () => { const key = queryKey() const options = infiniteQueryOptions({ queryKey: key, queryFn: () => Promise.resolve('string'), getNextPageParam: () => 1, initialPageParam: 1, }) // grandfathered direct test // eslint-disable-next-line no-restricted-syntax const data = await new QueryClient().fetchInfiniteQuery(options) expectTypeOf(data).toEqualTypeOf>() }) })