Skip to content

Commit c9109b6

Browse files
authored
test(vue-query/mutationOptions): add reactive 'mutationKey' test for getter overload (#10474)
1 parent 4c489e4 commit c9109b6

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

packages/vue-query/src/__tests__/mutationOptions.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { isReactive, ref } from 'vue-demi'
3-
import { sleep } from '@tanstack/query-test-utils'
3+
import { queryKey, sleep } from '@tanstack/query-test-utils'
44
import { useMutation } from '../useMutation'
55
import { useIsMutating, useMutationState } from '../useMutationState'
66
import { useQueryClient } from '../useQueryClient'
@@ -716,4 +716,38 @@ describe('mutationOptions', () => {
716716
expect(data.value).toEqual({ nested: { count: 0 } })
717717
expect(isReactive(data.value?.nested)).toBe(false)
718718
})
719+
720+
it('should reactively update mutationKey when ref changes in getter', async () => {
721+
const key = queryKey()
722+
const keyRef = ref('key01')
723+
const fnMock = vi.fn((params: string) => sleep(10).then(() => params))
724+
const mutationOpts = mutationOptions(() => ({
725+
mutationKey: [...key, keyRef.value],
726+
mutationFn: fnMock,
727+
}))
728+
729+
const mutation = useMutation(mutationOpts)
730+
731+
mutation.mutate('data')
732+
await vi.advanceTimersByTimeAsync(10)
733+
734+
expect(fnMock).toHaveBeenCalledTimes(1)
735+
expect(fnMock).toHaveBeenNthCalledWith(
736+
1,
737+
'data',
738+
expect.objectContaining({ mutationKey: [...key, 'key01'] }),
739+
)
740+
741+
keyRef.value = 'key02'
742+
await vi.advanceTimersByTimeAsync(0)
743+
mutation.mutate('data')
744+
await vi.advanceTimersByTimeAsync(10)
745+
746+
expect(fnMock).toHaveBeenCalledTimes(2)
747+
expect(fnMock).toHaveBeenNthCalledWith(
748+
2,
749+
'data',
750+
expect.objectContaining({ mutationKey: [...key, 'key02'] }),
751+
)
752+
})
719753
})

0 commit comments

Comments
 (0)