11import { assertType , describe , expectTypeOf , it , test } from 'vitest'
2+ import { queryKey } from '@tanstack/query-test-utils'
23import { QueryClient , dataTagSymbol } from '@tanstack/query-core'
34import { infiniteQueryOptions } from '../infinite-query-options'
45import { injectInfiniteQuery } from '../inject-infinite-query'
@@ -11,9 +12,10 @@ import type {
1112
1213describe ( 'infiniteQueryOptions' , ( ) => {
1314 it ( 'should not allow excess properties' , ( ) => {
15+ const key = queryKey ( )
1416 assertType (
1517 infiniteQueryOptions ( {
16- queryKey : [ ' key' ] ,
18+ queryKey : key ,
1719 queryFn : ( ) => Promise . resolve ( 'data' ) ,
1820 getNextPageParam : ( ) => 1 ,
1921 initialPageParam : 1 ,
@@ -23,8 +25,9 @@ describe('infiniteQueryOptions', () => {
2325 )
2426 } )
2527 it ( 'should infer types for callbacks' , ( ) => {
28+ const key = queryKey ( )
2629 infiniteQueryOptions ( {
27- queryKey : [ ' key' ] ,
30+ queryKey : key ,
2831 queryFn : ( ) => Promise . resolve ( 'data' ) ,
2932 staleTime : 1000 ,
3033 getNextPageParam : ( ) => 1 ,
@@ -35,8 +38,9 @@ describe('infiniteQueryOptions', () => {
3538 } )
3639 } )
3740 it ( 'should work when passed to useInfiniteQuery' , ( ) => {
41+ const key = queryKey ( )
3842 const options = infiniteQueryOptions ( {
39- queryKey : [ ' key' ] ,
43+ queryKey : key ,
4044 queryFn : ( ) => Promise . resolve ( 'string' ) ,
4145 getNextPageParam : ( ) => 1 ,
4246 initialPageParam : 1 ,
@@ -51,8 +55,9 @@ describe('infiniteQueryOptions', () => {
5155 } )
5256
5357 it ( 'should work when passed to fetchInfiniteQuery' , async ( ) => {
58+ const key = queryKey ( )
5459 const options = infiniteQueryOptions ( {
55- queryKey : [ ' key' ] ,
60+ queryKey : key ,
5661 queryFn : ( ) => Promise . resolve ( 'string' ) ,
5762 getNextPageParam : ( ) => 1 ,
5863 initialPageParam : 1 ,
@@ -63,61 +68,66 @@ describe('infiniteQueryOptions', () => {
6368 expectTypeOf ( data ) . toEqualTypeOf < InfiniteData < string , number > > ( )
6469 } )
6570 it ( 'should tag the queryKey with the result type of the QueryFn' , ( ) => {
66- const { queryKey } = infiniteQueryOptions ( {
67- queryKey : [ 'key' ] ,
71+ const key = queryKey ( )
72+ const { queryKey : tagged } = infiniteQueryOptions ( {
73+ queryKey : key ,
6874 queryFn : ( ) => Promise . resolve ( 'string' ) ,
6975 getNextPageParam : ( ) => 1 ,
7076 initialPageParam : 1 ,
7177 } )
7278
73- expectTypeOf ( queryKey [ dataTagSymbol ] ) . toEqualTypeOf < InfiniteData < string > > ( )
79+ expectTypeOf ( tagged [ dataTagSymbol ] ) . toEqualTypeOf < InfiniteData < string > > ( )
7480 } )
7581 it ( 'should tag the queryKey even if no promise is returned' , ( ) => {
76- const { queryKey } = infiniteQueryOptions ( {
77- queryKey : [ 'key' ] ,
82+ const key = queryKey ( )
83+ const { queryKey : tagged } = infiniteQueryOptions ( {
84+ queryKey : key ,
7885 queryFn : ( ) => 'string' ,
7986 getNextPageParam : ( ) => 1 ,
8087 initialPageParam : 1 ,
8188 } )
8289
83- expectTypeOf ( queryKey [ dataTagSymbol ] ) . toEqualTypeOf < InfiniteData < string > > ( )
90+ expectTypeOf ( tagged [ dataTagSymbol ] ) . toEqualTypeOf < InfiniteData < string > > ( )
8491 } )
8592 it ( 'should tag the queryKey with the result type of the QueryFn if select is used' , ( ) => {
86- const { queryKey } = infiniteQueryOptions ( {
87- queryKey : [ 'key' ] ,
93+ const key = queryKey ( )
94+ const { queryKey : tagged } = infiniteQueryOptions ( {
95+ queryKey : key ,
8896 queryFn : ( ) => Promise . resolve ( 'string' ) ,
8997 select : ( data ) => data . pages ,
9098 getNextPageParam : ( ) => 1 ,
9199 initialPageParam : 1 ,
92100 } )
93101
94- expectTypeOf ( queryKey [ dataTagSymbol ] ) . toEqualTypeOf < InfiniteData < string > > ( )
102+ expectTypeOf ( tagged [ dataTagSymbol ] ) . toEqualTypeOf < InfiniteData < string > > ( )
95103 } )
96104 it ( 'should return the proper type when passed to getQueryData' , ( ) => {
97- const { queryKey } = infiniteQueryOptions ( {
98- queryKey : [ 'key' ] ,
105+ const key = queryKey ( )
106+ const { queryKey : tagged } = infiniteQueryOptions ( {
107+ queryKey : key ,
99108 queryFn : ( ) => Promise . resolve ( 'string' ) ,
100109 getNextPageParam : ( ) => 1 ,
101110 initialPageParam : 1 ,
102111 } )
103112
104113 const queryClient = new QueryClient ( )
105- const data = queryClient . getQueryData ( queryKey )
114+ const data = queryClient . getQueryData ( tagged )
106115
107116 expectTypeOf ( data ) . toEqualTypeOf <
108117 InfiniteData < string , unknown > | undefined
109118 > ( )
110119 } )
111120 it ( 'should properly type when passed to setQueryData' , ( ) => {
112- const { queryKey } = infiniteQueryOptions ( {
113- queryKey : [ 'key' ] ,
121+ const key = queryKey ( )
122+ const { queryKey : tagged } = infiniteQueryOptions ( {
123+ queryKey : key ,
114124 queryFn : ( ) => Promise . resolve ( 'string' ) ,
115125 getNextPageParam : ( ) => 1 ,
116126 initialPageParam : 1 ,
117127 } )
118128
119129 const queryClient = new QueryClient ( )
120- const data = queryClient . setQueryData ( queryKey , ( prev ) => {
130+ const data = queryClient . setQueryData ( tagged , ( prev ) => {
121131 expectTypeOf ( prev ) . toEqualTypeOf <
122132 InfiniteData < string , unknown > | undefined
123133 > ( )
@@ -130,9 +140,10 @@ describe('infiniteQueryOptions', () => {
130140 } )
131141
132142 test ( 'should not be allowed to be passed to non-infinite query functions' , ( ) => {
143+ const key = queryKey ( )
133144 const queryClient = new QueryClient ( )
134145 const options = infiniteQueryOptions ( {
135- queryKey : [ ' key' ] ,
146+ queryKey : key ,
136147 queryFn : ( ) => Promise . resolve ( 'string' ) ,
137148 getNextPageParam : ( ) => 1 ,
138149 initialPageParam : 1 ,
@@ -156,9 +167,10 @@ describe('infiniteQueryOptions', () => {
156167 } )
157168
158169 test ( 'allow optional initialData function' , ( ) => {
170+ const key = queryKey ( )
159171 const initialData : { example : boolean } | undefined = { example : true }
160172 const queryOptions = infiniteQueryOptions ( {
161- queryKey : [ 'example' ] ,
173+ queryKey : key ,
162174 queryFn : ( ) => initialData ,
163175 initialData : initialData
164176 ? ( ) => ( { pages : [ initialData ] , pageParams : [ ] } )
@@ -174,9 +186,10 @@ describe('infiniteQueryOptions', () => {
174186 } )
175187
176188 test ( 'allow optional initialData object' , ( ) => {
189+ const key = queryKey ( )
177190 const initialData : { example : boolean } | undefined = { example : true }
178191 const queryOptions = infiniteQueryOptions ( {
179- queryKey : [ 'example' ] ,
192+ queryKey : key ,
180193 queryFn : ( ) => initialData ,
181194 initialData : initialData
182195 ? { pages : [ initialData ] , pageParams : [ ] }
0 commit comments