Open History to see the last commit
1import { describe, expect, test, vi } from 'vitest'
2import { waitFor } from '@testing-library/react'
3import { MutationCache, MutationObserver } from '..'
4import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
5β
6describe('mutationCache', () => {
7 describe('MutationCacheConfig error callbacks', () => {
8 test('should call onError and onSettled when a mutation errors', async () => {
9 const key = queryKey()
10 const onError = vi.fn()
11 const onSuccess = vi.fn()
12 const onSettled = vi.fn()
13 const testCache = new MutationCache({ onError, onSuccess, onSettled })
14 const testClient = createQueryClient({ mutationCache: testCache })
15β
16 try {
17 await executeMutation(
18 testClient,
19 {
20 mutationKey: key,
21 mutationFn: () => Promise.reject(new Error('error')),
22 onMutate: () => 'context',
23 },
24 'vars',
25 )
26 } catch {}
27β
28 const mutation = testCache.getAll()[0]
29 expect(onError).toHaveBeenCalledTimes(1)
30 expect(onError).toHaveBeenCalledWith(
31 new Error('error'),
32 'vars',
33 'context',
34 mutation,
35 )
36 expect(onSuccess).not.toHaveBeenCalled()
37 expect(onSettled).toHaveBeenCalledTimes(1)
38 expect(onSettled).toHaveBeenCalledWith(
39 undefined,
40 new Error('error'),
41 'vars',
42 'context',
43 mutation,
44 )
45 })
46β
47 test('should be awaited', async () => {
48 const key = queryKey()
49 const states: Array<number> = []
50 const onError = async () => {
51 states.push(1)
52 await sleep(1)
53 states.push(2)
54 }
55 const onSettled = async () => {
56 states.push(5)
57 await sleep(1)
58 states.push(6)
59 }
60 const testCache = new MutationCache({ onError, onSettled })
61 const testClient = createQueryClient({ mutationCache: testCache })
62β
63 try {
64 await executeMutation(
65 testClient,
66 {
67 mutationKey: key,
68 mutationFn: () => Promise.reject(new Error('error')),
69 onError: async () => {
70 states.push(3)
71 await sleep(1)
72 states.push(4)
73 },
74 onSettled: async () => {
75 states.push(7)
76 await sleep(1)
77 states.push(8)
78 },
79 },
80 'vars',
81 )
82 } catch {}
83β
84 expect(states).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
85 })
86 })
87 describe('MutationCacheConfig success callbacks', () => {
88 test('should call onSuccess and onSettled when a mutation is successful', async () => {
89 const key = queryKey()
90 const onError = vi.fn()
91 const onSuccess = vi.fn()
92 const onSettled = vi.fn()
93 const testCache = new MutationCache({ onError, onSuccess, onSettled })
94 const testClient = createQueryClient({ mutationCache: testCache })
95β
96 try {
97 await executeMutation(
98 testClient,
99 {
100 mutationKey: key,
101 mutationFn: () => Promise.resolve({ data: 5 }),
102 onMutate: () => 'context',
103 },
104 'vars',
105 )
106 } catch {}
107β
108 const mutation = testCache.getAll()[0]
109 expect(onSuccess).toHaveBeenCalledTimes(1)
110 expect(onSuccess).toHaveBeenCalledWith(
111 { data: 5 },
112 'vars',
113 'context',
114 mutation,
115 )
116 expect(onError).not.toHaveBeenCalled()
117 expect(onSettled).toHaveBeenCalledTimes(1)
118 expect(onSettled).toHaveBeenCalledWith(
119 { data: 5 },
120 null,
121 'vars',
122 'context',
123 mutation,
124 )
125 })
126 test('should be awaited', async () => {
127 const key = queryKey()
128 const states: Array<number> = []
129 const onSuccess = async () => {
130 states.push(1)
131 await sleep(1)
132 states.push(2)
133 }
134 const onSettled = async () => {
135 states.push(5)
136 await sleep(1)
137 states.push(6)
138 }
139 const testCache = new MutationCache({ onSuccess, onSettled })
140 const testClient = createQueryClient({ mutationCache: testCache })
141β
142 await executeMutation(
143 testClient,
144 {
145 mutationKey: key,
146 mutationFn: () => Promise.resolve({ data: 5 }),
147 onSuccess: async () => {
148 states.push(3)
149 await sleep(1)
150 states.push(4)
151 },
152 onSettled: async () => {
153 states.push(7)
154 await sleep(1)
155 states.push(8)
156 },
157 },
158 'vars',
159 )
160β
161 expect(states).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
162 })
163 })
164 describe('MutationCacheConfig.onMutate', () => {
165 test('should be called before a mutation executes', async () => {
166 const key = queryKey()
167 const onMutate = vi.fn()
168 const testCache = new MutationCache({ onMutate })
169 const testClient = createQueryClient({ mutationCache: testCache })
170β
171 try {
172 await executeMutation(
173 testClient,
174 {
175 mutationKey: key,
176 mutationFn: () => Promise.resolve({ data: 5 }),
177 onMutate: () => 'context',
178 },
179 'vars',
180 )
181 } catch {}
182β
183 const mutation = testCache.getAll()[0]
184 expect(onMutate).toHaveBeenCalledWith('vars', mutation)
185 })
186β
187 test('should be awaited', async () => {
188 const key = queryKey()
189 const states: Array<number> = []
190 const onMutate = async () => {
191 states.push(1)
192 await sleep(1)
193 states.push(2)
194 }
195 const testCache = new MutationCache({ onMutate })
196 const testClient = createQueryClient({ mutationCache: testCache })
197β
198 await executeMutation(
199 testClient,
200 {
201 mutationKey: key,
202 mutationFn: () => Promise.resolve({ data: 5 }),
203 onMutate: async () => {
204 states.push(3)
205 await sleep(1)
206 states.push(4)
207 },
208 },
209 'vars',
210 )
211β
212 expect(states).toEqual([1, 2, 3, 4])
213 })
214 })
215β
216 describe('find', () => {
217 test('should filter correctly', async () => {
218 const testCache = new MutationCache()
219 const testClient = createQueryClient({ mutationCache: testCache })
220 const key = ['mutation', 'vars']
221 await executeMutation(
222 testClient,
223 {
224 mutationKey: key,
225 mutationFn: () => Promise.resolve(),
226 },
227 'vars',
228 )
229 const [mutation] = testCache.getAll()
230 expect(testCache.find({ mutationKey: key })).toEqual(mutation)
231 expect(
232 testCache.find({ mutationKey: ['mutation'], exact: false }),
233 ).toEqual(mutation)
234 expect(testCache.find({ mutationKey: ['unknown'] })).toEqual(undefined)
235 expect(
236 testCache.find({
237 predicate: (m) => m.options.mutationKey?.[0] === key[0],
238 }),
239 ).toEqual(mutation)
240 })
241 })
242β
243 describe('findAll', () => {
244 test('should filter correctly', async () => {
245 const testCache = new MutationCache()
246 const testClient = createQueryClient({ mutationCache: testCache })
247 await executeMutation(
248 testClient,
249 {
250 mutationKey: ['a', 1],
251 mutationFn: () => Promise.resolve(),
252 },
253 1,
254 )
255 await executeMutation(
256 testClient,
257 {
258 mutationKey: ['a', 2],
259 mutationFn: () => Promise.resolve(),
260 },
261 2,
262 )
263 await executeMutation(
264 testClient,
265 {
266 mutationKey: ['b'],
267 mutationFn: () => Promise.resolve(),
268 },
269 3,
270 )
271β
272 const [mutation1, mutation2] = testCache.getAll()
273 expect(
274 testCache.findAll({ mutationKey: ['a'], exact: false }),
275 ).toHaveLength(2)
276 expect(testCache.find({ mutationKey: ['a', 1] })).toEqual(mutation1)
277 expect(
278 testCache.findAll({
279 predicate: (m) => m.options.mutationKey?.[1] === 2,
280 }),
281 ).toEqual([mutation2])
282 expect(testCache.findAll({ mutationKey: ['unknown'] })).toEqual([])
283 })
284 })
285β
286 describe('garbage collection', () => {
287 test('should remove unused mutations after gcTime has elapsed', async () => {
288 const testCache = new MutationCache()
289 const testClient = createQueryClient({ mutationCache: testCache })
290 const onSuccess = vi.fn()
291 await executeMutation(
292 testClient,
293 {
294 mutationKey: ['a', 1],
295 gcTime: 10,
296 mutationFn: () => Promise.resolve(),
297 onSuccess,
298 },
299 1,
300 )
301β
302 expect(testCache.getAll()).toHaveLength(1)
303 await sleep(10)
304 await waitFor(() => {
305 expect(testCache.getAll()).toHaveLength(0)
306 })
307 expect(onSuccess).toHaveBeenCalledTimes(1)
308 })
309β
310 test('should not remove mutations if there are active observers', async () => {
311 const queryClient = createQueryClient()
312 const observer = new MutationObserver(queryClient, {
313 gcTime: 10,
314 mutationFn: (input: number) => Promise.resolve(input),
315 })
316 const unsubscribe = observer.subscribe(() => undefined)
317β
318 expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
319 observer.mutate(1)
320 expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
321 await sleep(10)
322 expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
323 unsubscribe()
324 expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
325 await sleep(10)
326 await waitFor(() => {
327 expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
328 })
329 })
330β
331 test('should be garbage collected later when unsubscribed and mutation is pending', async () => {
332 const queryClient = createQueryClient()
333 const onSuccess = vi.fn()
334 const observer = new MutationObserver(queryClient, {
335 gcTime: 10,
336 mutationFn: async () => {
337 await sleep(20)
338 return 'data'
339 },
340 onSuccess,
341 })
342 const unsubscribe = observer.subscribe(() => undefined)
343 observer.mutate(1)
344 unsubscribe()
345 expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
346 await sleep(10)
347 // unsubscribe should not remove even though gcTime has elapsed b/c mutation is still pending
348 expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
349 await sleep(10)
350 // should be removed after an additional gcTime wait
351 await waitFor(() => {
352 expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
353 })
354 expect(onSuccess).toHaveBeenCalledTimes(1)
355 })
356β
357 test('should call callbacks even with gcTime 0 and mutation still pending', async () => {
358 const queryClient = createQueryClient()
359 const onSuccess = vi.fn()
360 const observer = new MutationObserver(queryClient, {
361 gcTime: 0,
362 mutationFn: async () => {
363 return 'data'
364 },
365 onSuccess,
366 })
367 const unsubscribe = observer.subscribe(() => undefined)
368 observer.mutate(1)
369 unsubscribe()
370 await waitFor(() => {
371 expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
372 })
373 expect(onSuccess).toHaveBeenCalledTimes(1)
374 })
375 })
376})