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

mutationOptions.test.ts

450 lines13.1 KB

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