Open History to see the last commit
1import { afterEach, beforeEach, describe, expectTypeOf, it } from 'vitest'
2import { MutationCache, QueryClient } from '..'
3import type {
4 DefaultError,
5 Mutation,
6 MutationCacheConfig,
7 MutationCacheNotifyEvent,
8 MutationFilters,
9 MutationFunctionContext,
10 MutationObserver,
11 MutationState,
12} from '..'
13β
14class CustomError extends Error {
15 override name = 'CustomError' as const
16}
17β
18describe('mutationCache', () => {
19 let queryClient: QueryClient
20 let mutationCache: MutationCache
21β
22 beforeEach(() => {
23 queryClient = new QueryClient()
24 queryClient.mount()
25 mutationCache = queryClient.getMutationCache()
26 })
27β
28 afterEach(() => {
29 queryClient.clear()
30 })
31β
32 describe('MutationCache', () => {
33 it('should only expose its documented members', () => {
34 expectTypeOf<keyof MutationCache>().toEqualTypeOf<
35 | 'subscribe'
36 | 'hasListeners'
37 | 'config'
38 | 'build'
39 | 'add'
40 | 'remove'
41 | 'canRun'
42 | 'runNext'
43 | 'clear'
44 | 'getAll'
45 | 'find'
46 | 'findAll'
47 | 'notify'
48 | 'resumePausedMutations'
49 >()
50 })
51 })
52β
53 describe('config', () => {
54 it('should be typed as the cache config', () => {
55 expectTypeOf(mutationCache.config).toEqualTypeOf<MutationCacheConfig>()
56 expectTypeOf(
57 new MutationCache().config,
58 ).toEqualTypeOf<MutationCacheConfig>()
59 })
60β
61 it('should stay writable', () => {
62 type Config = Pick<MutationCache, 'config'>
63β
64 expectTypeOf<Config>().toEqualTypeOf<{
65 -readonly [K in keyof Config]: Config[K]
66 }>()
67 })
68β
69 it('should be optional when constructing a cache', () => {
70 expectTypeOf(MutationCache).constructorParameters.toEqualTypeOf<
71 [config?: MutationCacheConfig]
72 >()
73 })
74 })
75β
76 describe('MutationCacheConfig', () => {
77 it('should declare exactly its four callbacks', () => {
78 expectTypeOf<keyof MutationCacheConfig>().toEqualTypeOf<
79 'onError' | 'onSuccess' | 'onMutate' | 'onSettled'
80 >()
81 })
82β
83 it('should declare every callback as optional', () => {
84 type OptionalKeys = {
85 [K in keyof MutationCacheConfig]-?: {} extends Pick<
86 MutationCacheConfig,
87 K
88 >
89 ? K
90 : never
91 }[keyof MutationCacheConfig]
92β
93 expectTypeOf<OptionalKeys>().toEqualTypeOf<keyof MutationCacheConfig>()
94 })
95β
96 describe('onError', () => {
97 it('should take the error, variables, onMutateResult, mutation and context, and return an unknown value', () => {
98 expectTypeOf<
99 NonNullable<MutationCacheConfig['onError']>
100 >().returns.toEqualTypeOf<unknown>()
101 expectTypeOf<
102 NonNullable<MutationCacheConfig['onError']>
103 >().parameters.toEqualTypeOf<
104 [
105 error: DefaultError,
106 variables: unknown,
107 onMutateResult: unknown,
108 mutation: Mutation<unknown, unknown, unknown>,
109 context: MutationFunctionContext,
110 ]
111 >()
112 })
113 })
114β
115 describe('onSuccess', () => {
116 it('should take the data, variables, onMutateResult, mutation and context, and return an unknown value', () => {
117 expectTypeOf<
118 NonNullable<MutationCacheConfig['onSuccess']>
119 >().returns.toEqualTypeOf<unknown>()
120 expectTypeOf<
121 NonNullable<MutationCacheConfig['onSuccess']>
122 >().parameters.toEqualTypeOf<
123 [
124 data: unknown,
125 variables: unknown,
126 onMutateResult: unknown,
127 mutation: Mutation<unknown, unknown, unknown>,
128 context: MutationFunctionContext,
129 ]
130 >()
131 })
132 })
133β
134 describe('onMutate', () => {
135 it('should take only the variables, mutation and context, and return an unknown value', () => {
136 expectTypeOf<
137 NonNullable<MutationCacheConfig['onMutate']>
138 >().returns.toEqualTypeOf<unknown>()
139 expectTypeOf<
140 NonNullable<MutationCacheConfig['onMutate']>
141 >().parameters.toEqualTypeOf<
142 [
143 variables: unknown,
144 mutation: Mutation<unknown, unknown, unknown>,
145 context: MutationFunctionContext,
146 ]
147 >()
148 })
149 })
150β
151 describe('onSettled', () => {
152 it('should take the data, error, variables, onMutateResult, mutation and context, and return an unknown value', () => {
153 expectTypeOf<
154 NonNullable<MutationCacheConfig['onSettled']>
155 >().returns.toEqualTypeOf<unknown>()
156 expectTypeOf<
157 NonNullable<MutationCacheConfig['onSettled']>
158 >().parameters.toEqualTypeOf<
159 [
160 data: unknown,
161 error: DefaultError | null,
162 variables: unknown,
163 onMutateResult: unknown,
164 mutation: Mutation<unknown, unknown, unknown>,
165 context: MutationFunctionContext,
166 ]
167 >()
168 })
169 })
170 })
171β
172 describe('MutationCacheNotifyEvent', () => {
173 it('should union exactly the six event type literals', () => {
174 expectTypeOf<MutationCacheNotifyEvent['type']>().toEqualTypeOf<
175 | 'added'
176 | 'removed'
177 | 'observerAdded'
178 | 'observerRemoved'
179 | 'observerOptionsUpdated'
180 | 'updated'
181 >()
182 })
183β
184 it('should declare an optional mutation only on the observerOptionsUpdated event', () => {
185 type OptionsUpdated = Extract<
186 MutationCacheNotifyEvent,
187 { type: 'observerOptionsUpdated' }
188 >
189β
190 expectTypeOf<keyof OptionsUpdated>().toEqualTypeOf<
191 'type' | 'mutation' | 'observer'
192 >()
193 expectTypeOf<OptionsUpdated['mutation']>().toEqualTypeOf<
194 Mutation<any, any, any, any> | undefined
195 >()
196 expectTypeOf<OptionsUpdated['observer']>().toEqualTypeOf<
197 MutationObserver<any, any, any, any>
198 >()
199 })
200β
201 it('should declare only a mutation on the added event', () => {
202 type Added = Extract<MutationCacheNotifyEvent, { type: 'added' }>
203β
204 expectTypeOf<keyof Added>().toEqualTypeOf<'type' | 'mutation'>()
205 expectTypeOf<Added['mutation']>().toEqualTypeOf<
206 Mutation<any, any, any, any>
207 >()
208 })
209β
210 it('should declare only a mutation on the removed event', () => {
211 expectTypeOf<
212 keyof Extract<MutationCacheNotifyEvent, { type: 'removed' }>
213 >().toEqualTypeOf<'type' | 'mutation'>()
214 })
215β
216 it('should carry an action on the updated event', () => {
217 expectTypeOf<
218 keyof Extract<MutationCacheNotifyEvent, { type: 'updated' }>
219 >().toEqualTypeOf<'type' | 'mutation' | 'action'>()
220 })
221β
222 it('should carry an observer on the observerAdded event', () => {
223 expectTypeOf<
224 keyof Extract<MutationCacheNotifyEvent, { type: 'observerAdded' }>
225 >().toEqualTypeOf<'type' | 'mutation' | 'observer'>()
226 expectTypeOf<
227 Extract<MutationCacheNotifyEvent, { type: 'observerAdded' }>['observer']
228 >().toEqualTypeOf<MutationObserver<any, any, any>>()
229 })
230β
231 it('should carry an observer on the observerRemoved event', () => {
232 expectTypeOf<
233 keyof Extract<MutationCacheNotifyEvent, { type: 'observerRemoved' }>
234 >().toEqualTypeOf<'type' | 'mutation' | 'observer'>()
235 expectTypeOf<
236 Extract<
237 MutationCacheNotifyEvent,
238 { type: 'observerRemoved' }
239 >['observer']
240 >().toEqualTypeOf<MutationObserver<any, any, any>>()
241 })
242β
243 it('should require the mutation on every event except observerOptionsUpdated', () => {
244 type EventsWithRequiredMutation = Extract<
245 MutationCacheNotifyEvent,
246 { mutation: Mutation<any, any, any, any> }
247 >['type']
248β
249 expectTypeOf<EventsWithRequiredMutation>().toEqualTypeOf<
250 'added' | 'removed' | 'observerAdded' | 'observerRemoved' | 'updated'
251 >()
252 })
253 })
254β
255 describe('subscribe', () => {
256 it('should type the event given to a listener', () => {
257 mutationCache.subscribe((event) => {
258 expectTypeOf(event).toEqualTypeOf<MutationCacheNotifyEvent>()
259 })
260 })
261β
262 it('should return an unsubscribe function', () => {
263 expectTypeOf(mutationCache.subscribe(() => {})).toEqualTypeOf<
264 () => void
265 >()
266 })
267 })
268β
269 describe('build', () => {
270 it('should carry its type parameters into the mutation it returns', () => {
271 const mutation = mutationCache.build<
272 { value: string },
273 CustomError,
274 number,
275 string
276 >(queryClient, {
277 mutationFn: (variables) => {
278 expectTypeOf(variables).toEqualTypeOf<number>()
279 return Promise.resolve({ value: 'data' })
280 },
281 })
282β
283 expectTypeOf(mutation).toEqualTypeOf<
284 Mutation<{ value: string }, CustomError, number, string>
285 >()
286 expectTypeOf(mutation.state).toEqualTypeOf<
287 MutationState<{ value: string }, CustomError, number, string>
288 >()
289 })
290β
291 it('should accept an optional initial state', () => {
292 expectTypeOf(
293 mutationCache.build<{ value: string }, CustomError, number, string>,
294 )
295 .parameter(2)
296 .toEqualTypeOf<
297 | MutationState<{ value: string }, CustomError, number, string>
298 | undefined
299 >()
300 })
301 })
302β
303 describe('getAll', () => {
304 it('should return an array of fully defaulted mutations', () => {
305 const all = mutationCache.getAll()
306β
307 expectTypeOf(all).toBeArray()
308 expectTypeOf(all[0]!.state).toEqualTypeOf<
309 MutationState<unknown, DefaultError, unknown, unknown>
310 >()
311 expectTypeOf(all[0]!.state.data).toEqualTypeOf<unknown>()
312 expectTypeOf(all[0]!.state.error).toEqualTypeOf<DefaultError | null>()
313 })
314β
315 it('should take no arguments', () => {
316 expectTypeOf(mutationCache.getAll).parameters.toEqualTypeOf<[]>()
317 })
318 })
319β
320 describe('find', () => {
321 it('should carry its type parameters into the mutation it returns', () => {
322 expectTypeOf(
323 mutationCache.find<{ value: string }, CustomError, number, string>({
324 mutationKey: ['a'],
325 }),
326 ).toEqualTypeOf<
327 Mutation<{ value: string }, CustomError, number, string> | undefined
328 >()
329 })
330β
331 it('should default its variables type parameter to any', () => {
332 expectTypeOf(mutationCache.find({ mutationKey: ['a'] })).toEqualTypeOf<
333 Mutation<unknown, DefaultError, any, unknown> | undefined
334 >()
335 })
336β
337 it('should only accept mutation filters', () => {
338 expectTypeOf(mutationCache.find).parameters.toEqualTypeOf<
339 [filters: MutationFilters]
340 >()
341 })
342 })
343β
344 describe('findAll', () => {
345 it('should return an array of fully defaulted mutations', () => {
346 const found = mutationCache.findAll({ mutationKey: ['a'] })
347β
348 expectTypeOf(found).toBeArray()
349 expectTypeOf(found[0]!.state).toEqualTypeOf<
350 MutationState<unknown, DefaultError, unknown, unknown>
351 >()
352 })
353β
354 it('should accept filters or nothing', () => {
355 mutationCache.findAll()
356 mutationCache.findAll({ status: 'pending' })
357 // @ts-expect-error status must be a mutation status
358 mutationCache.findAll({ status: 'nope' })
359β
360 expectTypeOf(mutationCache.findAll).parameters.toEqualTypeOf<
361 [filters?: MutationFilters]
362 >()
363 })
364 })
365β
366 describe('add', () => {
367 it('should take a single mutation and return void', () => {
368 expectTypeOf(mutationCache.add).returns.toEqualTypeOf<void>()
369 expectTypeOf(mutationCache.add).parameters.toEqualTypeOf<
370 [mutation: Mutation<any, any, any, any>]
371 >()
372 })
373 })
374β
375 describe('remove', () => {
376 it('should take a single mutation and return void', () => {
377 expectTypeOf(mutationCache.remove).returns.toEqualTypeOf<void>()
378 expectTypeOf(mutationCache.remove).parameters.toEqualTypeOf<
379 [mutation: Mutation<any, any, any, any>]
380 >()
381 })
382 })
383β
384 describe('canRun', () => {
385 it('should return a boolean for a single mutation', () => {
386 expectTypeOf(mutationCache.canRun).returns.toEqualTypeOf<boolean>()
387 expectTypeOf(mutationCache.canRun).parameters.toEqualTypeOf<
388 [mutation: Mutation<any, any, any, any>]
389 >()
390 })
391 })
392β
393 describe('runNext', () => {
394 it('should resolve with an unknown value', () => {
395 expectTypeOf(mutationCache.runNext).returns.toEqualTypeOf<
396 Promise<unknown>
397 >()
398 expectTypeOf(mutationCache.runNext).parameters.toEqualTypeOf<
399 [mutation: Mutation<any, any, any, any>]
400 >()
401 })
402 })
403β
404 describe('clear', () => {
405 it('should take no arguments and return void', () => {
406 expectTypeOf(mutationCache.clear).parameters.toEqualTypeOf<[]>()
407 expectTypeOf(mutationCache.clear).returns.toEqualTypeOf<void>()
408 })
409 })
410β
411 describe('notify', () => {
412 it('should only accept a notify event and return void', () => {
413 expectTypeOf(mutationCache.notify).parameters.toEqualTypeOf<
414 [event: MutationCacheNotifyEvent]
415 >()
416 expectTypeOf(mutationCache.notify).returns.toEqualTypeOf<void>()
417 })
418 })
419β
420 describe('resumePausedMutations', () => {
421 it('should take no arguments and resolve with an unknown value', () => {
422 expectTypeOf(
423 mutationCache.resumePausedMutations,
424 ).parameters.toEqualTypeOf<[]>()
425 expectTypeOf(mutationCache.resumePausedMutations).returns.toEqualTypeOf<
426 Promise<unknown>
427 >()
428 })
429 })
430})