11import { assertType , describe , expectTypeOf , it } from 'vitest'
22import { computed , reactive , ref } from 'vue-demi'
3- import { dataTagSymbol } from '@tanstack/query-core'
3+ import { dataTagSymbol , skipToken } from '@tanstack/query-core'
44import { queryKey } from '@tanstack/query-test-utils'
55import { QueryClient } from '../queryClient'
66import { queryOptions } from '../queryOptions'
@@ -11,9 +11,9 @@ describe('queryOptions', () => {
1111 const key = queryKey ( )
1212 assertType (
1313 queryOptions ( {
14- // @ts -expect-error this is a good error, because stallTime does not exist!
1514 queryKey : key ,
1615 queryFn : ( ) => Promise . resolve ( 5 ) ,
16+ // @ts -expect-error this is a good error, because stallTime does not exist!
1717 stallTime : 1000 ,
1818 } ) ,
1919 )
@@ -231,27 +231,45 @@ describe('queryOptions', () => {
231231 expectTypeOf ( data ) . toEqualTypeOf < number > ( )
232232 } )
233233
234- it ( 'should allow accessing queryFn and other properties on the returned options object ' , ( ) => {
234+ it ( 'should keep the tagged queryKey accessible and be spreadable into useQuery ' , ( ) => {
235235 const options = queryOptions ( {
236236 queryKey : queryKey ( ) ,
237- queryFn : ( ) => Promise . resolve ( [ ] ) ,
237+ queryFn : ( ) => Promise . resolve ( 5 ) ,
238238 } )
239239
240- expectTypeOf ( options . queryFn ) . not . toBeUndefined ( )
241240 expectTypeOf ( options . queryKey ) . not . toBeUndefined ( )
242- expectTypeOf ( options . staleTime ) . not . toBeUndefined ( )
241+
242+ const { data } = reactive (
243+ useQuery ( {
244+ ...options ,
245+ select : ( d ) => d . toString ( ) ,
246+ } ) ,
247+ )
248+ expectTypeOf ( data ) . toEqualTypeOf < string | undefined > ( )
243249 } )
244250
245- it ( 'should allow accessing queryFn and other properties on the returned options when used with getter ' , ( ) => {
251+ it ( 'should allow a getter returning options to be passed to useQuery ' , ( ) => {
246252 const options = queryOptions ( ( ) => ( {
247253 queryKey : queryKey ( ) ,
248- queryFn : ( ) => Promise . resolve ( [ ] ) ,
254+ queryFn : ( ) => Promise . resolve ( 5 ) ,
249255 } ) )
250256
251- const resolvedGetter = options ( )
257+ const { data } = reactive ( useQuery ( options ) )
258+ expectTypeOf ( data ) . toEqualTypeOf < number | undefined > ( )
259+ } )
260+
261+ it ( 'should allow a computed queryFn returning skipToken, matching useQuery' , ( ) => {
262+ const enabled = ref ( false )
263+
264+ const options = queryOptions ( {
265+ queryKey : queryKey ( ) ,
266+ queryFn : computed ( ( ) =>
267+ enabled . value ? ( ) => Promise . resolve ( 5 ) : skipToken ,
268+ ) ,
269+ } )
252270
253- expectTypeOf ( resolvedGetter . queryFn ) . not . toBeUndefined ( )
254- expectTypeOf ( resolvedGetter . queryKey ) . not . toBeUndefined ( )
271+ const { data } = reactive ( useQuery ( options ) )
272+ expectTypeOf ( data ) . toEqualTypeOf < number | undefined > ( )
255273 } )
256274
257275 it ( 'should allow computed ref as enabled property' , ( ) => {
@@ -322,14 +340,15 @@ describe('queryOptions', () => {
322340 expectTypeOf ( options . queryKey ) . not . toBeUndefined ( )
323341 } )
324342
325- it ( 'should allow getter function as queryKey' , ( ) => {
343+ it ( 'should not allow a per-field getter as queryKey, matching useQuery (use computed/ref, or a getter for the whole options) ' , ( ) => {
326344 const id = ref < string | null > ( '1' )
327345
328- const options = queryOptions ( {
329- queryKey : ( ) => [ 'foo' , id . value ] as const ,
330- queryFn : ( ) => Promise . resolve ( { id : '1' } ) ,
331- } )
332-
333- expectTypeOf ( options . queryKey ) . not . toBeUndefined ( )
346+ assertType (
347+ queryOptions ( {
348+ // @ts -expect-error queryKey must be a plain key / ref / computed, not a per-field getter
349+ queryKey : ( ) => [ 'foo' , id . value ] as const ,
350+ queryFn : ( ) => Promise . resolve ( { id : '1' } ) ,
351+ } ) ,
352+ )
334353 } )
335354} )
0 commit comments