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

useIsMutating.test.ts

147 lines4.4 KB

1import { afterEach, beforeEach, describe, expect, it, 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 { useQueryClient } from '../useQueryClient'
7import { QueryClient } from '../queryClient'
8​
9vi.mock('../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).toStrictEqual(0)
30​
31 mutation.mutateAsync('a')
32 mutation2.mutateAsync('b')
33 await vi.advanceTimersByTimeAsync(0)
34 expect(isMutating.value).toStrictEqual(2)
35 await vi.advanceTimersByTimeAsync(10)
36 expect(isMutating.value).toStrictEqual(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 = queryKey()
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).toStrictEqual(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).toStrictEqual(0)
62 await vi.advanceTimersByTimeAsync(10)
63 expect(queryClient.isMutating({ mutationKey: key })).toBe(0)
64 expect(isMutating.value).toStrictEqual(0)
65​
66 onScopeDisposeMock.mockReset()
67 })
68​
69 it('should properly update filters', async () => {
70 const key = queryKey()
71 const filterKey = queryKey()
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).toStrictEqual(0)
82​
83 filter.mutationKey = key
84 await vi.advanceTimersByTimeAsync(0)
85 expect(isMutating.value).toStrictEqual(1)
86 })
87​
88 it('should work with options getter and be reactive', async () => {
89 const key = queryKey()
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).toStrictEqual(0)
102​
103 keyRef.value = key[0]!
104 await vi.advanceTimersByTimeAsync(0)
105 expect(isMutating.value).toStrictEqual(1)
106 })
107​
108 it('should accept filters wrapped in a ref', async () => {
109 const key1 = queryKey()
110 const key2 = queryKey()
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).toStrictEqual(1)
125 await vi.advanceTimersByTimeAsync(10)
126 expect(isMutating.value).toStrictEqual(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).toStrictEqual(0)
140​
141 mutation.mutate('a')
142 await vi.advanceTimersByTimeAsync(0)
143 expect(isMutating.value).toStrictEqual(1)
144 await vi.advanceTimersByTimeAsync(10)
145 expect(isMutating.value).toStrictEqual(0)
146 })
147})