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

mutations.test.tsx

363 lines8.2 KB

1import { QueryClient } from '../..'
2import {
3 createQueryClient,
4 executeMutation,
5 queryKey,
6 sleep,
7} from '../../reactjs/tests/utils'
8import { MutationState } from '../mutation'
9import { MutationObserver } from '../mutationObserver'
10​
11describe('mutations', () => {
12 let queryClient: QueryClient
13​
14 beforeEach(() => {
15 queryClient = createQueryClient()
16 queryClient.mount()
17 })
18​
19 afterEach(() => {
20 queryClient.clear()
21 })
22​
23 test('mutate should trigger a mutation', async () => {
24 const result = await executeMutation(queryClient, {
25 mutationFn: async (text: string) => text,
26 variables: 'todo',
27 })
28​
29 expect(result).toBe(result)
30 })
31​
32 test('mutate should accept null values', async () => {
33 let variables
34​
35 const mutation = new MutationObserver(queryClient, {
36 mutationFn: async (vars: unknown) => {
37 variables = vars
38 return vars
39 },
40 })
41​
42 mutation.mutate(null)
43​
44 await sleep(10)
45​
46 expect(variables).toBe(null)
47 })
48​
49 test('setMutationDefaults should be able to set defaults', async () => {
50 const key = queryKey()
51​
52 queryClient.setMutationDefaults(key, {
53 mutationFn: async (text: string) => text,
54 })
55​
56 const result = await executeMutation(queryClient, {
57 mutationKey: key,
58 variables: 'todo',
59 })
60​
61 expect(result).toBe(result)
62 })
63​
64 test('mutation should set correct success states', async () => {
65 const mutation = new MutationObserver(queryClient, {
66 mutationFn: async (text: string) => {
67 await sleep(10)
68 return text
69 },
70 onMutate: text => text,
71 variables: 'todo',
72 })
73​
74 expect(mutation.getCurrentResult()).toEqual({
75 context: undefined,
76 data: undefined,
77 error: null,
78 failureCount: 0,
79 isError: false,
80 isIdle: true,
81 isLoading: false,
82 isPaused: false,
83 isSuccess: false,
84 mutate: expect.any(Function),
85 reset: expect.any(Function),
86 status: 'idle',
87 variables: undefined,
88 })
89​
90 const states: MutationState<string, unknown, string, string>[] = []
91​
92 mutation.subscribe(state => {
93 states.push(state)
94 })
95​
96 mutation.mutate()
97​
98 await sleep(0)
99​
100 expect(states[0]).toEqual({
101 context: undefined,
102 data: undefined,
103 error: null,
104 failureCount: 0,
105 isError: false,
106 isIdle: false,
107 isLoading: true,
108 isPaused: false,
109 isSuccess: false,
110 mutate: expect.any(Function),
111 reset: expect.any(Function),
112 status: 'loading',
113 variables: 'todo',
114 })
115​
116 await sleep(5)
117​
118 expect(states[1]).toEqual({
119 context: 'todo',
120 data: undefined,
121 error: null,
122 failureCount: 0,
123 isError: false,
124 isIdle: false,
125 isLoading: true,
126 isPaused: false,
127 isSuccess: false,
128 mutate: expect.any(Function),
129 reset: expect.any(Function),
130 status: 'loading',
131 variables: 'todo',
132 })
133​
134 await sleep(20)
135​
136 expect(states[2]).toEqual({
137 context: 'todo',
138 data: 'todo',
139 error: null,
140 failureCount: 0,
141 isError: false,
142 isIdle: false,
143 isLoading: false,
144 isPaused: false,
145 isSuccess: true,
146 mutate: expect.any(Function),
147 reset: expect.any(Function),
148 status: 'success',
149 variables: 'todo',
150 })
151 })
152​
153 test('mutation should set correct error states', async () => {
154 const mutation = new MutationObserver(queryClient, {
155 mutationFn: async () => {
156 await sleep(20)
157 return Promise.reject('err')
158 },
159 onMutate: text => text,
160 variables: 'todo',
161 retry: 1,
162 retryDelay: 1,
163 })
164​
165 const states: MutationState<string, unknown, string, string>[] = []
166​
167 mutation.subscribe(state => {
168 states.push(state)
169 })
170​
171 mutation.mutate().catch(() => undefined)
172​
173 await sleep(0)
174​
175 expect(states[0]).toEqual({
176 context: undefined,
177 data: undefined,
178 error: null,
179 failureCount: 0,
180 isError: false,
181 isIdle: false,
182 isLoading: true,
183 isPaused: false,
184 isSuccess: false,
185 mutate: expect.any(Function),
186 reset: expect.any(Function),
187 status: 'loading',
188 variables: 'todo',
189 })
190​
191 await sleep(10)
192​
193 expect(states[1]).toEqual({
194 context: 'todo',
195 data: undefined,
196 error: null,
197 failureCount: 0,
198 isError: false,
199 isIdle: false,
200 isLoading: true,
201 isPaused: false,
202 isSuccess: false,
203 mutate: expect.any(Function),
204 reset: expect.any(Function),
205 status: 'loading',
206 variables: 'todo',
207 })
208​
209 await sleep(20)
210​
211 expect(states[2]).toEqual({
212 context: 'todo',
213 data: undefined,
214 error: null,
215 failureCount: 1,
216 isError: false,
217 isIdle: false,
218 isLoading: true,
219 isPaused: false,
220 isSuccess: false,
221 mutate: expect.any(Function),
222 reset: expect.any(Function),
223 status: 'loading',
224 variables: 'todo',
225 })
226​
227 await sleep(30)
228​
229 expect(states[3]).toEqual({
230 context: 'todo',
231 data: undefined,
232 error: 'err',
233 failureCount: 2,
234 isError: true,
235 isIdle: false,
236 isLoading: false,
237 isPaused: false,
238 isSuccess: false,
239 mutate: expect.any(Function),
240 reset: expect.any(Function),
241 status: 'error',
242 variables: 'todo',
243 })
244 })
245​
246 test('should be able to restore a mutation', async () => {
247 const key = queryKey()
248​
249 const onMutate = jest.fn()
250 const onSuccess = jest.fn()
251 const onSettled = jest.fn()
252​
253 queryClient.setMutationDefaults(key, {
254 mutationFn: async (text: string) => text,
255 onMutate,
256 onSuccess,
257 onSettled,
258 })
259​
260 const mutation = queryClient
261 .getMutationCache()
262 .build<string, unknown, string, string>(
263 queryClient,
264 {
265 mutationKey: key,
266 },
267 {
268 context: 'todo',
269 data: undefined,
270 error: null,
271 failureCount: 1,
272 isPaused: true,
273 status: 'loading',
274 variables: 'todo',
275 }
276 )
277​
278 expect(mutation.state).toEqual({
279 context: 'todo',
280 data: undefined,
281 error: null,
282 failureCount: 1,
283 isPaused: true,
284 status: 'loading',
285 variables: 'todo',
286 })
287​
288 await queryClient.resumePausedMutations()
289​
290 expect(mutation.state).toEqual({
291 context: 'todo',
292 data: 'todo',
293 error: null,
294 failureCount: 1,
295 isPaused: false,
296 status: 'success',
297 variables: 'todo',
298 })
299​
300 expect(onMutate).not.toHaveBeenCalled()
301 expect(onSuccess).toHaveBeenCalled()
302 expect(onSettled).toHaveBeenCalled()
303 })
304​
305 test('setState should update the mutation state', async () => {
306 const mutation = new MutationObserver(queryClient, {
307 mutationFn: async () => {
308 return 'update'
309 },
310 onMutate: text => text,
311 })
312 await mutation.mutate()
313 expect(mutation.getCurrentResult().data).toEqual('update')
314​
315 // Force setState usage
316 // because no use case has been found using mutation.setState
317 const currentMutation = mutation['currentMutation']
318 currentMutation?.setState({
319 context: undefined,
320 variables: undefined,
321 data: 'new',
322 error: undefined,
323 failureCount: 0,
324 isPaused: false,
325 status: 'success',
326 })
327​
328 expect(mutation.getCurrentResult().data).toEqual('new')
329 })
330​
331 test('addObserver should not add an existing observer', async () => {
332 const mutation = new MutationObserver(queryClient, {
333 mutationFn: async () => {
334 return 'update'
335 },
336 onMutate: text => text,
337 })
338 await mutation.mutate()
339​
340 // Force addObserver usage to add an existing observer
341 // because no use case has been found
342 const currentMutation = mutation['currentMutation']!
343 expect(currentMutation['observers'].length).toEqual(1)
344 currentMutation.addObserver(mutation)
345​
346 expect(currentMutation['observers'].length).toEqual(1)
347 })
348​
349 test('mutate should throw an error if no mutationFn found', async () => {
350 const mutation = new MutationObserver(queryClient, {
351 mutationFn: undefined,
352 retry: false,
353 })
354​
355 let error: any
356 try {
357 await mutation.mutate()
358 } catch (err) {
359 error = err
360 }
361 expect(error).toEqual('No mutationFn found')
362 })
363})