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