From 89264056f3ded086d203ac294de641bd4715aff1 Mon Sep 17 00:00:00 2001 From: Wonsuk Choi Date: Thu, 11 Jun 2026 09:43:39 +0900 Subject: [PATCH] test(solid-query/useMutation): add 'mutate' callback tests for when 'useMutation' has no callbacks --- .../src/__tests__/useMutation.test.tsx | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) diff --git a/packages/solid-query/src/__tests__/useMutation.test.tsx b/packages/solid-query/src/__tests__/useMutation.test.tsx index 4d22e0ae298..c80c8f020d1 100644 --- a/packages/solid-query/src/__tests__/useMutation.test.tsx +++ b/packages/solid-query/src/__tests__/useMutation.test.tsx @@ -108,6 +108,193 @@ describe('useMutation', () => { consoleMock.mockRestore() }) + it('should call mutate callbacks when useMutation has no callbacks', async () => { + const callbacks: Array = [] + + function Page() { + const mutation = useMutation(() => ({ + mutationFn: (text: string) => sleep(10).then(() => text), + })) + + return ( + + ) + } + + const rendered = render(() => ( + + + + )) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['mutate.onSuccess', 'mutate.onSettled']) + }) + + it('should call mutate error callbacks when useMutation has no callbacks', async () => { + const callbacks: Array = [] + + function Page() { + const mutation = useMutation(() => ({ + mutationFn: (_text: string) => + sleep(10).then(() => { + throw new Error('oops') + }), + })) + + return ( + + ) + } + + const rendered = render(() => ( + + + + )) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['mutate.onError', 'mutate.onSettled']) + }) + + it('should call only mutate onSuccess when useMutation has no callbacks', async () => { + const callbacks: Array = [] + + function Page() { + const mutation = useMutation(() => ({ + mutationFn: (text: string) => sleep(10).then(() => text), + })) + + return ( + + ) + } + + const rendered = render(() => ( + + + + )) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['mutate.onSuccess']) + }) + + it('should call only mutate onError when useMutation has no callbacks', async () => { + const callbacks: Array = [] + + function Page() { + const mutation = useMutation(() => ({ + mutationFn: (_text: string) => + sleep(10).then(() => { + throw new Error('oops') + }), + })) + + return ( + + ) + } + + const rendered = render(() => ( + + + + )) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['mutate.onError']) + }) + + it('should call only mutate onSettled when useMutation has no callbacks', async () => { + const callbacks: Array = [] + + function Page() { + const mutation = useMutation(() => ({ + mutationFn: (text: string) => sleep(10).then(() => text), + })) + + return ( + + ) + } + + const rendered = render(() => ( + + + + )) + + fireEvent.click(rendered.getByRole('button', { name: /mutate/i })) + await vi.advanceTimersByTimeAsync(10) + + expect(callbacks).toEqual(['mutate.onSettled']) + }) + it('should be able to call `onSuccess` and `onSettled` after each successful mutate', async () => { const [count, setCount] = createSignal(0) const onSuccessMock = vi.fn()