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

useMutationState.test.tsx

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