Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@solidjs/testing-library'
3import { createRenderEffect } from 'solid-js'
4import { createKey, sleep } from '@tanstack/query-test-utils'
5import { QueryClient, useIsMutating, useMutation } from '..'
6import { renderWithClient, setActTimeout } from './utils'
7import type { MutationOptions } from '@tanstack/query-core'
8β
9/**
10 * Drives a mutation through the mutation cache directly, outside any
11 * useMutation action transaction. Cache events emitted during a
12 * useMutation flight are delivered inside the action's transaction, where
13 * useIsMutating's plain-signal write is held until settle β the in-flight
14 * count never commits to the DOM (see the skipped test below). Feeding the
15 * cache directly keeps the hook's own contract observable.
16 */
17function startMutation<TData, TVariables>(
18 client: QueryClient,
19 options: MutationOptions<TData, Error, TVariables>,
20 variables: TVariables,
21): Promise<TData> {
22 return client.getMutationCache().build(client, options).execute(variables)
23}
24β
25describe('useIsMutating', () => {
26 let queryClient: QueryClient
27β
28 beforeEach(() => {
29 vi.useFakeTimers()
30 queryClient = new QueryClient()
31 })
32β
33 afterEach(() => {
34 queryClient.clear()
35 vi.useRealTimers()
36 })
37β
38 it('should return the number of fetching mutations', async () => {
39 const mutationKey1 = createKey()
40 const mutationKey2 = createKey()
41β
42 function Page() {
43 const isMutating = useIsMutating()
44β
45 setActTimeout(() => {
46 startMutation(
47 queryClient,
48 {
49 mutationKey: mutationKey1,
50 mutationFn: () => sleep(150).then(() => 'data'),
51 },
52 undefined,
53 )
54 }, 0)
55 setActTimeout(() => {
56 startMutation(
57 queryClient,
58 {
59 mutationKey: mutationKey2,
60 mutationFn: () => sleep(50).then(() => 'data'),
61 },
62 undefined,
63 )
64 }, 50)
65β
66 return <div>mutating: {isMutating()}</div>
67 }
68β
69 const rendered = renderWithClient(queryClient, () => <Page />)
70β
71 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
72β
73 // t=0: mutation1 in flight
74 await vi.advanceTimersByTimeAsync(0)
75 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
76β
77 // t=50: mutation2 joins mutation1 in flight
78 await vi.advanceTimersByTimeAsync(50)
79 expect(rendered.getByText('mutating: 2')).toBeInTheDocument()
80β
81 // t=100: mutation2 settles
82 await vi.advanceTimersByTimeAsync(50)
83 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
84β
85 // t=150: mutation1 settles
86 await vi.advanceTimersByTimeAsync(50)
87 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
88 })
89β
90 it('should filter correctly by mutationKey', async () => {
91 const isMutatingArray: Array<number> = []
92 const mutationKey1 = createKey()
93 const mutationKey2 = createKey()
94β
95 function Page() {
96 const isMutating = useIsMutating(() => ({ mutationKey: mutationKey1 }))
97β
98 createRenderEffect(isMutating, (i) => {
99 isMutatingArray.push(i)
100 })
101β
102 setActTimeout(() => {
103 startMutation(
104 queryClient,
105 {
106 mutationKey: mutationKey1,
107 mutationFn: () => sleep(100).then(() => 'data'),
108 },
109 undefined,
110 )
111 startMutation(
112 queryClient,
113 {
114 mutationKey: mutationKey2,
115 mutationFn: () => sleep(100).then(() => 'data'),
116 },
117 undefined,
118 )
119 }, 10)
120β
121 return <div>mutating: {isMutating()}</div>
122 }
123β
124 const rendered = renderWithClient(queryClient, () => <Page />)
125β
126 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
127β
128 // t=10: both mutations in flight, only mutationKey1 counted
129 await vi.advanceTimersByTimeAsync(10)
130 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
131β
132 // t=110: both settled
133 await vi.advanceTimersByTimeAsync(100)
134 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
135β
136 // the mutation with the other key must never leak through the filter
137 expect(isMutatingArray).toStrictEqual(expect.not.arrayContaining([2]))
138 })
139β
140 it('should filter correctly by predicate', async () => {
141 const isMutatingArray: Array<number> = []
142 const mutationKey1 = createKey()
143 const mutationKey2 = createKey()
144β
145 function Page() {
146 const isMutating = useIsMutating(() => ({
147 predicate: (mutation) =>
148 mutation.options.mutationKey?.[0] === mutationKey1[0],
149 }))
150β
151 createRenderEffect(isMutating, (i) => {
152 isMutatingArray.push(i)
153 })
154β
155 setActTimeout(() => {
156 startMutation(
157 queryClient,
158 {
159 mutationKey: mutationKey1,
160 mutationFn: () => sleep(100).then(() => 'data'),
161 },
162 undefined,
163 )
164 startMutation(
165 queryClient,
166 {
167 mutationKey: mutationKey2,
168 mutationFn: () => sleep(100).then(() => 'data'),
169 },
170 undefined,
171 )
172 }, 10)
173β
174 return <div>mutating: {isMutating()}</div>
175 }
176β
177 const rendered = renderWithClient(queryClient, () => <Page />)
178β
179 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
180β
181 // t=10: both mutations in flight, only the predicate match counted
182 await vi.advanceTimersByTimeAsync(10)
183 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
184β
185 // t=110: both settled
186 await vi.advanceTimersByTimeAsync(100)
187 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
188β
189 // the non-matching mutation must never be counted
190 expect(isMutatingArray).toStrictEqual(expect.not.arrayContaining([2]))
191 })
192β
193 it('should use provided custom queryClient', async () => {
194 const customClient = new QueryClient()
195 const mutationKey1 = createKey()
196β
197 function Page() {
198 const isMutating = useIsMutating(undefined, () => customClient)
199β
200 setActTimeout(() => {
201 startMutation(
202 customClient,
203 {
204 mutationKey: mutationKey1,
205 mutationFn: () => sleep(20).then(() => 'data'),
206 },
207 undefined,
208 )
209 }, 10)
210β
211 return (
212 <div>
213 <div>mutating: {isMutating()}</div>
214 </div>
215 )
216 }
217β
218 const rendered = render(() => <Page />)
219β
220 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
221 await vi.advanceTimersByTimeAsync(10)
222 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
223 await vi.advanceTimersByTimeAsync(20)
224 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
225 })
226β
227 it('should count mutations started by useMutation while in flight', async () => {
228 const mutationKey1 = createKey()
229β
230 function Page() {
231 const isMutating = useIsMutating()
232 const mutation = useMutation(() => ({
233 mutationKey: mutationKey1,
234 mutationFn: () => sleep(150).then(() => 'data'),
235 }))
236β
237 setActTimeout(() => {
238 mutation.mutate()
239 }, 0)
240β
241 return <div>mutating: {isMutating()}</div>
242 }
243β
244 const rendered = renderWithClient(queryClient, () => <Page />)
245β
246 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
247β
248 await vi.advanceTimersByTimeAsync(0)
249 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
250β
251 await vi.advanceTimersByTimeAsync(150)
252 expect(rendered.getByText('mutating: 0')).toBeInTheDocument()
253 })
254})