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

inject-mutation.test.ts

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