Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

mutationOptions.test.ts

462 lines13.2 KB

1import { QueryClient } from '@tanstack/query-core'
2import { createKey, sleep } from '@tanstack/query-test-utils'
3import { LitElement } from 'lit'
4import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
5import { createMutationController } from '../createMutationController.js'
6import { mutationOptions } from '../mutationOptions.js'
7import { useIsMutating } from '../useIsMutating.js'
8import { useMutationState } from '../useMutationState.js'
9import { generateElementName } from './utils.js'
10import type { CreateMutationOptions } from '../createMutationController.js'
11​
12describe('mutationOptions', () => {
13 let queryClient: QueryClient
14 let container: HTMLElement
15​
16 beforeEach(() => {
17 vi.useFakeTimers()
18 queryClient = new QueryClient()
19 container = document.createElement('div')
20 document.body.append(container)
21 })
22​
23 afterEach(() => {
24 container.remove()
25 queryClient.clear()
26 vi.useRealTimers()
27 })
28​
29 it('should return the object received as a parameter without any modification (with mutationKey in mutationOptions)', () => {
30 const object: CreateMutationOptions = {
31 mutationKey: ['key'],
32 mutationFn: () => sleep(10).then(() => 5),
33 } as const
34​
35 expect(mutationOptions(object)).toBe(object)
36 })
37​
38 it('should return the object received as a parameter without any modification (without mutationKey in mutationOptions)', () => {
39 const object: CreateMutationOptions = {
40 mutationFn: () => sleep(10).then(() => 5),
41 } as const
42​
43 expect(mutationOptions(object)).toBe(object)
44 })
45​
46 it('should return the number of fetching mutations when used with useIsMutating (with mutationKey in mutationOptions)', async () => {
47 const mutationOpts = mutationOptions({
48 mutationKey: createKey(),
49 mutationFn: () => sleep(10).then(() => 'data'),
50 })
51​
52 class Host extends LitElement {
53 readonly mutation1 = createMutationController(
54 this,
55 mutationOpts,
56 queryClient,
57 )
58​
59 readonly isMutating = useIsMutating(this, {}, queryClient)
60 }
61​
62 customElements.define(generateElementName(), Host)
63 const host = new Host()
64 container.append(host)
65​
66 expect(host.isMutating()).toBe(0)
67​
68 host.mutation1.mutate()
69 await vi.advanceTimersByTimeAsync(0)
70 expect(host.isMutating()).toBe(1)
71 await vi.advanceTimersByTimeAsync(10)
72 expect(host.isMutating()).toBe(0)
73 })
74​
75 it('should return the number of fetching mutations when used with useIsMutating (without mutationKey in mutationOptions)', async () => {
76 const mutationOpts = mutationOptions({
77 mutationFn: () => sleep(10).then(() => 'data'),
78 })
79​
80 class Host extends LitElement {
81 readonly mutation1 = createMutationController(
82 this,
83 mutationOpts,
84 queryClient,
85 )
86​
87 readonly isMutating = useIsMutating(this, {}, queryClient)
88 }
89​
90 customElements.define(generateElementName(), Host)
91 const host = new Host()
92 container.append(host)
93​
94 expect(host.isMutating()).toBe(0)
95​
96 host.mutation1.mutate()
97 await vi.advanceTimersByTimeAsync(0)
98 expect(host.isMutating()).toBe(1)
99 await vi.advanceTimersByTimeAsync(10)
100 expect(host.isMutating()).toBe(0)
101 })
102​
103 it('should return the number of fetching mutations when used with useIsMutating', async () => {
104 const mutationOpts1 = mutationOptions({
105 mutationKey: createKey(),
106 mutationFn: () => sleep(10).then(() => 'data1'),
107 })
108 const mutationOpts2 = mutationOptions({
109 mutationFn: () => sleep(10).then(() => 'data2'),
110 })
111​
112 class Host extends LitElement {
113 readonly mutation1 = createMutationController(
114 this,
115 mutationOpts1,
116 queryClient,
117 )
118 readonly mutation2 = createMutationController(
119 this,
120 mutationOpts2,
121 queryClient,
122 )
123​
124 readonly isMutating = useIsMutating(this, {}, queryClient)
125 }
126​
127 customElements.define(generateElementName(), Host)
128 const host = new Host()
129 container.append(host)
130​
131 expect(host.isMutating()).toBe(0)
132​
133 host.mutation1.mutate()
134 host.mutation2.mutate()
135 await vi.advanceTimersByTimeAsync(0)
136 expect(host.isMutating()).toBe(2)
137 await vi.advanceTimersByTimeAsync(10)
138 expect(host.isMutating()).toBe(0)
139 })
140​
141 it('should return the number of fetching mutations when used with useIsMutating (filter mutationOpts1.mutationKey)', async () => {
142 const mutationOpts1 = mutationOptions({
143 mutationKey: createKey(),
144 mutationFn: () => sleep(10).then(() => 'data1'),
145 })
146 const mutationOpts2 = mutationOptions({
147 mutationFn: () => sleep(10).then(() => 'data2'),
148 })
149​
150 class Host extends LitElement {
151 readonly mutation1 = createMutationController(
152 this,
153 mutationOpts1,
154 queryClient,
155 )
156 readonly mutation2 = createMutationController(
157 this,
158 mutationOpts2,
159 queryClient,
160 )
161​
162 readonly isMutating = useIsMutating(
163 this,
164 { mutationKey: mutationOpts1.mutationKey },
165 queryClient,
166 )
167 }
168​
169 customElements.define(generateElementName(), Host)
170 const host = new Host()
171 container.append(host)
172​
173 expect(host.isMutating()).toBe(0)
174​
175 host.mutation1.mutate()
176 host.mutation2.mutate()
177 await vi.advanceTimersByTimeAsync(0)
178 expect(host.isMutating()).toBe(1)
179 await vi.advanceTimersByTimeAsync(10)
180 expect(host.isMutating()).toBe(0)
181 })
182​
183 it('should return the number of fetching mutations when used with queryClient.isMutating (with mutationKey in mutationOptions)', async () => {
184 const mutationOpts = mutationOptions({
185 mutationKey: createKey(),
186 mutationFn: () => sleep(10).then(() => 'data'),
187 })
188​
189 class Host extends LitElement {
190 readonly mutation1 = createMutationController(
191 this,
192 mutationOpts,
193 queryClient,
194 )
195 }
196​
197 customElements.define(generateElementName(), Host)
198 const host = new Host()
199 container.append(host)
200​
201 expect(queryClient.isMutating(mutationOpts)).toBe(0)
202​
203 host.mutation1.mutate()
204 expect(queryClient.isMutating(mutationOpts)).toBe(1)
205 await vi.advanceTimersByTimeAsync(10)
206 expect(queryClient.isMutating(mutationOpts)).toBe(0)
207 })
208​
209 it('should return the number of fetching mutations when used with queryClient.isMutating (without mutationKey in mutationOptions)', async () => {
210 const mutationOpts = mutationOptions({
211 mutationFn: () => sleep(10).then(() => 'data'),
212 })
213​
214 class Host extends LitElement {
215 readonly mutation1 = createMutationController(
216 this,
217 mutationOpts,
218 queryClient,
219 )
220 }
221​
222 customElements.define(generateElementName(), Host)
223 const host = new Host()
224 container.append(host)
225​
226 expect(queryClient.isMutating()).toBe(0)
227​
228 host.mutation1.mutate()
229 expect(queryClient.isMutating()).toBe(1)
230 await vi.advanceTimersByTimeAsync(10)
231 expect(queryClient.isMutating()).toBe(0)
232 })
233​
234 it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
235 const mutationOpts1 = mutationOptions({
236 mutationKey: createKey(),
237 mutationFn: () => sleep(10).then(() => 'data1'),
238 })
239 const mutationOpts2 = mutationOptions({
240 mutationFn: () => sleep(10).then(() => 'data2'),
241 })
242​
243 class Host extends LitElement {
244 readonly mutation1 = createMutationController(
245 this,
246 mutationOpts1,
247 queryClient,
248 )
249 readonly mutation2 = createMutationController(
250 this,
251 mutationOpts2,
252 queryClient,
253 )
254 }
255​
256 customElements.define(generateElementName(), Host)
257 const host = new Host()
258 container.append(host)
259​
260 expect(queryClient.isMutating()).toBe(0)
261​
262 host.mutation1.mutate()
263 host.mutation2.mutate()
264 expect(queryClient.isMutating()).toBe(2)
265 await vi.advanceTimersByTimeAsync(10)
266 expect(queryClient.isMutating()).toBe(0)
267 })
268​
269 it('should return the number of fetching mutations when used with queryClient.isMutating (filter mutationOpts1.mutationKey)', async () => {
270 const mutationOpts1 = mutationOptions({
271 mutationKey: createKey(),
272 mutationFn: () => sleep(10).then(() => 'data1'),
273 })
274 const mutationOpts2 = mutationOptions({
275 mutationFn: () => sleep(10).then(() => 'data2'),
276 })
277​
278 class Host extends LitElement {
279 readonly mutation1 = createMutationController(
280 this,
281 mutationOpts1,
282 queryClient,
283 )
284 readonly mutation2 = createMutationController(
285 this,
286 mutationOpts2,
287 queryClient,
288 )
289 }
290​
291 customElements.define(generateElementName(), Host)
292 const host = new Host()
293 container.append(host)
294​
295 expect(
296 queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
297 ).toBe(0)
298​
299 host.mutation1.mutate()
300 host.mutation2.mutate()
301 expect(
302 queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
303 ).toBe(1)
304 await vi.advanceTimersByTimeAsync(10)
305 expect(
306 queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
307 ).toBe(0)
308 })
309​
310 it('should return the number of fetching mutations when used with useMutationState (with mutationKey in mutationOptions)', async () => {
311 const mutationOpts = mutationOptions({
312 mutationKey: createKey(),
313 mutationFn: () => sleep(10).then(() => 'data'),
314 })
315​
316 class Host extends LitElement {
317 readonly mutation1 = createMutationController(
318 this,
319 mutationOpts,
320 queryClient,
321 )
322​
323 readonly mutationStates = useMutationState(
324 this,
325 {
326 filters: { mutationKey: mutationOpts.mutationKey, status: 'success' },
327 },
328 queryClient,
329 )
330 }
331​
332 customElements.define(generateElementName(), Host)
333 const host = new Host()
334 container.append(host)
335​
336 expect(host.mutationStates()).toHaveLength(0)
337​
338 host.mutation1.mutate()
339 await vi.advanceTimersByTimeAsync(10)
340 expect(host.mutationStates()).toHaveLength(1)
341 expect(host.mutationStates()[0]?.data).toBe('data')
342 })
343​
344 it('should return the number of fetching mutations when used with useMutationState (without mutationKey in mutationOptions)', async () => {
345 const mutationOpts = mutationOptions({
346 mutationFn: () => sleep(10).then(() => 'data'),
347 })
348​
349 class Host extends LitElement {
350 readonly mutation1 = createMutationController(
351 this,
352 mutationOpts,
353 queryClient,
354 )
355​
356 readonly mutationStates = useMutationState(
357 this,
358 { filters: { status: 'success' } },
359 queryClient,
360 )
361 }
362​
363 customElements.define(generateElementName(), Host)
364 const host = new Host()
365 container.append(host)
366​
367 expect(host.mutationStates()).toHaveLength(0)
368​
369 host.mutation1.mutate()
370 await vi.advanceTimersByTimeAsync(10)
371 expect(host.mutationStates()).toHaveLength(1)
372 expect(host.mutationStates()[0]?.data).toBe('data')
373 })
374​
375 it('should return the number of fetching mutations when used with useMutationState', async () => {
376 const mutationOpts1 = mutationOptions({
377 mutationKey: createKey(),
378 mutationFn: () => sleep(10).then(() => 'data1'),
379 })
380 const mutationOpts2 = mutationOptions({
381 mutationFn: () => sleep(10).then(() => 'data2'),
382 })
383​
384 class Host extends LitElement {
385 readonly mutation1 = createMutationController(
386 this,
387 mutationOpts1,
388 queryClient,
389 )
390 readonly mutation2 = createMutationController(
391 this,
392 mutationOpts2,
393 queryClient,
394 )
395​
396 readonly mutationStates = useMutationState(
397 this,
398 { filters: { status: 'success' } },
399 queryClient,
400 )
401 }
402​
403 customElements.define(generateElementName(), Host)
404 const host = new Host()
405 container.append(host)
406​
407 expect(host.mutationStates()).toHaveLength(0)
408​
409 host.mutation1.mutate()
410 host.mutation2.mutate()
411 await vi.advanceTimersByTimeAsync(10)
412 expect(host.mutationStates()).toHaveLength(2)
413 expect(host.mutationStates()[0]?.data).toBe('data1')
414 expect(host.mutationStates()[1]?.data).toBe('data2')
415 })
416​
417 it('should return the number of fetching mutations when used with useMutationState (filter mutationOpts1.mutationKey)', async () => {
418 const mutationOpts1 = mutationOptions({
419 mutationKey: createKey(),
420 mutationFn: () => sleep(10).then(() => 'data1'),
421 })
422 const mutationOpts2 = mutationOptions({
423 mutationFn: () => sleep(10).then(() => 'data2'),
424 })
425​
426 class Host extends LitElement {
427 readonly mutation1 = createMutationController(
428 this,
429 mutationOpts1,
430 queryClient,
431 )
432 readonly mutation2 = createMutationController(
433 this,
434 mutationOpts2,
435 queryClient,
436 )
437​
438 readonly mutationStates = useMutationState(
439 this,
440 {
441 filters: {
442 mutationKey: mutationOpts1.mutationKey,
443 status: 'success',
444 },
445 },
446 queryClient,
447 )
448 }
449​
450 customElements.define(generateElementName(), Host)
451 const host = new Host()
452 container.append(host)
453​
454 expect(host.mutationStates()).toHaveLength(0)
455​
456 host.mutation1.mutate()
457 host.mutation2.mutate()
458 await vi.advanceTimersByTimeAsync(10)
459 expect(host.mutationStates()).toHaveLength(1)
460 expect(host.mutationStates()[0]?.data).toBe('data1')
461 })
462})