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

useIsMutating.test.ts

114 lines3.0 KB

1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { onScopeDispose, reactive, ref } from 'vue-demi'
3import { queryKey, sleep } from '@tanstack/query-test-utils'
4import { useMutation } from '../useMutation'
5import { useIsMutating } from '../useMutationState'
6import type { MockedFunction } from 'vitest'
7​
8vi.mock('../useQueryClient')
9​
10describe('useIsMutating', () => {
11 beforeEach(() => {
12 vi.useFakeTimers()
13 })
14​
15 afterEach(() => {
16 vi.useRealTimers()
17 })
18​
19 test('should properly return isMutating state', async () => {
20 const mutation = useMutation({
21 mutationFn: (params: string) => sleep(10).then(() => params),
22 })
23 const mutation2 = useMutation({
24 mutationFn: (params: string) => sleep(10).then(() => params),
25 })
26 const isMutating = useIsMutating()
27​
28 expect(isMutating.value).toStrictEqual(0)
29​
30 mutation.mutateAsync('a')
31 mutation2.mutateAsync('b')
32​
33 await vi.advanceTimersByTimeAsync(0)
34​
35 expect(isMutating.value).toStrictEqual(2)
36​
37 await vi.advanceTimersByTimeAsync(10)
38​
39 expect(isMutating.value).toStrictEqual(0)
40 })
41​
42 test('should stop listening to changes on onScopeDispose', async () => {
43 const onScopeDisposeMock = onScopeDispose as MockedFunction<
44 typeof onScopeDispose
45 >
46 onScopeDisposeMock.mockImplementation((fn) => fn())
47​
48 const mutation = useMutation({
49 mutationFn: (params: string) => sleep(0).then(() => params),
50 })
51 const mutation2 = useMutation({
52 mutationFn: (params: string) => sleep(0).then(() => params),
53 })
54 const isMutating = useIsMutating()
55​
56 expect(isMutating.value).toStrictEqual(0)
57​
58 mutation.mutateAsync('a')
59 mutation2.mutateAsync('b')
60​
61 await vi.advanceTimersByTimeAsync(0)
62​
63 expect(isMutating.value).toStrictEqual(0)
64​
65 await vi.advanceTimersByTimeAsync(0)
66​
67 expect(isMutating.value).toStrictEqual(0)
68​
69 onScopeDisposeMock.mockReset()
70 })
71​
72 test('should properly update filters', async () => {
73 const key = queryKey()
74 const filterKey = queryKey()
75 const filter = reactive({ mutationKey: filterKey })
76 const { mutate } = useMutation({
77 mutationKey: key,
78 mutationFn: (params: string) => sleep(10).then(() => params),
79 })
80 mutate('foo')
81​
82 const isMutating = useIsMutating(filter)
83​
84 expect(isMutating.value).toStrictEqual(0)
85​
86 filter.mutationKey = key
87​
88 await vi.advanceTimersByTimeAsync(0)
89​
90 expect(isMutating.value).toStrictEqual(1)
91 })
92​
93 test('should work with options getter and be reactive', async () => {
94 const key = queryKey()
95 const keyRef = ref('isMutatingGetter2')
96 const { mutate } = useMutation({
97 mutationKey: key,
98 mutationFn: (params: string) => sleep(10).then(() => params),
99 })
100 mutate('foo')
101​
102 const isMutating = useIsMutating(() => ({
103 mutationKey: [keyRef.value],
104 }))
105​
106 expect(isMutating.value).toStrictEqual(0)
107​
108 keyRef.value = key[0]!
109​
110 await vi.advanceTimersByTimeAsync(0)
111​
112 expect(isMutating.value).toStrictEqual(1)
113 })
114})