Skip to content

Commit dbef038

Browse files
authored
tests: Add callback tests for useMutation (TanStack#641)
Adds callback tests for when the `mutate` function is called multiple times in a row.
1 parent 5a5ad74 commit dbef038

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

src/react/tests/useMutation.test.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,115 @@ describe('useMutation', () => {
8585

8686
console.error.mockRestore()
8787
})
88+
89+
it('should be able to call `onSuccess` and `onSettled` after each successful mutate', async () => {
90+
let count = 0
91+
const onSuccessMock = jest.fn()
92+
const onSettledMock = jest.fn()
93+
94+
function Page() {
95+
const [mutate] = useMutation(
96+
async ({ count }) => Promise.resolve(count),
97+
{
98+
onSuccess: data => onSuccessMock(data),
99+
onSettled: data => onSettledMock(data),
100+
}
101+
)
102+
103+
return (
104+
<div>
105+
<h1 data-testid="title">{count}</h1>
106+
<button onClick={() => mutate({ count: ++count })}>mutate</button>
107+
</div>
108+
)
109+
}
110+
111+
const { getByTestId, getByText } = render(<Page />)
112+
113+
expect(getByTestId('title').textContent).toBe('0')
114+
115+
fireEvent.click(getByText('mutate'))
116+
fireEvent.click(getByText('mutate'))
117+
fireEvent.click(getByText('mutate'))
118+
119+
await waitFor(() => getByTestId('title'))
120+
121+
expect(onSuccessMock).toHaveBeenCalledTimes(3)
122+
expect(onSuccessMock).toHaveBeenCalledWith(1)
123+
expect(onSuccessMock).toHaveBeenCalledWith(2)
124+
expect(onSuccessMock).toHaveBeenCalledWith(3)
125+
126+
expect(onSettledMock).toHaveBeenCalledTimes(3)
127+
expect(onSettledMock).toHaveBeenCalledWith(1)
128+
expect(onSettledMock).toHaveBeenCalledWith(2)
129+
expect(onSettledMock).toHaveBeenCalledWith(3)
130+
131+
expect(getByTestId('title').textContent).toBe('3')
132+
})
133+
134+
it('should be able to call `onError` and `onSettled` after each failed mutate', async () => {
135+
jest.spyOn(console, 'error')
136+
console.error.mockImplementation(() => {})
137+
const onErrorMock = jest.fn()
138+
const onSettledMock = jest.fn()
139+
let count = 0
140+
141+
function Page() {
142+
const [mutate] = useMutation(
143+
({ count }) => {
144+
const error = new Error(`Expected mock error. All is well! ${count}`)
145+
error.stack = ''
146+
return Promise.reject(error)
147+
},
148+
{
149+
onError: error => onErrorMock(error.message),
150+
onSettled: (_data, error) => onSettledMock(error.message),
151+
},
152+
{
153+
throwOnError: false,
154+
}
155+
)
156+
157+
return (
158+
<div>
159+
<h1 data-testid="title">{count}</h1>
160+
<button onClick={() => mutate({ count: ++count })}>mutate</button>
161+
</div>
162+
)
163+
}
164+
165+
const { getByTestId, getByText } = render(<Page />)
166+
167+
expect(getByTestId('title').textContent).toBe('0')
168+
169+
fireEvent.click(getByText('mutate'))
170+
fireEvent.click(getByText('mutate'))
171+
fireEvent.click(getByText('mutate'))
172+
173+
await waitFor(() => getByTestId('title'))
174+
175+
expect(onErrorMock).toHaveBeenCalledTimes(3)
176+
expect(onErrorMock).toHaveBeenCalledWith(
177+
'Expected mock error. All is well! 1'
178+
)
179+
expect(onErrorMock).toHaveBeenCalledWith(
180+
'Expected mock error. All is well! 2'
181+
)
182+
expect(onErrorMock).toHaveBeenCalledWith(
183+
'Expected mock error. All is well! 3'
184+
)
185+
186+
expect(onSettledMock).toHaveBeenCalledTimes(3)
187+
expect(onSettledMock).toHaveBeenCalledWith(
188+
'Expected mock error. All is well! 1'
189+
)
190+
expect(onSettledMock).toHaveBeenCalledWith(
191+
'Expected mock error. All is well! 2'
192+
)
193+
expect(onSettledMock).toHaveBeenCalledWith(
194+
'Expected mock error. All is well! 3'
195+
)
196+
197+
expect(getByTestId('title').textContent).toBe('3')
198+
})
88199
})

0 commit comments

Comments
 (0)