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