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