Skip to content

Commit eccac7c

Browse files
test({react,preact}-query/useMutation): add single callback tests for 'onSuccess', 'onError', and 'onSettled' (#10487)
* test({react,preact}-query/useMutation): add single callback tests for 'onSuccess', 'onError', and 'onSettled' * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 224c073 commit eccac7c

2 files changed

Lines changed: 210 additions & 0 deletions

File tree

packages/preact-query/src/__tests__/useMutation.test.tsx

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,111 @@ describe('useMutation', () => {
264264
)
265265
})
266266

267+
it('should be able to call `onSuccess` callback after successful mutate', async () => {
268+
const callbacks: Array<string> = []
269+
270+
function Page() {
271+
const { mutate } = useMutation({
272+
mutationFn: (text: string) => sleep(10).then(() => text),
273+
onSuccess: () => {
274+
callbacks.push('useMutation.onSuccess')
275+
},
276+
})
277+
278+
return (
279+
<button
280+
onClick={() =>
281+
mutate('todo', {
282+
onSuccess: () => {
283+
callbacks.push('mutate.onSuccess')
284+
},
285+
})
286+
}
287+
>
288+
mutate
289+
</button>
290+
)
291+
}
292+
293+
const rendered = renderWithClient(queryClient, <Page />)
294+
295+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
296+
await vi.advanceTimersByTimeAsync(10)
297+
298+
expect(callbacks).toEqual(['useMutation.onSuccess', 'mutate.onSuccess'])
299+
})
300+
301+
it('should be able to call `onError` callback after failed mutate', async () => {
302+
const callbacks: Array<string> = []
303+
304+
function Page() {
305+
const { mutate } = useMutation({
306+
mutationFn: (_text: string) =>
307+
sleep(10).then(() => {
308+
throw new Error('oops')
309+
}),
310+
onError: () => {
311+
callbacks.push('useMutation.onError')
312+
},
313+
})
314+
315+
return (
316+
<button
317+
onClick={() =>
318+
mutate('todo', {
319+
onError: () => {
320+
callbacks.push('mutate.onError')
321+
},
322+
})
323+
}
324+
>
325+
mutate
326+
</button>
327+
)
328+
}
329+
330+
const rendered = renderWithClient(queryClient, <Page />)
331+
332+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
333+
await vi.advanceTimersByTimeAsync(10)
334+
335+
expect(callbacks).toEqual(['useMutation.onError', 'mutate.onError'])
336+
})
337+
338+
it('should be able to call `onSettled` callback after mutate', async () => {
339+
const callbacks: Array<string> = []
340+
341+
function Page() {
342+
const { mutate } = useMutation({
343+
mutationFn: (text: string) => sleep(10).then(() => text),
344+
onSettled: () => {
345+
callbacks.push('useMutation.onSettled')
346+
},
347+
})
348+
349+
return (
350+
<button
351+
onClick={() =>
352+
mutate('todo', {
353+
onSettled: () => {
354+
callbacks.push('mutate.onSettled')
355+
},
356+
})
357+
}
358+
>
359+
mutate
360+
</button>
361+
)
362+
}
363+
364+
const rendered = renderWithClient(queryClient, <Page />)
365+
366+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
367+
await vi.advanceTimersByTimeAsync(10)
368+
369+
expect(callbacks).toEqual(['useMutation.onSettled', 'mutate.onSettled'])
370+
})
371+
267372
it('should be able to override the useMutation success callbacks', async () => {
268373
const callbacks: Array<string> = []
269374

packages/react-query/src/__tests__/useMutation.test.tsx

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,111 @@ describe('useMutation', () => {
263263
)
264264
})
265265

266+
it('should be able to call `onSuccess` callback after successful mutate', async () => {
267+
const callbacks: Array<string> = []
268+
269+
function Page() {
270+
const { mutate } = useMutation({
271+
mutationFn: (text: string) => sleep(10).then(() => text),
272+
onSuccess: () => {
273+
callbacks.push('useMutation.onSuccess')
274+
},
275+
})
276+
277+
return (
278+
<button
279+
onClick={() =>
280+
mutate('todo', {
281+
onSuccess: () => {
282+
callbacks.push('mutate.onSuccess')
283+
},
284+
})
285+
}
286+
>
287+
mutate
288+
</button>
289+
)
290+
}
291+
292+
const rendered = renderWithClient(queryClient, <Page />)
293+
294+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
295+
await vi.advanceTimersByTimeAsync(10)
296+
297+
expect(callbacks).toEqual(['useMutation.onSuccess', 'mutate.onSuccess'])
298+
})
299+
300+
it('should be able to call `onError` callback after failed mutate', async () => {
301+
const callbacks: Array<string> = []
302+
303+
function Page() {
304+
const { mutate } = useMutation({
305+
mutationFn: (_text: string) =>
306+
sleep(10).then(() => {
307+
throw new Error('oops')
308+
}),
309+
onError: () => {
310+
callbacks.push('useMutation.onError')
311+
},
312+
})
313+
314+
return (
315+
<button
316+
onClick={() =>
317+
mutate('todo', {
318+
onError: () => {
319+
callbacks.push('mutate.onError')
320+
},
321+
})
322+
}
323+
>
324+
mutate
325+
</button>
326+
)
327+
}
328+
329+
const rendered = renderWithClient(queryClient, <Page />)
330+
331+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
332+
await vi.advanceTimersByTimeAsync(10)
333+
334+
expect(callbacks).toEqual(['useMutation.onError', 'mutate.onError'])
335+
})
336+
337+
it('should be able to call `onSettled` callback after mutate', async () => {
338+
const callbacks: Array<string> = []
339+
340+
function Page() {
341+
const { mutate } = useMutation({
342+
mutationFn: (text: string) => sleep(10).then(() => text),
343+
onSettled: () => {
344+
callbacks.push('useMutation.onSettled')
345+
},
346+
})
347+
348+
return (
349+
<button
350+
onClick={() =>
351+
mutate('todo', {
352+
onSettled: () => {
353+
callbacks.push('mutate.onSettled')
354+
},
355+
})
356+
}
357+
>
358+
mutate
359+
</button>
360+
)
361+
}
362+
363+
const rendered = renderWithClient(queryClient, <Page />)
364+
365+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
366+
await vi.advanceTimersByTimeAsync(10)
367+
368+
expect(callbacks).toEqual(['useMutation.onSettled', 'mutate.onSettled'])
369+
})
370+
266371
it('should be able to override the useMutation success callbacks', async () => {
267372
const callbacks: Array<string> = []
268373

0 commit comments

Comments
 (0)