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