Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import {
3 QueriesObserver,
4 QueryClient,
5 dataTagSymbol,
6 skipToken,
7} from '@tanstack/query-core'
8import { queryKey } from '@tanstack/query-test-utils'
9import { createQueries, queryOptions } from '../src/index.js'
10import type { QueryObserverResult } from '@tanstack/query-core'
11β
12describe('queryOptions', () => {
13 it('should not allow excess properties', () => {
14 const key = queryKey()
15 assertType(
16 queryOptions({
17 queryKey: key,
18 queryFn: () => Promise.resolve(5),
19 // @ts-expect-error this is a good error, because stallTime does not exist!
20 stallTime: 1000,
21 }),
22 )
23 })
24β
25 it('should infer types for callbacks', () => {
26 const key = queryKey()
27 queryOptions({
28 queryKey: key,
29 queryFn: () => Promise.resolve(5),
30 staleTime: 1000,
31 select: (data) => {
32 expectTypeOf(data).toEqualTypeOf<number>()
33 },
34 })
35 })
36β
37 it('should work when passed to fetchQuery', async () => {
38 const key = queryKey()
39 const options = queryOptions({
40 queryKey: key,
41 queryFn: () => Promise.resolve(5),
42 })
43β
44 const data = await new QueryClient().fetchQuery(options)
45 expectTypeOf(data).toEqualTypeOf<number>()
46 })
47β
48 it('should work when passed to createQueries', () => {
49 const key = queryKey()
50 const options = queryOptions({
51 queryKey: key,
52 queryFn: () => Promise.resolve(5),
53 })
54β
55 const queries = createQueries(() => ({
56 queries: [options],
57 }))
58β
59 expectTypeOf(queries[0].data).toEqualTypeOf<number | undefined>()
60 })
61β
62 it('should tag the queryKey with the result type of the QueryFn', () => {
63 const key = queryKey()
64 const { queryKey: tagged } = queryOptions({
65 queryKey: key,
66 queryFn: () => Promise.resolve(5),
67 })
68β
69 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
70 })
71β
72 it('should tag the queryKey even if no promise is returned', () => {
73 const key = queryKey()
74 const { queryKey: tagged } = queryOptions({
75 queryKey: key,
76 queryFn: () => 5,
77 })
78β
79 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
80 })
81β
82 it('should tag the queryKey with unknown if there is no queryFn', () => {
83 const key = queryKey()
84 const { queryKey: tagged } = queryOptions({
85 queryKey: key,
86 })
87β
88 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
89 })
90β
91 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
92 const key = queryKey()
93 const { queryKey: tagged } = queryOptions({
94 queryKey: key,
95 queryFn: () => Promise.resolve(5),
96 select: (data) => data.toString(),
97 })
98β
99 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
100 })
101β
102 it('should return the proper type when passed to getQueryData', () => {
103 const key = queryKey()
104 const { queryKey: tagged } = queryOptions({
105 queryKey: key,
106 queryFn: () => Promise.resolve(5),
107 })
108β
109 const queryClient = new QueryClient()
110 const data = queryClient.getQueryData(tagged)
111 expectTypeOf(data).toEqualTypeOf<number | undefined>()
112 })
113β
114 it('should return the proper type when passed to getQueryState', () => {
115 const key = queryKey()
116 const { queryKey: tagged } = queryOptions({
117 queryKey: key,
118 queryFn: () => Promise.resolve(5),
119 })
120β
121 const queryClient = new QueryClient()
122 const state = queryClient.getQueryState(tagged)
123 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
124 })
125β
126 it('should properly type updaterFn when passed to setQueryData', () => {
127 const key = queryKey()
128 const { queryKey: tagged } = queryOptions({
129 queryKey: key,
130 queryFn: () => Promise.resolve(5),
131 })
132β
133 const queryClient = new QueryClient()
134 const data = queryClient.setQueryData(tagged, (prev) => {
135 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
136 return prev
137 })
138 expectTypeOf(data).toEqualTypeOf<number | undefined>()
139 })
140β
141 it('should properly type value when passed to setQueryData', () => {
142 const key = queryKey()
143 const { queryKey: tagged } = queryOptions({
144 queryKey: key,
145 queryFn: () => Promise.resolve(5),
146 })
147β
148 const queryClient = new QueryClient()
149β
150 // @ts-expect-error value should be a number
151 queryClient.setQueryData(tagged, '5')
152 // @ts-expect-error value should be a number
153 queryClient.setQueryData(tagged, () => '5')
154β
155 const data = queryClient.setQueryData(tagged, 5)
156 expectTypeOf(data).toEqualTypeOf<number | undefined>()
157 })
158β
159 it('should infer even if there is a conditional skipToken', () => {
160 const key = queryKey()
161 const options = queryOptions({
162 queryKey: key,
163 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
164 })
165β
166 const queryClient = new QueryClient()
167 const data = queryClient.getQueryData(options.queryKey)
168 expectTypeOf(data).toEqualTypeOf<number | undefined>()
169 })
170β
171 it('should infer to unknown if we disable a query with just a skipToken', () => {
172 const key = queryKey()
173 const options = queryOptions({
174 queryKey: key,
175 queryFn: skipToken,
176 })
177β
178 const queryClient = new QueryClient()
179 const data = queryClient.getQueryData(options.queryKey)
180 expectTypeOf(data).toEqualTypeOf<unknown>()
181 })
182β
183 it('should return the proper type when passed to QueriesObserver', () => {
184 const key = queryKey()
185 const options = queryOptions({
186 queryKey: key,
187 queryFn: () => Promise.resolve(5),
188 })
189β
190 const queryClient = new QueryClient()
191 const queriesObserver = new QueriesObserver(queryClient, [options])
192 expectTypeOf(queriesObserver).toEqualTypeOf<
193 QueriesObserver<Array<QueryObserverResult>>
194 >()
195 })
196β
197 it('should allow undefined response in initialData', () => {
198 const key = queryKey()
199 const options = (id: string | null) =>
200 queryOptions({
201 queryKey: [...key, id],
202 queryFn: () =>
203 Promise.resolve({
204 id: '1',
205 title: 'Do Laundry',
206 }),
207 initialData: () =>
208 !id
209 ? undefined
210 : {
211 id,
212 title: 'Initial Data',
213 },
214 })
215β
216 expectTypeOf(options(null).initialData).returns.toEqualTypeOf<
217 { id: string; title: string } | undefined
218 >()
219 })
220})