Open History to see the last commit
1import { createKey } from '@tanstack/query-test-utils'
2import { afterEach, beforeEach, describe, expectTypeOf, it } from 'vitest'
3import { QueryCache, QueryClient } from '..'
4import type {
5 DefaultError,
6 NotifyEvent,
7 NotifyEventType,
8 Query,
9 QueryCacheConfig,
10 QueryCacheNotifyEvent,
11 QueryFilters,
12 QueryKey,
13 QueryObserver,
14 QueryState,
15} from '..'
16import type { Action } from '../query'
17import type { QueryStore } from '../queryCache'
18β
19class CustomError extends Error {
20 override name = 'CustomError' as const
21}
22β
23describe('queryCache', () => {
24 let queryClient: QueryClient
25 let queryCache: QueryCache
26β
27 beforeEach(() => {
28 queryClient = new QueryClient()
29 queryClient.mount()
30 queryCache = queryClient.getQueryCache()
31 })
32β
33 afterEach(() => {
34 queryClient.clear()
35 })
36β
37 describe('QueryCache', () => {
38 it('should only expose its documented members', () => {
39 expectTypeOf<keyof QueryCache>().toEqualTypeOf<
40 | 'subscribe'
41 | 'hasListeners'
42 | 'config'
43 | 'build'
44 | 'add'
45 | 'remove'
46 | 'clear'
47 | 'get'
48 | 'getAll'
49 | 'find'
50 | 'findAll'
51 | 'notify'
52 | 'onFocus'
53 | 'onOnline'
54 >()
55 })
56 })
57β
58 describe('config', () => {
59 it('should be typed as the cache config', () => {
60 expectTypeOf(queryCache.config).toEqualTypeOf<QueryCacheConfig>()
61 expectTypeOf(new QueryCache().config).toEqualTypeOf<QueryCacheConfig>()
62 })
63β
64 it('should stay writable', () => {
65 type Config = Pick<QueryCache, 'config'>
66β
67 expectTypeOf<Config>().toEqualTypeOf<{
68 -readonly [K in keyof Config]: Config[K]
69 }>()
70 })
71β
72 it('should be optional when constructing a cache', () => {
73 expectTypeOf(QueryCache).constructorParameters.toEqualTypeOf<
74 [config?: QueryCacheConfig]
75 >()
76 })
77 })
78β
79 describe('QueryCacheConfig', () => {
80 it('should declare every callback as optional', () => {
81 expectTypeOf<QueryCacheConfig>().toEqualTypeOf<{
82 onError?: (
83 error: DefaultError,
84 query: Query<unknown, unknown, unknown>,
85 ) => void
86 onSuccess?: (
87 data: unknown,
88 query: Query<unknown, unknown, unknown>,
89 ) => void
90 onSettled?: (
91 data: unknown | undefined,
92 error: DefaultError | null,
93 query: Query<unknown, unknown, unknown>,
94 ) => void
95 }>()
96 })
97β
98 describe('onError', () => {
99 it('should type the error and the query it is given', () => {
100 expectTypeOf<
101 NonNullable<QueryCacheConfig['onError']>
102 >().parameters.toEqualTypeOf<
103 [error: DefaultError, query: Query<unknown, unknown, unknown>]
104 >()
105 })
106β
107 it('should not await its return value', () => {
108 expectTypeOf<
109 NonNullable<QueryCacheConfig['onError']>
110 >().returns.toEqualTypeOf<void>()
111 })
112 })
113β
114 describe('onSuccess', () => {
115 it('should type the data and the query it is given', () => {
116 expectTypeOf<
117 NonNullable<QueryCacheConfig['onSuccess']>
118 >().parameters.toEqualTypeOf<
119 [data: unknown, query: Query<unknown, unknown, unknown>]
120 >()
121 })
122β
123 it('should not await its return value', () => {
124 expectTypeOf<
125 NonNullable<QueryCacheConfig['onSuccess']>
126 >().returns.toEqualTypeOf<void>()
127 })
128 })
129β
130 describe('onSettled', () => {
131 it('should type the data, error and query it is given', () => {
132 expectTypeOf<
133 NonNullable<QueryCacheConfig['onSettled']>
134 >().parameters.toEqualTypeOf<
135 [
136 data: unknown,
137 error: DefaultError | null,
138 query: Query<unknown, unknown, unknown>,
139 ]
140 >()
141 })
142β
143 it('should not await its return value', () => {
144 expectTypeOf<
145 NonNullable<QueryCacheConfig['onSettled']>
146 >().returns.toEqualTypeOf<void>()
147 })
148 })
149 })
150β
151 describe('NotifyEventType', () => {
152 it('should union exactly the seven event type literals', () => {
153 // Every `QueryCacheNotifyEvent` variant redeclares `type` with its own
154 // literal, so the base union is only observable through this alias.
155 expectTypeOf<NotifyEventType>().toEqualTypeOf<
156 | 'added'
157 | 'removed'
158 | 'updated'
159 | 'observerAdded'
160 | 'observerRemoved'
161 | 'observerResultsUpdated'
162 | 'observerOptionsUpdated'
163 >()
164 })
165 })
166β
167 describe('NotifyEvent', () => {
168 it('should declare a single type property taken from NotifyEventType', () => {
169 expectTypeOf<keyof NotifyEvent>().toEqualTypeOf<'type'>()
170 expectTypeOf<NotifyEvent['type']>().toEqualTypeOf<NotifyEventType>()
171 })
172 })
173β
174 describe('QueryCacheNotifyEvent', () => {
175 it('should union exactly the seven event type literals', () => {
176 expectTypeOf<QueryCacheNotifyEvent['type']>().toEqualTypeOf<
177 | 'added'
178 | 'removed'
179 | 'updated'
180 | 'observerAdded'
181 | 'observerRemoved'
182 | 'observerResultsUpdated'
183 | 'observerOptionsUpdated'
184 >()
185 })
186β
187 it('should carry an action only on the updated event', () => {
188 type Updated = Extract<QueryCacheNotifyEvent, { type: 'updated' }>
189β
190 expectTypeOf<keyof Updated>().toEqualTypeOf<'type' | 'query' | 'action'>()
191 expectTypeOf<Updated['query']>().toEqualTypeOf<
192 Query<any, any, any, any>
193 >()
194 expectTypeOf<Updated['action']>().toEqualTypeOf<Action<any, any>>()
195 })
196β
197 it('should declare only a query on the added event', () => {
198 expectTypeOf<
199 keyof Extract<QueryCacheNotifyEvent, { type: 'added' }>
200 >().toEqualTypeOf<'type' | 'query'>()
201 })
202β
203 it('should declare only a query on the removed event', () => {
204 expectTypeOf<
205 keyof Extract<QueryCacheNotifyEvent, { type: 'removed' }>
206 >().toEqualTypeOf<'type' | 'query'>()
207 })
208β
209 it('should carry an observer on the observerAdded event', () => {
210 expectTypeOf<
211 keyof Extract<QueryCacheNotifyEvent, { type: 'observerAdded' }>
212 >().toEqualTypeOf<'type' | 'query' | 'observer'>()
213 expectTypeOf<
214 Extract<QueryCacheNotifyEvent, { type: 'observerAdded' }>['observer']
215 >().toEqualTypeOf<QueryObserver<any, any, any, any, any>>()
216 })
217β
218 it('should carry an observer on the observerRemoved event', () => {
219 expectTypeOf<
220 keyof Extract<QueryCacheNotifyEvent, { type: 'observerRemoved' }>
221 >().toEqualTypeOf<'type' | 'query' | 'observer'>()
222 expectTypeOf<
223 Extract<QueryCacheNotifyEvent, { type: 'observerRemoved' }>['observer']
224 >().toEqualTypeOf<QueryObserver<any, any, any, any, any>>()
225 })
226β
227 it('should carry an observer on the observerOptionsUpdated event', () => {
228 expectTypeOf<
229 keyof Extract<QueryCacheNotifyEvent, { type: 'observerOptionsUpdated' }>
230 >().toEqualTypeOf<'type' | 'query' | 'observer'>()
231 expectTypeOf<
232 Extract<
233 QueryCacheNotifyEvent,
234 { type: 'observerOptionsUpdated' }
235 >['observer']
236 >().toEqualTypeOf<QueryObserver<any, any, any, any, any>>()
237 })
238β
239 it('should require the query on every event', () => {
240 type EventsWithRequiredQuery = Extract<
241 QueryCacheNotifyEvent,
242 { query: Query<any, any, any, any> }
243 >['type']
244β
245 expectTypeOf<EventsWithRequiredQuery>().toEqualTypeOf<
246 QueryCacheNotifyEvent['type']
247 >()
248 })
249β
250 it('should not carry an observer on the observerResultsUpdated event', () => {
251 expectTypeOf<
252 keyof Extract<QueryCacheNotifyEvent, { type: 'observerResultsUpdated' }>
253 >().toEqualTypeOf<'type' | 'query'>()
254 })
255 })
256β
257 describe('QueryStore', () => {
258 it('should type every method of the store', () => {
259 expectTypeOf<QueryStore>().toEqualTypeOf<{
260 has: (queryHash: string) => boolean
261 set: (queryHash: string, query: Query) => void
262 get: (queryHash: string) => Query | undefined
263 delete: (queryHash: string) => void
264 values: () => IterableIterator<Query>
265 }>()
266 })
267 })
268β
269 describe('subscribe', () => {
270 it('should type the event given to a listener', () => {
271 queryCache.subscribe((event) => {
272 expectTypeOf(event).toEqualTypeOf<QueryCacheNotifyEvent>()
273 })
274 })
275β
276 it('should return an unsubscribe function', () => {
277 expectTypeOf(queryCache.subscribe(() => {})).toEqualTypeOf<() => void>()
278 })
279 })
280β
281 describe('build', () => {
282 it('should carry its type parameters into the query it returns', () => {
283 const query = queryCache.build<{ value: string }, CustomError>(
284 queryClient,
285 {
286 queryKey: createKey(),
287 queryFn: () => Promise.resolve({ value: 'data' }),
288 },
289 )
290β
291 expectTypeOf(query).toEqualTypeOf<
292 Query<
293 { value: string },
294 CustomError,
295 { value: string },
296 ReadonlyArray<unknown>
297 >
298 >()
299 expectTypeOf(query.state).toEqualTypeOf<
300 QueryState<{ value: string }, CustomError>
301 >()
302 })
303β
304 it('should preserve a literal queryKey', () => {
305 const key = ['a', 1] as const
306β
307 expectTypeOf(
308 queryCache.build(queryClient, {
309 queryKey: key,
310 queryFn: () => Promise.resolve('data'),
311 }).queryKey,
312 ).toEqualTypeOf<readonly ['a', 1]>()
313 })
314β
315 it('should require a queryKey in the options it is given', () => {
316 // @ts-expect-error a queryKey is required
317 queryCache.build(queryClient, {
318 queryFn: () => Promise.resolve('data'),
319 })
320β
321 expectTypeOf(queryCache.build)
322 .parameter(1)
323 .toHaveProperty('queryKey')
324 .toEqualTypeOf<ReadonlyArray<unknown>>()
325 })
326β
327 it('should accept an optional initial state', () => {
328 expectTypeOf(queryCache.build<{ value: string }, CustomError>)
329 .parameter(2)
330 .toEqualTypeOf<QueryState<{ value: string }, CustomError> | undefined>()
331 })
332β
333 it('should default its type parameters', () => {
334 const query = queryCache.build(queryClient, { queryKey: createKey() })
335β
336 expectTypeOf(query.state).toEqualTypeOf<
337 QueryState<unknown, DefaultError>
338 >()
339 expectTypeOf(query.state.data).toEqualTypeOf<unknown>()
340 expectTypeOf(query.state.error).toEqualTypeOf<DefaultError | null>()
341 })
342 })
343β
344 describe('get', () => {
345 it('should carry its type parameters into the query it returns', () => {
346 expectTypeOf(
347 queryCache.get<{ value: string }, CustomError>('hash'),
348 ).toEqualTypeOf<
349 | Query<
350 { value: string },
351 CustomError,
352 { value: string },
353 ReadonlyArray<unknown>
354 >
355 | undefined
356 >()
357 })
358β
359 it('should take a query hash and default its type parameters', () => {
360 expectTypeOf(queryCache.get).parameters.toEqualTypeOf<
361 [queryHash: string]
362 >()
363 expectTypeOf(queryCache.get('hash')).toEqualTypeOf<
364 Query<unknown, DefaultError, unknown, QueryKey> | undefined
365 >()
366 })
367β
368 it('should accept a queryKey type parameter, unlike find', () => {
369 expectTypeOf(
370 queryCache.get<
371 { value: string },
372 CustomError,
373 { value: string },
374 readonly ['a', 1]
375 >('hash'),
376 ).toEqualTypeOf<
377 | Query<
378 { value: string },
379 CustomError,
380 { value: string },
381 readonly ['a', 1]
382 >
383 | undefined
384 >()
385 })
386 })
387β
388 describe('getAll', () => {
389 it('should return an array of fully defaulted queries', () => {
390 expectTypeOf(queryCache.getAll()).toEqualTypeOf<
391 Array<Query<unknown, DefaultError, unknown, QueryKey>>
392 >()
393 })
394β
395 it('should take no arguments', () => {
396 expectTypeOf(queryCache.getAll).parameters.toEqualTypeOf<[]>()
397 })
398 })
399β
400 describe('find', () => {
401 it('should carry its type parameters into the query it returns', () => {
402 expectTypeOf(
403 queryCache.find<{ value: string }, CustomError>({
404 queryKey: createKey(),
405 }),
406 ).toEqualTypeOf<
407 | Query<{ value: string }, CustomError, { value: string }, QueryKey>
408 | undefined
409 >()
410 })
411β
412 it('should require a queryKey in the filters it is given', () => {
413 // @ts-expect-error a queryKey is required
414 queryCache.find({})
415β
416 expectTypeOf(queryCache.find)
417 .parameter(0)
418 .toHaveProperty('queryKey')
419 .toEqualTypeOf<ReadonlyArray<unknown>>()
420 })
421β
422 it('should default its type parameters', () => {
423 expectTypeOf(queryCache.find({ queryKey: createKey() })).toEqualTypeOf<
424 Query<unknown, DefaultError, unknown, QueryKey> | undefined
425 >()
426 })
427 })
428β
429 describe('findAll', () => {
430 it('should return an array of fully defaulted queries', () => {
431 expectTypeOf(queryCache.findAll({ queryKey: createKey() })).toEqualTypeOf<
432 Array<Query<unknown, DefaultError, unknown, QueryKey>>
433 >()
434 })
435β
436 it('should accept filters or nothing', () => {
437 queryCache.findAll()
438 queryCache.findAll({ queryKey: createKey() })
439 queryCache.findAll({ stale: true })
440 // @ts-expect-error stale must be a boolean
441 queryCache.findAll({ stale: 'yes' })
442β
443 expectTypeOf(queryCache.findAll).parameters.toEqualTypeOf<
444 [filters?: QueryFilters<any>]
445 >()
446 })
447 })
448β
449 describe('add', () => {
450 it('should take a single query and return void', () => {
451 expectTypeOf(queryCache.add).returns.toEqualTypeOf<void>()
452 expectTypeOf(queryCache.add).parameters.toEqualTypeOf<
453 [query: Query<any, any, any, any>]
454 >()
455 })
456 })
457β
458 describe('remove', () => {
459 it('should take a single query and return void', () => {
460 expectTypeOf(queryCache.remove).returns.toEqualTypeOf<void>()
461 expectTypeOf(queryCache.remove).parameters.toEqualTypeOf<
462 [query: Query<any, any, any, any>]
463 >()
464 })
465 })
466β
467 describe('clear', () => {
468 it('should take no arguments and return void', () => {
469 expectTypeOf(queryCache.clear).parameters.toEqualTypeOf<[]>()
470 expectTypeOf(queryCache.clear).returns.toEqualTypeOf<void>()
471 })
472 })
473β
474 describe('notify', () => {
475 it('should only accept a notify event and return void', () => {
476 expectTypeOf(queryCache.notify).parameters.toEqualTypeOf<
477 [event: QueryCacheNotifyEvent]
478 >()
479 expectTypeOf(queryCache.notify).returns.toEqualTypeOf<void>()
480 })
481 })
482β
483 describe('onFocus', () => {
484 it('should take no arguments and return void', () => {
485 expectTypeOf(queryCache.onFocus).parameters.toEqualTypeOf<[]>()
486 expectTypeOf(queryCache.onFocus).returns.toEqualTypeOf<void>()
487 })
488 })
489β
490 describe('onOnline', () => {
491 it('should take no arguments and return void', () => {
492 expectTypeOf(queryCache.onOnline).parameters.toEqualTypeOf<[]>()
493 expectTypeOf(queryCache.onOnline).returns.toEqualTypeOf<void>()
494 })
495 })
496})