Skip to content

Commit 3471d0f

Browse files
minseong0324TkDodo
andauthoredApr 12, 2025··
test(query-core): add test case for mutationObserver (#8975)
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent ec12377 commit 3471d0f

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
 

‎packages/query-core/src/__tests__/mutationObserver.test.tsx

+60
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,64 @@ describe('mutationObserver', () => {
307307

308308
unsubscribe()
309309
})
310+
311+
test('mutation callbacks should be called in correct order with correct arguments for success case', async () => {
312+
const onSuccess = vi.fn()
313+
const onSettled = vi.fn()
314+
315+
const mutationObserver = new MutationObserver(queryClient, {
316+
mutationFn: (text: string) => Promise.resolve(text.toUpperCase()),
317+
})
318+
319+
const subscriptionHandler = vi.fn()
320+
const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
321+
322+
mutationObserver.mutate('success', {
323+
onSuccess,
324+
onSettled,
325+
})
326+
327+
await vi.advanceTimersByTimeAsync(0)
328+
329+
expect(onSuccess).toHaveBeenCalledTimes(1)
330+
expect(onSuccess).toHaveBeenCalledWith('SUCCESS', 'success', undefined)
331+
expect(onSettled).toHaveBeenCalledTimes(1)
332+
expect(onSettled).toHaveBeenCalledWith(
333+
'SUCCESS',
334+
null,
335+
'success',
336+
undefined,
337+
)
338+
339+
unsubscribe()
340+
})
341+
342+
test('mutation callbacks should be called in correct order with correct arguments for error case', async () => {
343+
const onError = vi.fn()
344+
const onSettled = vi.fn()
345+
346+
const error = new Error('error')
347+
const mutationObserver = new MutationObserver(queryClient, {
348+
mutationFn: (_: string) => Promise.reject(error),
349+
})
350+
351+
const subscriptionHandler = vi.fn()
352+
const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
353+
354+
mutationObserver
355+
.mutate('error', {
356+
onError,
357+
onSettled,
358+
})
359+
.catch(() => {})
360+
361+
await vi.advanceTimersByTimeAsync(0)
362+
363+
expect(onError).toHaveBeenCalledTimes(1)
364+
expect(onError).toHaveBeenCalledWith(error, 'error', undefined)
365+
expect(onSettled).toHaveBeenCalledTimes(1)
366+
expect(onSettled).toHaveBeenCalledWith(undefined, error, 'error', undefined)
367+
368+
unsubscribe()
369+
})
310370
})

0 commit comments

Comments
 (0)
Please sign in to comment.