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