Open History to see the last commit
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})