Skip to content

Commit 86740ae

Browse files
authored
test(vue-query/queryOptions): add type tests for 'useQueries', 'query' with 'enabled: false' and 'skipToken', and undefined 'initialData' (#12309)
1 parent 394ea29 commit 86740ae

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

‎packages/vue-query/src/__tests__/queryOptions.test-d.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { dataTagSymbol, skipToken } from '@tanstack/query-core'
44
import { createKey } from '@tanstack/query-test-utils'
55
import { QueryClient } from '../queryClient'
66
import { queryOptions } from '../queryOptions'
7+
import { useQueries } from '../useQueries'
78
import { useQuery } from '../useQuery'
89
import type { InitialDataFunction } from '@tanstack/query-core'
910

@@ -54,6 +55,17 @@ describe('queryOptions', () => {
5455
expectTypeOf(data).toEqualTypeOf<number | undefined>()
5556
})
5657

58+
it('should work when passed to useQueries', () => {
59+
const options = queryOptions({
60+
queryKey: createKey(),
61+
queryFn: () => Promise.resolve(5),
62+
})
63+
64+
const { value: queriesState } = useQueries({ queries: [options] })
65+
66+
expectTypeOf(queriesState[0].data).toEqualTypeOf<number | undefined>()
67+
})
68+
5769
it('should work when passed to query', async () => {
5870
const options = queryOptions({
5971
queryKey: ['key'],
@@ -75,6 +87,27 @@ describe('queryOptions', () => {
7587
expectTypeOf(data).toEqualTypeOf<string>()
7688
})
7789

90+
it('should work when passed to query with enabled: false', async () => {
91+
const options = queryOptions({
92+
queryKey: ['key'],
93+
queryFn: () => Promise.resolve(5),
94+
enabled: false,
95+
})
96+
97+
const data = await new QueryClient().query(options)
98+
expectTypeOf(data).toEqualTypeOf<number>()
99+
})
100+
101+
it('should work when passed to query with skipToken', async () => {
102+
const options = queryOptions({
103+
queryKey: ['key'],
104+
queryFn: skipToken,
105+
})
106+
107+
const data = await new QueryClient().query(options)
108+
expectTypeOf(data).toEqualTypeOf<unknown>()
109+
})
110+
78111
it('should tag the queryKey with the result type of the QueryFn', () => {
79112
const key = createKey()
80113
const { queryKey: tagged } = queryOptions({
@@ -327,6 +360,26 @@ describe('queryOptions', () => {
327360
>()
328361
})
329362

363+
it('should allow undefined response in initialData', () => {
364+
assertType((id: string | null) =>
365+
queryOptions({
366+
queryKey: [...createKey(), id],
367+
queryFn: () =>
368+
Promise.resolve({
369+
id: '1',
370+
title: 'Do Laundry',
371+
}),
372+
initialData: () =>
373+
!id
374+
? undefined
375+
: {
376+
id,
377+
title: 'Initial Data',
378+
},
379+
}),
380+
)
381+
})
382+
330383
it('should allow accessing queryFn and other properties on the returned options object', () => {
331384
const options = queryOptions({
332385
queryKey: createKey(),

0 commit comments

Comments
 (0)