Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import {
3 QueryClient,
4 dataTagErrorSymbol,
5 dataTagSymbol,
6 skipToken,
7} from '@tanstack/query-core'
8import { queryKey } from '@tanstack/query-test-utils'
9import { useQuery } from '../useQuery'
10import { queryOptions } from '../queryOptions'
11β
12describe('queryOptions', () => {
13 it('should not allow excess properties', () => {
14 assertType(
15 queryOptions({
16 queryKey: queryKey(),
17 queryFn: () => Promise.resolve(5),
18 // @ts-expect-error this is a good error, because stallTime does not exist!
19 stallTime: 1000,
20 }),
21 )
22 })
23 it('should infer types for callbacks', () => {
24 queryOptions({
25 queryKey: queryKey(),
26 queryFn: () => Promise.resolve(5),
27 staleTime: 1000,
28 select: (data) => {
29 expectTypeOf(data).toEqualTypeOf<number>()
30 },
31 })
32 })
33 it('should work when passed to useQuery', () => {
34 const options = queryOptions({
35 queryKey: queryKey(),
36 queryFn: () => Promise.resolve(5),
37 })
38β
39 const { data } = useQuery(() => options)
40 // Non-optional: `data` is a suspending read β the pre-fetch gap suspends
41 // into <Loading> instead of surfacing as `undefined`.
42 expectTypeOf(data).toEqualTypeOf<number>()
43 })
44 it('should work when passed to fetchQuery', async () => {
45 const options = queryOptions({
46 queryKey: queryKey(),
47 queryFn: () => Promise.resolve(5),
48 })
49β
50 const data = await new QueryClient().fetchQuery(options)
51 expectTypeOf(data).toEqualTypeOf<number>()
52 })
53 it('should tag the queryKey with the result type of the QueryFn', () => {
54 const { queryKey: tagged } = queryOptions({
55 queryKey: queryKey(),
56 queryFn: () => Promise.resolve(5),
57 })
58β
59 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
60 })
61 it('should tag the queryKey even if no promise is returned', () => {
62 const { queryKey: tagged } = queryOptions({
63 queryKey: queryKey(),
64 queryFn: () => 5,
65 })
66β
67 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
68 })
69 it('should tag the queryKey with unknown if there is no queryFn', () => {
70 const { queryKey: tagged } = queryOptions({
71 queryKey: queryKey(),
72 })
73β
74 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
75 })
76 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
77 const { queryKey: tagged } = queryOptions({
78 queryKey: queryKey(),
79 queryFn: () => Promise.resolve(5),
80 select: (data) => data.toString(),
81 })
82β
83 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
84 })
85 it('should tag the queryKey with the default error type', () => {
86 const { queryKey: tagged } = queryOptions({
87 queryKey: queryKey(),
88 queryFn: () => Promise.resolve(1),
89 })
90β
91 expectTypeOf(tagged[dataTagErrorSymbol]).toEqualTypeOf<Error>()
92 })
93 it('should return the proper type when passed to getQueryData', () => {
94 const { queryKey: tagged } = queryOptions({
95 queryKey: queryKey(),
96 queryFn: () => Promise.resolve(5),
97 })
98β
99 const queryClient = new QueryClient()
100 const data = queryClient.getQueryData(tagged)
101 expectTypeOf(data).toEqualTypeOf<number | undefined>()
102 })
103 it('should return the proper type when passed to getQueryState', () => {
104 const { queryKey: tagged } = queryOptions({
105 queryKey: queryKey(),
106 queryFn: () => Promise.resolve(5),
107 })
108β
109 const queryClient = new QueryClient()
110 const state = queryClient.getQueryState(tagged)
111 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
112 })
113 it('should properly type updaterFn when passed to setQueryData', () => {
114 const { queryKey: tagged } = queryOptions({
115 queryKey: queryKey(),
116 queryFn: () => Promise.resolve(5),
117 })
118β
119 const queryClient = new QueryClient()
120 const data = queryClient.setQueryData(tagged, (prev) => {
121 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
122 return prev
123 })
124 expectTypeOf(data).toEqualTypeOf<number | undefined>()
125 })
126 it('should properly type value when passed to setQueryData', () => {
127 const { queryKey: tagged } = queryOptions({
128 queryKey: queryKey(),
129 queryFn: () => Promise.resolve(5),
130 })
131β
132 const queryClient = new QueryClient()
133β
134 // @ts-expect-error value should be a number
135 queryClient.setQueryData(tagged, '5')
136 // @ts-expect-error value should be a number
137 queryClient.setQueryData(tagged, () => '5')
138β
139 const data = queryClient.setQueryData(tagged, 5)
140 expectTypeOf(data).toEqualTypeOf<number | undefined>()
141 })
142β
143 it('should infer even if there is a conditional skipToken', () => {
144 const options = queryOptions({
145 queryKey: queryKey(),
146 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
147 })
148β
149 const queryClient = new QueryClient()
150 const data = queryClient.getQueryData(options.queryKey)
151 expectTypeOf(data).toEqualTypeOf<number | undefined>()
152 })
153β
154 it('should infer to unknown if we disable a query with just a skipToken', () => {
155 const options = queryOptions({
156 queryKey: queryKey(),
157 queryFn: skipToken,
158 })
159β
160 const queryClient = new QueryClient()
161 const data = queryClient.getQueryData(options.queryKey)
162 expectTypeOf(data).toEqualTypeOf<unknown>()
163 })
164})