Open History to see the last commit
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})