Skip to content

Commit 224c073

Browse files
authored
test({react,preact}-query/useMutation): add 'mutate' callback override tests (#10486)
1 parent 9bd8b25 commit 224c073

2 files changed

Lines changed: 182 additions & 0 deletions

File tree

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,97 @@ describe('useMutation', () => {
365365
])
366366
})
367367

368+
it('should be able to override the error callbacks when using mutate', async () => {
369+
const callbacks: Array<string> = []
370+
371+
function Page() {
372+
const { mutate } = useMutation({
373+
mutationFn: async (_text: string) =>
374+
sleep(10).then(() => Promise.reject(new Error('oops'))),
375+
onError: () => {
376+
callbacks.push('useMutation.onError')
377+
},
378+
onSettled: () => {
379+
callbacks.push('useMutation.onSettled')
380+
},
381+
})
382+
383+
return (
384+
<button
385+
onClick={() =>
386+
mutate('todo', {
387+
onError: () => {
388+
callbacks.push('mutate.onError')
389+
},
390+
onSettled: () => {
391+
callbacks.push('mutate.onSettled')
392+
},
393+
})
394+
}
395+
>
396+
mutate
397+
</button>
398+
)
399+
}
400+
401+
const rendered = renderWithClient(queryClient, <Page />)
402+
403+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
404+
await vi.advanceTimersByTimeAsync(10)
405+
406+
expect(callbacks).toEqual([
407+
'useMutation.onError',
408+
'useMutation.onSettled',
409+
'mutate.onError',
410+
'mutate.onSettled',
411+
])
412+
})
413+
414+
it('should be able to override the settled callbacks when using mutate', async () => {
415+
const callbacks: Array<string> = []
416+
417+
function Page() {
418+
const { mutate } = useMutation({
419+
mutationFn: (text: string) => sleep(10).then(() => text),
420+
onSuccess: () => {
421+
callbacks.push('useMutation.onSuccess')
422+
},
423+
onSettled: () => {
424+
callbacks.push('useMutation.onSettled')
425+
},
426+
})
427+
428+
return (
429+
<button
430+
onClick={() =>
431+
mutate('todo', {
432+
onSuccess: () => {
433+
callbacks.push('mutate.onSuccess')
434+
},
435+
onSettled: () => {
436+
callbacks.push('mutate.onSettled')
437+
},
438+
})
439+
}
440+
>
441+
mutate
442+
</button>
443+
)
444+
}
445+
446+
const rendered = renderWithClient(queryClient, <Page />)
447+
448+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
449+
await vi.advanceTimersByTimeAsync(10)
450+
451+
expect(callbacks).toEqual([
452+
'useMutation.onSuccess',
453+
'useMutation.onSettled',
454+
'mutate.onSuccess',
455+
'mutate.onSettled',
456+
])
457+
})
458+
368459
it('should be able to use mutation defaults', async () => {
369460
const key = queryKey()
370461

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,97 @@ describe('useMutation', () => {
364364
])
365365
})
366366

367+
it('should be able to override the error callbacks when using mutate', async () => {
368+
const callbacks: Array<string> = []
369+
370+
function Page() {
371+
const { mutate } = useMutation({
372+
mutationFn: async (_text: string) =>
373+
sleep(10).then(() => Promise.reject(new Error('oops'))),
374+
onError: () => {
375+
callbacks.push('useMutation.onError')
376+
},
377+
onSettled: () => {
378+
callbacks.push('useMutation.onSettled')
379+
},
380+
})
381+
382+
return (
383+
<button
384+
onClick={() =>
385+
mutate('todo', {
386+
onError: () => {
387+
callbacks.push('mutate.onError')
388+
},
389+
onSettled: () => {
390+
callbacks.push('mutate.onSettled')
391+
},
392+
})
393+
}
394+
>
395+
mutate
396+
</button>
397+
)
398+
}
399+
400+
const rendered = renderWithClient(queryClient, <Page />)
401+
402+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
403+
await vi.advanceTimersByTimeAsync(10)
404+
405+
expect(callbacks).toEqual([
406+
'useMutation.onError',
407+
'useMutation.onSettled',
408+
'mutate.onError',
409+
'mutate.onSettled',
410+
])
411+
})
412+
413+
it('should be able to override the settled callbacks when using mutate', async () => {
414+
const callbacks: Array<string> = []
415+
416+
function Page() {
417+
const { mutate } = useMutation({
418+
mutationFn: (text: string) => sleep(10).then(() => text),
419+
onSuccess: () => {
420+
callbacks.push('useMutation.onSuccess')
421+
},
422+
onSettled: () => {
423+
callbacks.push('useMutation.onSettled')
424+
},
425+
})
426+
427+
return (
428+
<button
429+
onClick={() =>
430+
mutate('todo', {
431+
onSuccess: () => {
432+
callbacks.push('mutate.onSuccess')
433+
},
434+
onSettled: () => {
435+
callbacks.push('mutate.onSettled')
436+
},
437+
})
438+
}
439+
>
440+
mutate
441+
</button>
442+
)
443+
}
444+
445+
const rendered = renderWithClient(queryClient, <Page />)
446+
447+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
448+
await vi.advanceTimersByTimeAsync(10)
449+
450+
expect(callbacks).toEqual([
451+
'useMutation.onSuccess',
452+
'useMutation.onSettled',
453+
'mutate.onSuccess',
454+
'mutate.onSettled',
455+
])
456+
})
457+
367458
it('should be able to use mutation defaults', async () => {
368459
const key = queryKey()
369460

0 commit comments

Comments
 (0)