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

useMutationState.test.tsx

246 lines6.2 KB

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