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

useIsMutating.test.ts

184 lines4.8 KB

1import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest'
2import { onScopeDispose, reactive, ref } from 'vue-demi'
3import { sleep } from '@tanstack/query-test-utils'
4import { useMutation } from '../useMutation'
5import { useIsMutating, useMutationState } from '../useMutationState'
6import { useQueryClient } from '../useQueryClient'
7import type { MockedFunction } from 'vitest'
8​
9vi.mock('../useQueryClient')
10​
11describe('useIsMutating', () => {
12 beforeEach(() => {
13 vi.useFakeTimers()
14 })
15​
16 afterEach(() => {
17 vi.useRealTimers()
18 })
19​
20 test('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​
34 await vi.advanceTimersByTimeAsync(0)
35​
36 expect(isMutating.value).toStrictEqual(2)
37​
38 await vi.advanceTimersByTimeAsync(10)
39​
40 expect(isMutating.value).toStrictEqual(0)
41 })
42​
43 test('should stop listening to changes on onScopeDispose', async () => {
44 const onScopeDisposeMock = onScopeDispose as MockedFunction<
45 typeof onScopeDispose
46 >
47 onScopeDisposeMock.mockImplementation((fn) => fn())
48​
49 const mutation = useMutation({
50 mutationFn: (params: string) => sleep(0).then(() => params),
51 })
52 const mutation2 = useMutation({
53 mutationFn: (params: string) => sleep(0).then(() => params),
54 })
55 const isMutating = useIsMutating()
56​
57 expect(isMutating.value).toStrictEqual(0)
58​
59 mutation.mutateAsync('a')
60 mutation2.mutateAsync('b')
61​
62 await vi.advanceTimersByTimeAsync(0)
63​
64 expect(isMutating.value).toStrictEqual(0)
65​
66 await vi.advanceTimersByTimeAsync(0)
67​
68 expect(isMutating.value).toStrictEqual(0)
69​
70 onScopeDisposeMock.mockReset()
71 })
72​
73 test('should properly update filters', async () => {
74 const filter = reactive({ mutationKey: ['foo'] })
75 const { mutate } = useMutation({
76 mutationKey: ['isMutating'],
77 mutationFn: (params: string) => sleep(10).then(() => params),
78 })
79 mutate('foo')
80​
81 const isMutating = useIsMutating(filter)
82​
83 expect(isMutating.value).toStrictEqual(0)
84​
85 filter.mutationKey = ['isMutating']
86​
87 await vi.advanceTimersByTimeAsync(0)
88​
89 expect(isMutating.value).toStrictEqual(1)
90 })
91​
92 test('should work with options getter and be reactive', async () => {
93 const keyRef = ref('isMutatingGetter2')
94 const { mutate } = useMutation({
95 mutationKey: ['isMutatingGetter'],
96 mutationFn: (params: string) => sleep(10).then(() => params),
97 })
98 mutate('foo')
99​
100 const isMutating = useIsMutating(() => ({
101 mutationKey: [keyRef.value],
102 }))
103​
104 expect(isMutating.value).toStrictEqual(0)
105​
106 keyRef.value = 'isMutatingGetter'
107​
108 await vi.advanceTimersByTimeAsync(0)
109​
110 expect(isMutating.value).toStrictEqual(1)
111 })
112})
113​
114describe('useMutationState', () => {
115 beforeEach(() => {
116 vi.useFakeTimers()
117 })
118​
119 afterEach(() => {
120 vi.useRealTimers()
121 })
122​
123 it('should return variables after calling mutate 1', () => {
124 const mutationKey = ['mutation']
125 const variables = 'foo123'
126​
127 const { mutate } = useMutation({
128 mutationKey: mutationKey,
129 mutationFn: (params: string) => sleep(0).then(() => params),
130 })
131​
132 mutate(variables)
133​
134 const mutationState = useMutationState({
135 filters: { mutationKey, status: 'pending' },
136 select: (mutation) => mutation.state.variables,
137 })
138​
139 expect(mutationState.value).toEqual([variables])
140 })
141​
142 it('should return variables after calling mutate 2', () => {
143 const queryClient = useQueryClient()
144 queryClient.clear()
145 const mutationKey = ['mutation']
146 const variables = 'bar234'
147​
148 const { mutate } = useMutation({
149 mutationKey: mutationKey,
150 mutationFn: (params: string) => sleep(0).then(() => params),
151 })
152​
153 mutate(variables)
154​
155 const mutationState = useMutationState()
156​
157 expect(mutationState.value[0]?.variables).toEqual(variables)
158 })
159​
160 it('should work with options getter and be reactive', async () => {
161 const keyRef = ref('useMutationStateGetter2')
162 const variables = 'foo123'
163​
164 const { mutate } = useMutation({
165 mutationKey: ['useMutationStateGetter'],
166 mutationFn: (params: string) => sleep(10).then(() => params),
167 })
168​
169 mutate(variables)
170​
171 const mutationState = useMutationState(() => ({
172 filters: { mutationKey: [keyRef.value], status: 'pending' },
173 select: (mutation) => mutation.state.variables,
174 }))
175​
176 expect(mutationState.value).toEqual([])
177​
178 keyRef.value = 'useMutationStateGetter'
179​
180 await vi.advanceTimersByTimeAsync(0)
181​
182 expect(mutationState.value).toEqual([variables])
183 })
184})