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

useMutationState.test.tsx

238 lines5.9 KB

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