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

mutation-options.test.ts

334 lines10.5 KB

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