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