Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queryOptions.test-d.tsx

208 lines6.6 KB

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 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
94 const data = await new QueryClient().fetchQuery(options)
95 expectTypeOf(data).toEqualTypeOf<number>()
96 })
97 it('should tag the queryKey with the result type of the QueryFn', () => {
98 const { queryKey: tagged } = queryOptions({
99 queryKey: queryKey(),
100 queryFn: () => Promise.resolve(5),
101 })
102​
103 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
104 })
105 it('should tag the queryKey even if no promise is returned', () => {
106 const { queryKey: tagged } = queryOptions({
107 queryKey: queryKey(),
108 queryFn: () => 5,
109 })
110​
111 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
112 })
113 it('should tag the queryKey with unknown if there is no queryFn', () => {
114 const { queryKey: tagged } = queryOptions({
115 queryKey: queryKey(),
116 })
117​
118 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
119 })
120 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
121 const { queryKey: tagged } = queryOptions({
122 queryKey: queryKey(),
123 queryFn: () => Promise.resolve(5),
124 select: (data) => data.toString(),
125 })
126​
127 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
128 })
129 it('should tag the queryKey with the default error type', () => {
130 const { queryKey: tagged } = queryOptions({
131 queryKey: queryKey(),
132 queryFn: () => Promise.resolve(1),
133 })
134​
135 expectTypeOf(tagged[dataTagErrorSymbol]).toEqualTypeOf<Error>()
136 })
137 it('should return the proper type when passed to getQueryData', () => {
138 const { queryKey: tagged } = queryOptions({
139 queryKey: queryKey(),
140 queryFn: () => Promise.resolve(5),
141 })
142​
143 const queryClient = new QueryClient()
144 const data = queryClient.getQueryData(tagged)
145 expectTypeOf(data).toEqualTypeOf<number | undefined>()
146 })
147 it('should return the proper type when passed to getQueryState', () => {
148 const { queryKey: tagged } = queryOptions({
149 queryKey: queryKey(),
150 queryFn: () => Promise.resolve(5),
151 })
152​
153 const queryClient = new QueryClient()
154 const state = queryClient.getQueryState(tagged)
155 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
156 })
157 it('should properly type updaterFn when passed to setQueryData', () => {
158 const { queryKey: tagged } = queryOptions({
159 queryKey: queryKey(),
160 queryFn: () => Promise.resolve(5),
161 })
162​
163 const queryClient = new QueryClient()
164 const data = queryClient.setQueryData(tagged, (prev) => {
165 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
166 return prev
167 })
168 expectTypeOf(data).toEqualTypeOf<number | undefined>()
169 })
170 it('should properly type value when passed to setQueryData', () => {
171 const { queryKey: tagged } = queryOptions({
172 queryKey: queryKey(),
173 queryFn: () => Promise.resolve(5),
174 })
175​
176 const queryClient = new QueryClient()
177​
178 // @ts-expect-error value should be a number
179 queryClient.setQueryData(tagged, '5')
180 // @ts-expect-error value should be a number
181 queryClient.setQueryData(tagged, () => '5')
182​
183 const data = queryClient.setQueryData(tagged, 5)
184 expectTypeOf(data).toEqualTypeOf<number | undefined>()
185 })
186​
187 it('should infer even if there is a conditional skipToken', () => {
188 const options = queryOptions({
189 queryKey: queryKey(),
190 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
191 })
192​
193 const queryClient = new QueryClient()
194 const data = queryClient.getQueryData(options.queryKey)
195 expectTypeOf(data).toEqualTypeOf<number | undefined>()
196 })
197​
198 it('should infer to unknown if we disable a query with just a skipToken', () => {
199 const options = queryOptions({
200 queryKey: queryKey(),
201 queryFn: skipToken,
202 })
203​
204 const queryClient = new QueryClient()
205 const data = queryClient.getQueryData(options.queryKey)
206 expectTypeOf(data).toEqualTypeOf<unknown>()
207 })
208})