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

mutation-options.test.ts

327 lines10.2 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { TestBed } from '@angular/core/testing'
3import { QueryClient } from '@tanstack/query-core'
4import { sleep } from '@tanstack/query-test-utils'
5import {
6 injectIsMutating,
7 injectMutation,
8 injectMutationState,
9 mutationOptions,
10} from '..'
11import { flushQueryUpdates, setupTanStackQueryTestBed } from './test-utils'
12​
13describe('mutationOptions', () => {
14 let queryClient: QueryClient
15​
16 beforeEach(() => {
17 vi.useFakeTimers()
18 queryClient = new QueryClient()
19 setupTanStackQueryTestBed(queryClient)
20 })
21​
22 afterEach(() => {
23 vi.useRealTimers()
24 })
25​
26 it('should return the object received as a parameter without any modification (with mutationKey in mutationOptions)', () => {
27 const object = {
28 mutationKey: ['key'],
29 mutationFn: () => sleep(10).then(() => 5),
30 } as const
31​
32 expect(mutationOptions(object)).toStrictEqual(object)
33 })
34​
35 it('should return the object received as a parameter without any modification (without mutationKey in mutationOptions)', () => {
36 const object = {
37 mutationFn: () => sleep(10).then(() => 5),
38 } as const
39​
40 expect(mutationOptions(object)).toStrictEqual(object)
41 })
42​
43 it('should return the number of fetching mutations when used with injectIsMutating (with mutationKey in mutationOptions)', async () => {
44 const mutationOpts = mutationOptions({
45 mutationKey: ['key'],
46 mutationFn: () => sleep(50).then(() => 'data'),
47 })
48​
49 const [mutation, isMutating] = TestBed.runInInjectionContext(() => [
50 injectMutation(() => mutationOpts),
51 injectIsMutating(),
52 ])
53​
54 expect(isMutating()).toBe(0)
55​
56 mutation.mutate()
57 expect(isMutating()).toBe(0)
58 await flushQueryUpdates()
59 expect(isMutating()).toBe(1)
60 await vi.advanceTimersByTimeAsync(51)
61 expect(isMutating()).toBe(0)
62 })
63​
64 it('should return the number of fetching mutations when used with injectIsMutating (without mutationKey in mutationOptions)', async () => {
65 const mutationOpts = mutationOptions({
66 mutationFn: () => sleep(50).then(() => 'data'),
67 })
68​
69 const [mutation, isMutating] = TestBed.runInInjectionContext(() => [
70 injectMutation(() => mutationOpts),
71 injectIsMutating(),
72 ])
73​
74 expect(isMutating()).toBe(0)
75​
76 mutation.mutate()
77 expect(isMutating()).toBe(0)
78 await flushQueryUpdates()
79 expect(isMutating()).toBe(1)
80 await vi.advanceTimersByTimeAsync(51)
81 expect(isMutating()).toBe(0)
82 })
83​
84 it('should return the number of fetching mutations when used with injectIsMutating', async () => {
85 const mutationOpts1 = mutationOptions({
86 mutationKey: ['key'],
87 mutationFn: () => sleep(50).then(() => 'data1'),
88 })
89 const mutationOpts2 = mutationOptions({
90 mutationFn: () => sleep(50).then(() => 'data2'),
91 })
92​
93 const [mutation1, mutation2, isMutating] = TestBed.runInInjectionContext(
94 () => [
95 injectMutation(() => mutationOpts1),
96 injectMutation(() => mutationOpts2),
97 injectIsMutating(),
98 ],
99 )
100​
101 expect(isMutating()).toBe(0)
102​
103 mutation1.mutate()
104 mutation2.mutate()
105 expect(isMutating()).toBe(0)
106 await flushQueryUpdates()
107 expect(isMutating()).toBe(2)
108 await vi.advanceTimersByTimeAsync(51)
109 expect(isMutating()).toBe(0)
110 })
111​
112 it('should return the number of fetching mutations when used with injectIsMutating (filter mutationOpts1.mutationKey)', async () => {
113 const mutationOpts1 = mutationOptions({
114 mutationKey: ['key'],
115 mutationFn: () => sleep(50).then(() => 'data1'),
116 })
117 const mutationOpts2 = mutationOptions({
118 mutationFn: () => sleep(50).then(() => 'data2'),
119 })
120​
121 const [mutation1, mutation2, isMutating] = TestBed.runInInjectionContext(
122 () => [
123 injectMutation(() => mutationOpts1),
124 injectMutation(() => mutationOpts2),
125 injectIsMutating({ mutationKey: mutationOpts1.mutationKey }),
126 ],
127 )
128​
129 expect(isMutating()).toBe(0)
130​
131 mutation1.mutate()
132 mutation2.mutate()
133 expect(isMutating()).toBe(0)
134 await flushQueryUpdates()
135 expect(isMutating()).toBe(1)
136 await vi.advanceTimersByTimeAsync(51)
137 expect(isMutating()).toBe(0)
138 })
139​
140 it('should return the number of fetching mutations when used with queryClient.isMutating (with mutationKey in mutationOptions)', async () => {
141 const mutationOpts = mutationOptions({
142 mutationKey: ['mutation'],
143 mutationFn: () => sleep(500).then(() => 'data'),
144 })
145​
146 const mutation = TestBed.runInInjectionContext(() =>
147 injectMutation(() => mutationOpts),
148 )
149​
150 expect(queryClient.isMutating(mutationOpts)).toBe(0)
151​
152 mutation.mutate()
153 expect(queryClient.isMutating(mutationOpts)).toBe(1)
154 await vi.advanceTimersByTimeAsync(501)
155 expect(queryClient.isMutating(mutationOpts)).toBe(0)
156 })
157​
158 it('should return the number of fetching mutations when used with queryClient.isMutating (without mutationKey in mutationOptions)', async () => {
159 const mutationOpts = mutationOptions({
160 mutationFn: () => sleep(500).then(() => 'data'),
161 })
162​
163 const mutation = TestBed.runInInjectionContext(() =>
164 injectMutation(() => mutationOpts),
165 )
166​
167 expect(queryClient.isMutating()).toBe(0)
168​
169 mutation.mutate()
170 expect(queryClient.isMutating()).toBe(1)
171 await vi.advanceTimersByTimeAsync(501)
172 expect(queryClient.isMutating()).toBe(0)
173 })
174​
175 it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
176 const mutationOpts1 = mutationOptions({
177 mutationKey: ['mutation'],
178 mutationFn: () => sleep(500).then(() => 'data1'),
179 })
180 const mutationOpts2 = mutationOptions({
181 mutationFn: () => sleep(500).then(() => 'data2'),
182 })
183​
184 const [mutation1, mutation2] = TestBed.runInInjectionContext(() => [
185 injectMutation(() => mutationOpts1),
186 injectMutation(() => mutationOpts2),
187 ])
188​
189 expect(queryClient.isMutating()).toBe(0)
190​
191 mutation1.mutate()
192 mutation2.mutate()
193 expect(queryClient.isMutating()).toBe(2)
194 await vi.advanceTimersByTimeAsync(501)
195 expect(queryClient.isMutating()).toBe(0)
196 })
197​
198 it('should return the number of fetching mutations when used with queryClient.isMutating (filter mutationOpt1.mutationKey)', async () => {
199 const mutationOpts1 = mutationOptions({
200 mutationKey: ['mutation'],
201 mutationFn: () => sleep(500).then(() => 'data1'),
202 })
203 const mutationOpts2 = mutationOptions({
204 mutationFn: () => sleep(500).then(() => 'data2'),
205 })
206​
207 const [mutation1, mutation2] = TestBed.runInInjectionContext(() => [
208 injectMutation(() => mutationOpts1),
209 injectMutation(() => mutationOpts2),
210 ])
211​
212 expect(
213 queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
214 ).toBe(0)
215​
216 mutation1.mutate()
217 mutation2.mutate()
218 expect(
219 queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
220 ).toBe(1)
221 await vi.advanceTimersByTimeAsync(501)
222 expect(
223 queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
224 ).toBe(0)
225 })
226​
227 it('should return the number of fetching mutations when used with injectMutationState (with mutationKey in mutationOptions)', async () => {
228 const mutationOpts = mutationOptions({
229 mutationKey: ['mutation'],
230 mutationFn: () => sleep(10).then(() => 'data'),
231 })
232​
233 const [mutation, mutationState] = TestBed.runInInjectionContext(() => [
234 injectMutation(() => mutationOpts),
235 injectMutationState(() => ({
236 filters: { mutationKey: mutationOpts.mutationKey, status: 'success' },
237 })),
238 ])
239​
240 expect(mutationState().length).toBe(0)
241​
242 mutation.mutate()
243 await vi.advanceTimersByTimeAsync(11)
244 expect(mutationState().length).toBe(1)
245 expect(mutationState()[0]?.data).toBe('data')
246 })
247​
248 it('should return the number of fetching mutations when used with injectMutationState (without mutationKey in mutationOptions)', async () => {
249 const mutationOpts = mutationOptions({
250 mutationFn: () => sleep(10).then(() => 'data'),
251 })
252​
253 const [mutation, mutationState] = TestBed.runInInjectionContext(() => [
254 injectMutation(() => mutationOpts),
255 injectMutationState(() => ({
256 filters: { status: 'success' },
257 })),
258 ])
259​
260 expect(mutationState().length).toBe(0)
261​
262 mutation.mutate()
263 await vi.advanceTimersByTimeAsync(11)
264 expect(mutationState().length).toBe(1)
265 expect(mutationState()[0]?.data).toBe('data')
266 })
267​
268 it('should return the number of fetching mutations when used with injectMutationState', async () => {
269 const mutationOpts1 = mutationOptions({
270 mutationKey: ['mutation'],
271 mutationFn: () => sleep(10).then(() => 'data1'),
272 })
273 const mutationOpts2 = mutationOptions({
274 mutationFn: () => sleep(10).then(() => 'data2'),
275 })
276​
277 const [mutation1, mutation2, mutationState] = TestBed.runInInjectionContext(
278 () => [
279 injectMutation(() => mutationOpts1),
280 injectMutation(() => mutationOpts2),
281 injectMutationState(() => ({
282 filters: { status: 'success' },
283 })),
284 ],
285 )
286​
287 expect(mutationState().length).toBe(0)
288​
289 mutation1.mutate()
290 mutation2.mutate()
291 await vi.advanceTimersByTimeAsync(11)
292 expect(mutationState().length).toBe(2)
293 expect(mutationState()[0]?.data).toBe('data1')
294 expect(mutationState()[1]?.data).toBe('data2')
295 })
296​
297 it('should return the number of fetching mutations when used with injectMutationState (filter mutationOpt1.mutationKey)', async () => {
298 const mutationOpts1 = mutationOptions({
299 mutationKey: ['mutation'],
300 mutationFn: () => sleep(10).then(() => 'data1'),
301 })
302 const mutationOpts2 = mutationOptions({
303 mutationFn: () => sleep(10).then(() => 'data2'),
304 })
305​
306 const [mutation1, mutation2, mutationState] = TestBed.runInInjectionContext(
307 () => [
308 injectMutation(() => mutationOpts1),
309 injectMutation(() => mutationOpts2),
310 injectMutationState(() => ({
311 filters: {
312 mutationKey: mutationOpts1.mutationKey,
313 status: 'success',
314 },
315 })),
316 ],
317 )
318​
319 expect(mutationState().length).toBe(0)
320​
321 mutation1.mutate()
322 mutation2.mutate()
323 await vi.advanceTimersByTimeAsync(11)
324 expect(mutationState().length).toBe(1)
325 expect(mutationState()[0]?.data).toBe('data1')
326 })
327})