diff --git a/packages/preact-query/src/__tests__/useMutation.test.tsx b/packages/preact-query/src/__tests__/useMutation.test.tsx index a981e73c665..39e56cc3c5a 100644 --- a/packages/preact-query/src/__tests__/useMutation.test.tsx +++ b/packages/preact-query/src/__tests__/useMutation.test.tsx @@ -264,6 +264,111 @@ describe('useMutation', () => { ) }) + it('should be able to call `onSuccess` callback after successful mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: (text: string) => sleep(10).then(() => text), + onSuccess: () => { + callbacks.push('useMutation.onSuccess') + }, + }) + + return ( + + ) + } + + const rendered = renderWithClient(queryClient, ) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['useMutation.onSuccess', 'mutate.onSuccess']) + }) + + it('should be able to call `onError` callback after failed mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: (_text: string) => + sleep(10).then(() => { + throw new Error('oops') + }), + onError: () => { + callbacks.push('useMutation.onError') + }, + }) + + return ( + + ) + } + + const rendered = renderWithClient(queryClient, ) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['useMutation.onError', 'mutate.onError']) + }) + + it('should be able to call `onSettled` callback after mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: (text: string) => sleep(10).then(() => text), + 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.onSettled', 'mutate.onSettled']) + }) + it('should be able to override the useMutation success callbacks', async () => { const callbacks: Array = [] diff --git a/packages/react-query/src/__tests__/useMutation.test.tsx b/packages/react-query/src/__tests__/useMutation.test.tsx index 12f001f85bc..b8deb78eed9 100644 --- a/packages/react-query/src/__tests__/useMutation.test.tsx +++ b/packages/react-query/src/__tests__/useMutation.test.tsx @@ -263,6 +263,111 @@ describe('useMutation', () => { ) }) + it('should be able to call `onSuccess` callback after successful mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: (text: string) => sleep(10).then(() => text), + onSuccess: () => { + callbacks.push('useMutation.onSuccess') + }, + }) + + return ( + + ) + } + + const rendered = renderWithClient(queryClient, ) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['useMutation.onSuccess', 'mutate.onSuccess']) + }) + + it('should be able to call `onError` callback after failed mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: (_text: string) => + sleep(10).then(() => { + throw new Error('oops') + }), + onError: () => { + callbacks.push('useMutation.onError') + }, + }) + + return ( + + ) + } + + const rendered = renderWithClient(queryClient, ) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['useMutation.onError', 'mutate.onError']) + }) + + it('should be able to call `onSettled` callback after mutate', async () => { + const callbacks: Array = [] + + function Page() { + const { mutate } = useMutation({ + mutationFn: (text: string) => sleep(10).then(() => text), + 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.onSettled', 'mutate.onSettled']) + }) + it('should be able to override the useMutation success callbacks', async () => { const callbacks: Array = []