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

inject-mutation.test.ts

308 lines7.8 KB

1import { signal } from '@angular/core'
2import { QueryClient } from '@tanstack/query-core'
3import { TestBed } from '@angular/core/testing'
4import { expect, test, vi } from 'vitest'
5import { injectMutation } from '../inject-mutation'
6import { provideAngularQuery } from '../providers'
7import { errorMutator, expectSignals, successMutator } from './test-utils'
8​
9const MUTATION_DURATION = 1000
10​
11const resolveMutations = () => vi.advanceTimersByTimeAsync(MUTATION_DURATION)
12​
13describe('injectMutation', () => {
14 let queryClient: QueryClient
15​
16 beforeEach(() => {
17 queryClient = new QueryClient()
18 vi.useFakeTimers()
19 TestBed.configureTestingModule({
20 providers: [provideAngularQuery(queryClient)],
21 })
22 })
23​
24 afterEach(() => {
25 vi.useRealTimers()
26 })
27​
28 describe('callback helpers', () => {
29 test('can access client from options callback', async () => {
30 const mutation = TestBed.runInInjectionContext(() => {
31 return injectMutation((client) => ({
32 mutationFn: () => {
33 expect(client).toBe(queryClient)
34 return Promise.resolve()
35 },
36 }))
37 })
38​
39 mutation.mutate()
40 vi.advanceTimersByTime(1)
41 expect(mutation.status()).toBe('pending')
42 })
43 })
44​
45 test('should be in idle state initially', () => {
46 const mutation = TestBed.runInInjectionContext(() => {
47 return injectMutation(() => ({
48 mutationFn: (params) => successMutator(params),
49 }))
50 })
51​
52 expectSignals(mutation, {
53 isIdle: true,
54 isPending: false,
55 isError: false,
56 isSuccess: false,
57 })
58 })
59​
60 test('should change state after invoking mutate', async () => {
61 const result = 'Mock data'
62​
63 const mutation = TestBed.runInInjectionContext(() => {
64 return injectMutation(() => ({
65 mutationFn: (params: string) => successMutator(params),
66 }))
67 })
68​
69 mutation.mutate(result)
70 vi.advanceTimersByTime(1)
71​
72 expectSignals(mutation, {
73 isIdle: false,
74 isPending: true,
75 isError: false,
76 isSuccess: false,
77 data: undefined,
78 error: null,
79 })
80 })
81​
82 test('should return error when request fails', async () => {
83 const mutation = TestBed.runInInjectionContext(() => {
84 return injectMutation(() => ({
85 mutationFn: errorMutator,
86 }))
87 })
88 mutation.mutate({})
89​
90 await resolveMutations()
91​
92 expectSignals(mutation, {
93 isIdle: false,
94 isPending: false,
95 isError: true,
96 isSuccess: false,
97 data: undefined,
98 error: Error('Some error'),
99 })
100 })
101​
102 test('should return data when request succeeds', async () => {
103 const result = 'Mock data'
104 const mutation = TestBed.runInInjectionContext(() => {
105 return injectMutation(() => ({
106 mutationFn: (params: string) => successMutator(params),
107 }))
108 })
109​
110 mutation.mutate(result)
111​
112 await resolveMutations()
113​
114 expectSignals(mutation, {
115 isIdle: false,
116 isPending: false,
117 isError: false,
118 isSuccess: true,
119 data: result,
120 error: null,
121 })
122 })
123​
124 test('reactive options should update mutation', async () => {
125 const mutationCache = queryClient.getMutationCache()
126 const mutationKey = signal(['foo'])
127 const mutation = TestBed.runInInjectionContext(() => {
128 return injectMutation(() => ({
129 mutationKey: mutationKey(),
130 mutationFn: (params: string) => successMutator(params),
131 }))
132 })
133​
134 mutationKey.set(['bar'])
135​
136 TestBed.flushEffects()
137​
138 await resolveMutations()
139​
140 mutation.mutate('xyz')
141​
142 await resolveMutations()
143​
144 const mutations = mutationCache.find({ mutationKey: ['bar'] })
145​
146 expect(mutations?.options.mutationKey).toEqual(['bar'])
147 })
148​
149 test('should reset state after invoking mutation.reset', async () => {
150 const mutation = TestBed.runInInjectionContext(() => {
151 return injectMutation(() => ({
152 mutationFn: (params: string) => errorMutator(params),
153 }))
154 })
155​
156 mutation.mutate('')
157​
158 await resolveMutations()
159​
160 expect(mutation.isError()).toBe(true)
161​
162 mutation.reset()
163​
164 await resolveMutations()
165​
166 expectSignals(mutation, {
167 isIdle: true,
168 isPending: false,
169 isError: false,
170 isSuccess: false,
171 data: undefined,
172 error: null,
173 })
174 })
175​
176 describe('side effects', () => {
177 beforeEach(() => {
178 vi.clearAllMocks()
179 })
180​
181 test('should call onMutate when passed as an option', async () => {
182 const onMutate = vi.fn()
183 const mutation = TestBed.runInInjectionContext(() => {
184 return injectMutation(() => ({
185 mutationFn: (params: string) => successMutator(params),
186 onMutate,
187 }))
188 })
189​
190 mutation.mutate('')
191​
192 await resolveMutations()
193​
194 expect(onMutate).toHaveBeenCalledTimes(1)
195 })
196​
197 test('should call onError when passed as an option', async () => {
198 const onError = vi.fn()
199 const mutation = TestBed.runInInjectionContext(() => {
200 return injectMutation(() => ({
201 mutationFn: (params: string) => errorMutator(params),
202 onError,
203 }))
204 })
205​
206 mutation.mutate('')
207​
208 await resolveMutations()
209​
210 expect(onError).toHaveBeenCalledTimes(1)
211 })
212​
213 test('should call onSuccess when passed as an option', async () => {
214 const onSuccess = vi.fn()
215 const mutation = TestBed.runInInjectionContext(() => {
216 return injectMutation(() => ({
217 mutationFn: (params: string) => successMutator(params),
218 onSuccess,
219 }))
220 })
221​
222 mutation.mutate('')
223​
224 await resolveMutations()
225​
226 expect(onSuccess).toHaveBeenCalledTimes(1)
227 })
228​
229 test('should call onSettled when passed as an option', async () => {
230 const onSettled = vi.fn()
231 const mutation = TestBed.runInInjectionContext(() => {
232 return injectMutation(() => ({
233 mutationFn: (params: string) => successMutator(params),
234 onSettled,
235 }))
236 })
237​
238 mutation.mutate('')
239​
240 await resolveMutations()
241​
242 expect(onSettled).toHaveBeenCalledTimes(1)
243 })
244​
245 test('should call onError when passed as an argument of mutate function', async () => {
246 const onError = vi.fn()
247 const mutation = TestBed.runInInjectionContext(() => {
248 return injectMutation(() => ({
249 mutationFn: (params: string) => errorMutator(params),
250 }))
251 })
252​
253 mutation.mutate('', { onError })
254​
255 await resolveMutations()
256​
257 expect(onError).toHaveBeenCalledTimes(1)
258 })
259​
260 test('should call onSuccess when passed as an argument of mutate function', async () => {
261 const onSuccess = vi.fn()
262 const mutation = TestBed.runInInjectionContext(() => {
263 return injectMutation(() => ({
264 mutationFn: (params: string) => successMutator(params),
265 }))
266 })
267​
268 mutation.mutate('', { onSuccess })
269​
270 await resolveMutations()
271​
272 expect(onSuccess).toHaveBeenCalledTimes(1)
273 })
274​
275 test('should call onSettled when passed as an argument of mutate function', async () => {
276 const onSettled = vi.fn()
277 const mutation = TestBed.runInInjectionContext(() => {
278 return injectMutation(() => ({
279 mutationFn: (params: string) => successMutator(params),
280 }))
281 })
282​
283 mutation.mutate('', { onSettled })
284​
285 await resolveMutations()
286​
287 expect(onSettled).toHaveBeenCalledTimes(1)
288 })
289​
290 test('should fire both onSettled functions', async () => {
291 const onSettled = vi.fn()
292 const onSettledOnFunction = vi.fn()
293 const mutation = TestBed.runInInjectionContext(() => {
294 return injectMutation(() => ({
295 mutationFn: (params: string) => successMutator(params),
296 onSettled,
297 }))
298 })
299​
300 mutation.mutate('', { onSettled: onSettledOnFunction })
301​
302 await resolveMutations()
303​
304 expect(onSettled).toHaveBeenCalledTimes(1)
305 expect(onSettledOnFunction).toHaveBeenCalledTimes(1)
306 })
307 })
308})