Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

infiniteQueryOptions.test-d.ts

66 lines2.0 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { QueryClient } from '@tanstack/query-core'
3import { queryKey } from '@tanstack/query-test-utils'
4import { createInfiniteQuery, infiniteQueryOptions } from '../src/index.js'
5import type { InfiniteData } from '@tanstack/query-core'
6​
7describe('infiniteQueryOptions', () => {
8 it('should not allow excess properties', () => {
9 const key = queryKey()
10 assertType(
11 infiniteQueryOptions({
12 queryKey: key,
13 queryFn: () => Promise.resolve('data'),
14 getNextPageParam: () => 1,
15 initialPageParam: 1,
16 // @ts-expect-error this is a good error, because stallTime does not exist!
17 stallTime: 1000,
18 }),
19 )
20 })
21​
22 it('should infer types for callbacks', () => {
23 const key = queryKey()
24 infiniteQueryOptions({
25 queryKey: key,
26 queryFn: () => Promise.resolve('data'),
27 staleTime: 1000,
28 getNextPageParam: () => 1,
29 initialPageParam: 1,
30 select: (data) => {
31 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
32 },
33 })
34 })
35​
36 it('should work when passed to createInfiniteQuery', () => {
37 const key = queryKey()
38 const options = infiniteQueryOptions({
39 queryKey: key,
40 queryFn: () => Promise.resolve('string'),
41 getNextPageParam: () => 1,
42 initialPageParam: 1,
43 })
44​
45 const query = createInfiniteQuery(() => options)
46​
47 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
48 expectTypeOf(query.data).toEqualTypeOf<
49 InfiniteData<string, unknown> | undefined
50 >()
51 })
52​
53 it('should work when passed to fetchInfiniteQuery', async () => {
54 const key = queryKey()
55 const options = infiniteQueryOptions({
56 queryKey: key,
57 queryFn: () => Promise.resolve('string'),
58 getNextPageParam: () => 1,
59 initialPageParam: 1,
60 })
61​
62 const data = await new QueryClient().fetchInfiniteQuery(options)
63​
64 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
65 })
66})