Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { fireEvent, render } from '@testing-library/react'
3import * as React from 'react'
4import { sleep } from '@tanstack/query-test-utils'
5import { QueryClient, useIsMutating, useMutation, useMutationState } from '..'
6import { renderWithClient } from './utils'
7β
8describe('useIsMutating', () => {
9 beforeEach(() => {
10 vi.useFakeTimers()
11 })
12β
13 afterEach(() => {
14 vi.useRealTimers()
15 })
16β
17 it('should return the number of fetching mutations', async () => {
18 const isMutatingArray: Array<number> = []
19 const queryClient = new QueryClient()
20β
21 function IsMutating() {
22 const isMutating = useIsMutating()
23 isMutatingArray.push(isMutating)
24 return null
25 }
26β
27 function Mutations() {
28 const { mutate: mutate1 } = useMutation({
29 mutationKey: ['mutation1'],
30 mutationFn: () => sleep(50).then(() => 'data'),
31 })
32 const { mutate: mutate2 } = useMutation({
33 mutationKey: ['mutation2'],
34 mutationFn: () => sleep(10).then(() => 'data'),
35 })
36β
37 return (
38 <div>
39 <button onClick={() => mutate1()}>mutate1</button>
40 <button onClick={() => mutate2()}>mutate2</button>
41 </div>
42 )
43 }
44β
45 function Page() {
46 return (
47 <div>
48 <IsMutating />
49 <Mutations />
50 </div>
51 )
52 }
53β
54 const rendered = renderWithClient(queryClient, <Page />)
55 fireEvent.click(rendered.getByRole('button', { name: /mutate1/i }))
56 await vi.advanceTimersByTimeAsync(10)
57 fireEvent.click(rendered.getByRole('button', { name: /mutate2/i }))
58β
59 // we don't really care if this yields
60 // [ +0, 1, 2, +0 ]
61 // or
62 // [ +0, 1, 2, 1, +0 ]
63 // our batching strategy might yield different results
64β
65 await vi.advanceTimersByTimeAsync(40)
66 expect(isMutatingArray[0]).toEqual(0)
67 expect(isMutatingArray[1]).toEqual(1)
68 expect(isMutatingArray[2]).toEqual(2)
69 await vi.advanceTimersByTimeAsync(1)
70 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
71 })
72β
73 it('should filter correctly by mutationKey', async () => {
74 const isMutatingArray: Array<number> = []
75 const queryClient = new QueryClient()
76β
77 function IsMutating() {
78 const isMutating = useIsMutating({ mutationKey: ['mutation1'] })
79 isMutatingArray.push(isMutating)
80 return null
81 }
82β
83 function Page() {
84 const { mutate: mutate1 } = useMutation({
85 mutationKey: ['mutation1'],
86 mutationFn: () => sleep(100).then(() => 'data'),
87 })
88 const { mutate: mutate2 } = useMutation({
89 mutationKey: ['mutation2'],
90 mutationFn: () => sleep(100).then(() => 'data'),
91 })
92β
93 React.useEffect(() => {
94 mutate1()
95 mutate2()
96 }, [mutate1, mutate2])
97β
98 return <IsMutating />
99 }
100β
101 renderWithClient(queryClient, <Page />)
102 await vi.advanceTimersByTimeAsync(101)
103 expect(isMutatingArray).toEqual([0, 1, 0])
104 })
105β
106 it('should filter correctly by predicate', async () => {
107 const isMutatingArray: Array<number> = []
108 const queryClient = new QueryClient()
109β
110 function IsMutating() {
111 const isMutating = useIsMutating({
112 predicate: (mutation) =>
113 mutation.options.mutationKey?.[0] === 'mutation1',
114 })
115 isMutatingArray.push(isMutating)
116 return null
117 }
118β
119 function Page() {
120 const { mutate: mutate1 } = useMutation({
121 mutationKey: ['mutation1'],
122 mutationFn: () => sleep(100).then(() => 'data'),
123 })
124 const { mutate: mutate2 } = useMutation({
125 mutationKey: ['mutation2'],
126 mutationFn: () => sleep(100).then(() => 'data'),
127 })
128β
129 React.useEffect(() => {
130 mutate1()
131 mutate2()
132 }, [mutate1, mutate2])
133β
134 return <IsMutating />
135 }
136β
137 renderWithClient(queryClient, <Page />)
138 await vi.advanceTimersByTimeAsync(101)
139 expect(isMutatingArray).toEqual([0, 1, 0])
140 })
141β
142 it('should use provided custom queryClient', async () => {
143 const queryClient = new QueryClient()
144β
145 function Page() {
146 const isMutating = useIsMutating({}, queryClient)
147 const { mutate } = useMutation(
148 {
149 mutationKey: ['mutation1'],
150 mutationFn: () => sleep(10).then(() => 'data'),
151 },
152 queryClient,
153 )
154β
155 React.useEffect(() => {
156 mutate()
157 }, [mutate])
158β
159 return (
160 <div>
161 <div>mutating: {isMutating}</div>
162 </div>
163 )
164 }
165β
166 const rendered = render(<Page></Page>)
167β
168 await vi.advanceTimersByTimeAsync(0)
169 expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
170 })
171})
172β
173describe('useMutationState', () => {
174 beforeEach(() => {
175 vi.useFakeTimers()
176 })
177β
178 afterEach(() => {
179 vi.useRealTimers()
180 })
181β
182 it('should return variables after calling mutate', async () => {
183 const queryClient = new QueryClient()
184 const variables: Array<Array<unknown>> = []
185 const mutationKey = ['mutation']
186β
187 function Variables() {
188 variables.push(
189 useMutationState({
190 filters: { mutationKey, status: 'pending' },
191 select: (mutation) => mutation.state.variables,
192 }),
193 )
194β
195 return null
196 }
197β
198 function Mutate() {
199 const { mutate, data } = useMutation({
200 mutationKey,
201 mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
202 })
203β
204 return (
205 <div>
206 data: {data ?? 'null'}
207 <button onClick={() => mutate(1)}>mutate</button>
208 </div>
209 )
210 }
211β
212 function Page() {
213 return (
214 <div>
215 <Variables />
216 <Mutate />
217 </div>
218 )
219 }
220β
221 const rendered = renderWithClient(queryClient, <Page />)
222β
223 rendered.getByText('data: null')
224β
225 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
226β
227 await vi.advanceTimersByTimeAsync(151)
228 rendered.getByText('data: data1')
229β
230 expect(variables).toEqual([[], [1], []])
231 })
232})