Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { QueryClient, skipToken } 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 infiniteQuery', async () => {
54 const options = infiniteQueryOptions({
55 queryKey: ['key'],
56 queryFn: () => Promise.resolve('string'),
57 getNextPageParam: () => 1,
58 initialPageParam: 1,
59 })
60β
61 const data = await new QueryClient().infiniteQuery(options)
62β
63 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
64 })
65β
66 it('should work when passed to infiniteQuery with select', async () => {
67 const options = infiniteQueryOptions({
68 queryKey: ['key'],
69 queryFn: () => Promise.resolve('string'),
70 getNextPageParam: () => 1,
71 initialPageParam: 1,
72 select: (data) => data.pages,
73 })
74β
75 const data = await new QueryClient().infiniteQuery(options)
76β
77 expectTypeOf(data).toEqualTypeOf<Array<string>>()
78 })
79β
80 it('should work when passed to infiniteQuery with enabled: false', async () => {
81 const options = infiniteQueryOptions({
82 queryKey: ['key'],
83 queryFn: () => Promise.resolve('string'),
84 getNextPageParam: () => 1,
85 initialPageParam: 1,
86 enabled: false,
87 })
88β
89 const data = await new QueryClient().infiniteQuery(options)
90β
91 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
92 })
93β
94 it('should work when passed to infiniteQuery with skipToken', async () => {
95 const options = infiniteQueryOptions({
96 queryKey: ['key'],
97 queryFn: skipToken,
98 getNextPageParam: () => 1,
99 initialPageParam: 1,
100 })
101β
102 const data = await new QueryClient().infiniteQuery(options)
103β
104 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
105 })
106β
107 it('should work when passed to fetchInfiniteQuery', async () => {
108 const key = queryKey()
109 const options = infiniteQueryOptions({
110 queryKey: key,
111 queryFn: () => Promise.resolve('string'),
112 getNextPageParam: () => 1,
113 initialPageParam: 1,
114 })
115β
116 const data = await new QueryClient().fetchInfiniteQuery(options)
117β
118 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
119 })
120})