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

useMutationState.test.tsx

251 lines6.0 KB

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