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β
12// Regression test for exported queryOptions inference under declaration emit.
13// TypeScript should be able to name the return type without expanding the
14// internal data tag symbols into the consumer's .d.ts output.
15export const exportedQueryOptions = queryOptions({
16 queryKey: ['invalid'],
17})
18β
19describe('queryOptions', () => {
20 it('should not allow excess properties', () => {
21 assertType(
22 queryOptions({
23 queryKey: queryKey(),
24 queryFn: () => Promise.resolve(5),
25 // @ts-expect-error this is a good error, because stallTime does not exist!
26 stallTime: 1000,
27 }),
28 )
29 })
30 it('should infer types for callbacks', () => {
31 queryOptions({
32 queryKey: queryKey(),
33 queryFn: () => Promise.resolve(5),
34 staleTime: 1000,
35 select: (data) => {
36 expectTypeOf(data).toEqualTypeOf<number>()
37 },
38 })
39 })
40 it('should work when passed to useQuery', () => {
41 const options = queryOptions({
42 queryKey: queryKey(),
43 queryFn: () => Promise.resolve(5),
44 })
45β
46 const { data } = useQuery(() => options)
47 expectTypeOf(data).toEqualTypeOf<number | undefined>()
48 })
49 it('should work when passed to query', async () => {
50 const options = queryOptions({
51 queryKey: ['key'],
52 queryFn: () => Promise.resolve(5),
53 })
54β
55 const data = await new QueryClient().query(options)
56 expectTypeOf(data).toEqualTypeOf<number>()
57 })
58 it('should work when passed to query with select', async () => {
59 const options = queryOptions({
60 queryKey: ['key'],
61 queryFn: () => Promise.resolve(5),
62 select: (data) => data.toString(),
63 })
64β
65 const data = await new QueryClient().query(options)
66 expectTypeOf(data).toEqualTypeOf<string>()
67 })
68 it('should work when passed to query with enabled: false', async () => {
69 const options = queryOptions({
70 queryKey: ['key'],
71 queryFn: () => Promise.resolve(5),
72 enabled: false,
73 })
74β
75 const data = await new QueryClient().query(options)
76 expectTypeOf(data).toEqualTypeOf<number>()
77 })
78 it('should work when passed to query with skipToken', async () => {
79 const options = queryOptions({
80 queryKey: ['key'],
81 queryFn: skipToken,
82 })
83β
84 const data = await new QueryClient().query(options)
85 expectTypeOf(data).toEqualTypeOf<unknown>()
86 })
87 it('should work when passed to fetchQuery', async () => {
88 const options = queryOptions({
89 queryKey: queryKey(),
90 queryFn: () => Promise.resolve(5),
91 })
92β
93 const data = await new QueryClient().fetchQuery(options)
94 expectTypeOf(data).toEqualTypeOf<number>()
95 })
96 it('should tag the queryKey with the result type of the QueryFn', () => {
97 const { queryKey: tagged } = queryOptions({
98 queryKey: queryKey(),
99 queryFn: () => Promise.resolve(5),
100 })
101β
102 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
103 })
104 it('should tag the queryKey even if no promise is returned', () => {
105 const { queryKey: tagged } = queryOptions({
106 queryKey: queryKey(),
107 queryFn: () => 5,
108 })
109β
110 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
111 })
112 it('should tag the queryKey with unknown if there is no queryFn', () => {
113 const { queryKey: tagged } = queryOptions({
114 queryKey: queryKey(),
115 })
116β
117 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
118 })
119 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
120 const { queryKey: tagged } = queryOptions({
121 queryKey: queryKey(),
122 queryFn: () => Promise.resolve(5),
123 select: (data) => data.toString(),
124 })
125β
126 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
127 })
128 it('should tag the queryKey with the default error type', () => {
129 const { queryKey: tagged } = queryOptions({
130 queryKey: queryKey(),
131 queryFn: () => Promise.resolve(1),
132 })
133β
134 expectTypeOf(tagged[dataTagErrorSymbol]).toEqualTypeOf<Error>()
135 })
136 it('should return the proper type when passed to getQueryData', () => {
137 const { queryKey: tagged } = queryOptions({
138 queryKey: queryKey(),
139 queryFn: () => Promise.resolve(5),
140 })
141β
142 const queryClient = new QueryClient()
143 const data = queryClient.getQueryData(tagged)
144 expectTypeOf(data).toEqualTypeOf<number | undefined>()
145 })
146 it('should return the proper type when passed to getQueryState', () => {
147 const { queryKey: tagged } = queryOptions({
148 queryKey: queryKey(),
149 queryFn: () => Promise.resolve(5),
150 })
151β
152 const queryClient = new QueryClient()
153 const state = queryClient.getQueryState(tagged)
154 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
155 })
156 it('should properly type updaterFn when passed to setQueryData', () => {
157 const { queryKey: tagged } = queryOptions({
158 queryKey: queryKey(),
159 queryFn: () => Promise.resolve(5),
160 })
161β
162 const queryClient = new QueryClient()
163 const data = queryClient.setQueryData(tagged, (prev) => {
164 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
165 return prev
166 })
167 expectTypeOf(data).toEqualTypeOf<number | undefined>()
168 })
169 it('should properly type value when passed to setQueryData', () => {
170 const { queryKey: tagged } = queryOptions({
171 queryKey: queryKey(),
172 queryFn: () => Promise.resolve(5),
173 })
174β
175 const queryClient = new QueryClient()
176β
177 // @ts-expect-error value should be a number
178 queryClient.setQueryData(tagged, '5')
179 // @ts-expect-error value should be a number
180 queryClient.setQueryData(tagged, () => '5')
181β
182 const data = queryClient.setQueryData(tagged, 5)
183 expectTypeOf(data).toEqualTypeOf<number | undefined>()
184 })
185β
186 it('should infer even if there is a conditional skipToken', () => {
187 const options = queryOptions({
188 queryKey: queryKey(),
189 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
190 })
191β
192 const queryClient = new QueryClient()
193 const data = queryClient.getQueryData(options.queryKey)
194 expectTypeOf(data).toEqualTypeOf<number | undefined>()
195 })
196β
197 it('should infer to unknown if we disable a query with just a skipToken', () => {
198 const options = queryOptions({
199 queryKey: queryKey(),
200 queryFn: skipToken,
201 })
202β
203 const queryClient = new QueryClient()
204 const data = queryClient.getQueryData(options.queryKey)
205 expectTypeOf(data).toEqualTypeOf<unknown>()
206 })
207})