Skip to content

Commit a36702b

Browse files
committed
test(core): add test for mutation function context
1 parent 0c58762 commit a36702b

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

packages/query-core/src/__tests__/mutations.test.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
22
import { waitFor } from '@testing-library/react'
33
import { MutationObserver } from '../mutationObserver'
44
import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
5-
import type { QueryClient } from '..'
5+
import type { MutationFunctionContext, QueryClient } from '..'
66
import type { MutationState } from '../mutation'
77

88
describe('mutations', () => {
@@ -49,7 +49,30 @@ describe('mutations', () => {
4949
)
5050

5151
expect(fn).toHaveBeenCalledTimes(1)
52-
expect(fn).toHaveBeenCalledWith('vars')
52+
expect(fn).toHaveBeenCalledWith('vars', expect.anything())
53+
})
54+
55+
test('should provide context to mutationFn', async () => {
56+
const mutationKey = queryKey()
57+
const vars = 'vars' as const
58+
const meta = { a: 1 }
59+
const mutationFn = vi
60+
.fn<[typeof vars, MutationFunctionContext], Promise<'data'>>()
61+
.mockResolvedValue('data')
62+
63+
const mutation = new MutationObserver(queryClient, {
64+
mutationKey,
65+
mutationFn,
66+
meta,
67+
})
68+
69+
await mutation.mutate(vars)
70+
71+
expect(mutationFn).toHaveBeenCalledTimes(1)
72+
const context = mutationFn.mock.calls[0]![1]
73+
expect(context).toBeDefined()
74+
expect(context.mutationKey).toEqual(mutationKey)
75+
expect(context.meta).toEqual(meta)
5376
})
5477

5578
test('mutation should set correct success states', async () => {

0 commit comments

Comments
 (0)