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

mutation-options.test.ts

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