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

useMutationState.test.tsx

222 lines5.8 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.waitFor(() => expect(isMutatingArray[0]).toEqual(0))
66 await vi.waitFor(() => expect(isMutatingArray[1]).toEqual(1))
67 await vi.waitFor(() => expect(isMutatingArray[2]).toEqual(2))
68 await vi.waitFor(() =>
69 expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0),
70 )
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.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
103 })
104​
105 it('should filter correctly by predicate', async () => {
106 const isMutatingArray: Array<number> = []
107 const queryClient = new QueryClient()
108​
109 function IsMutating() {
110 const isMutating = useIsMutating({
111 predicate: (mutation) =>
112 mutation.options.mutationKey?.[0] === 'mutation1',
113 })
114 isMutatingArray.push(isMutating)
115 return null
116 }
117​
118 function Page() {
119 const { mutate: mutate1 } = useMutation({
120 mutationKey: ['mutation1'],
121 mutationFn: () => sleep(100).then(() => 'data'),
122 })
123 const { mutate: mutate2 } = useMutation({
124 mutationKey: ['mutation2'],
125 mutationFn: () => sleep(100).then(() => 'data'),
126 })
127​
128 React.useEffect(() => {
129 mutate1()
130 mutate2()
131 }, [mutate1, mutate2])
132​
133 return <IsMutating />
134 }
135​
136 renderWithClient(queryClient, <Page />)
137 await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
138 })
139​
140 it('should use provided custom queryClient', async () => {
141 const queryClient = new QueryClient()
142​
143 function Page() {
144 const isMutating = useIsMutating({}, queryClient)
145 const { mutate } = useMutation(
146 {
147 mutationKey: ['mutation1'],
148 mutationFn: () => sleep(10).then(() => 'data'),
149 },
150 queryClient,
151 )
152​
153 React.useEffect(() => {
154 mutate()
155 }, [mutate])
156​
157 return (
158 <div>
159 <div>mutating: {isMutating}</div>
160 </div>
161 )
162 }
163​
164 const rendered = render(<Page></Page>)
165​
166 await vi.waitFor(() =>
167 expect(rendered.getByText('mutating: 1')).toBeInTheDocument(),
168 )
169 })
170})
171​
172describe('useMutationState', () => {
173 it('should return variables after calling mutate', async () => {
174 const queryClient = new QueryClient()
175 const variables: Array<Array<unknown>> = []
176 const mutationKey = ['mutation']
177​
178 function Variables() {
179 variables.push(
180 useMutationState({
181 filters: { mutationKey, status: 'pending' },
182 select: (mutation) => mutation.state.variables,
183 }),
184 )
185​
186 return null
187 }
188​
189 function Mutate() {
190 const { mutate, data } = useMutation({
191 mutationKey,
192 mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
193 })
194​
195 return (
196 <div>
197 data: {data ?? 'null'}
198 <button onClick={() => mutate(1)}>mutate</button>
199 </div>
200 )
201 }
202​
203 function Page() {
204 return (
205 <div>
206 <Variables />
207 <Mutate />
208 </div>
209 )
210 }
211​
212 const rendered = renderWithClient(queryClient, <Page />)
213​
214 await vi.waitFor(() => rendered.getByText('data: null'))
215​
216 fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
217​
218 await vi.waitFor(() => rendered.getByText('data: data1'))
219​
220 expect(variables).toEqual([[], [1], []])
221 })
222})