Open History to see the last commit
1import { createKey, sleep } from '@tanstack/query-test-utils'
2import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3import { onScopeDispose, reactive, ref } from 'vue-demi'
4import { QueryClient } from '../queryClient'
5import { useMutation } from '../useMutation'
6import { useIsMutating } from '../useMutationState'
7import { useQueryClient } from '../useQueryClient'
8β
9vi.mock(import('../useQueryClient'))
10β
11describe('useIsMutating', () => {
12 beforeEach(() => {
13 vi.useFakeTimers()
14 })
15β
16 afterEach(() => {
17 vi.useRealTimers()
18 })
19β
20 it('should properly return isMutating state', async () => {
21 const mutation = useMutation({
22 mutationFn: (params: string) => sleep(10).then(() => params),
23 })
24 const mutation2 = useMutation({
25 mutationFn: (params: string) => sleep(10).then(() => params),
26 })
27 const isMutating = useIsMutating()
28β
29 expect(isMutating.value).toBe(0)
30β
31 mutation.mutateAsync('a')
32 mutation2.mutateAsync('b')
33 await vi.advanceTimersByTimeAsync(0)
34 expect(isMutating.value).toBe(2)
35 await vi.advanceTimersByTimeAsync(10)
36 expect(isMutating.value).toBe(0)
37 })
38β
39 it('should stop listening to changes on onScopeDispose', async () => {
40 const onScopeDisposeMock = vi.mocked(onScopeDispose)
41 onScopeDisposeMock.mockImplementation((fn) => fn())
42β
43 const key = createKey()
44 const queryClient = useQueryClient()
45 const mutation = useMutation({
46 mutationKey: key,
47 mutationFn: (params: string) => sleep(10).then(() => params),
48 })
49 const mutation2 = useMutation({
50 mutationKey: key,
51 mutationFn: (params: string) => sleep(10).then(() => params),
52 })
53 const isMutating = useIsMutating()
54β
55 expect(isMutating.value).toBe(0)
56β
57 mutation.mutateAsync('a')
58 mutation2.mutateAsync('b')
59 await vi.advanceTimersByTimeAsync(0)
60 expect(queryClient.isMutating({ mutationKey: key })).toBe(2)
61 expect(isMutating.value).toBe(0)
62 await vi.advanceTimersByTimeAsync(10)
63 expect(queryClient.isMutating({ mutationKey: key })).toBe(0)
64 expect(isMutating.value).toBe(0)
65β
66 onScopeDisposeMock.mockReset()
67 })
68β
69 it('should properly update filters', async () => {
70 const key = createKey()
71 const filterKey = createKey()
72 const filter = reactive({ mutationKey: filterKey })
73 const { mutate } = useMutation({
74 mutationKey: key,
75 mutationFn: (params: string) => sleep(10).then(() => params),
76 })
77 mutate('foo')
78β
79 const isMutating = useIsMutating(filter)
80β
81 expect(isMutating.value).toBe(0)
82β
83 filter.mutationKey = key
84 await vi.advanceTimersByTimeAsync(0)
85 expect(isMutating.value).toBe(1)
86 })
87β
88 it('should work with options getter and be reactive', async () => {
89 const key = createKey()
90 const keyRef = ref('isMutatingGetter2')
91 const { mutate } = useMutation({
92 mutationKey: key,
93 mutationFn: (params: string) => sleep(10).then(() => params),
94 })
95 mutate('foo')
96β
97 const isMutating = useIsMutating(() => ({
98 mutationKey: [keyRef.value],
99 }))
100β
101 expect(isMutating.value).toBe(0)
102β
103 keyRef.value = key[0]!
104 await vi.advanceTimersByTimeAsync(0)
105 expect(isMutating.value).toBe(1)
106 })
107β
108 it('should accept filters wrapped in a ref', async () => {
109 const key1 = createKey()
110 const key2 = createKey()
111 const mutation1 = useMutation({
112 mutationKey: key1,
113 mutationFn: (params: string) => sleep(10).then(() => params),
114 })
115 const mutation2 = useMutation({
116 mutationKey: key2,
117 mutationFn: (params: string) => sleep(10).then(() => params),
118 })
119 const isMutating = useIsMutating(ref({ mutationKey: key1 }))
120β
121 mutation1.mutate('a')
122 mutation2.mutate('b')
123 await vi.advanceTimersByTimeAsync(0)
124 expect(isMutating.value).toBe(1)
125 await vi.advanceTimersByTimeAsync(10)
126 expect(isMutating.value).toBe(0)
127 })
128β
129 it('should use the queryClient passed as the second argument', async () => {
130 const queryClient = new QueryClient()
131 const mutation = useMutation(
132 {
133 mutationFn: (params: string) => sleep(10).then(() => params),
134 },
135 queryClient,
136 )
137 const isMutating = useIsMutating({}, queryClient)
138β
139 expect(isMutating.value).toBe(0)
140β
141 mutation.mutate('a')
142 await vi.advanceTimersByTimeAsync(0)
143 expect(isMutating.value).toBe(1)
144 await vi.advanceTimersByTimeAsync(10)
145 expect(isMutating.value).toBe(0)
146 })
147})