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

mutation-options.test.ts

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