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

useMutationState.test.tsx

244 lines6.1 KB

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