diff --git a/packages/preact-query/src/__tests__/useMutation.test.tsx b/packages/preact-query/src/__tests__/useMutation.test.tsx index 9ff5a41c318..a981e73c665 100644 --- a/packages/preact-query/src/__tests__/useMutation.test.tsx +++ b/packages/preact-query/src/__tests__/useMutation.test.tsx @@ -365,6 +365,97 @@ describe('useMutation', () => { ]) }) + it('should be able to override the error callbacks when using mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: async (_text: string) => + sleep(10).then(() => Promise.reject(new Error('oops'))), + onError: () => { + callbacks.push('useMutation.onError') + }, + onSettled: () => { + callbacks.push('useMutation.onSettled') + }, + }) + + return ( + + ) + } + + const rendered = renderWithClient(queryClient, ) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual([ + 'useMutation.onError', + 'useMutation.onSettled', + 'mutate.onError', + 'mutate.onSettled', + ]) + }) + + it('should be able to override the settled callbacks when using mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: (text: string) => sleep(10).then(() => text), + onSuccess: () => { + callbacks.push('useMutation.onSuccess') + }, + onSettled: () => { + callbacks.push('useMutation.onSettled') + }, + }) + + return ( + + ) + } + + const rendered = renderWithClient(queryClient, ) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual([ + 'useMutation.onSuccess', + 'useMutation.onSettled', + 'mutate.onSuccess', + 'mutate.onSettled', + ]) + }) + it('should be able to use mutation defaults', async () => { const key = queryKey() diff --git a/packages/react-query/src/__tests__/useMutation.test.tsx b/packages/react-query/src/__tests__/useMutation.test.tsx index d181df692c8..12f001f85bc 100644 --- a/packages/react-query/src/__tests__/useMutation.test.tsx +++ b/packages/react-query/src/__tests__/useMutation.test.tsx @@ -364,6 +364,97 @@ describe('useMutation', () => { ]) }) + it('should be able to override the error callbacks when using mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: async (_text: string) => + sleep(10).then(() => Promise.reject(new Error('oops'))), + onError: () => { + callbacks.push('useMutation.onError') + }, + onSettled: () => { + callbacks.push('useMutation.onSettled') + }, + }) + + return ( + + ) + } + + const rendered = renderWithClient(queryClient, ) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual([ + 'useMutation.onError', + 'useMutation.onSettled', + 'mutate.onError', + 'mutate.onSettled', + ]) + }) + + it('should be able to override the settled callbacks when using mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: (text: string) => sleep(10).then(() => text), + onSuccess: () => { + callbacks.push('useMutation.onSuccess') + }, + onSettled: () => { + callbacks.push('useMutation.onSettled') + }, + }) + + return ( + + ) + } + + const rendered = renderWithClient(queryClient, ) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual([ + 'useMutation.onSuccess', + 'useMutation.onSettled', + 'mutate.onSuccess', + 'mutate.onSettled', + ]) + }) + it('should be able to use mutation defaults', async () => { const key = queryKey()