|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import { isReactive, ref } from 'vue-demi' |
3 | | -import { sleep } from '@tanstack/query-test-utils' |
| 3 | +import { queryKey, sleep } from '@tanstack/query-test-utils' |
4 | 4 | import { useMutation } from '../useMutation' |
5 | 5 | import { useIsMutating, useMutationState } from '../useMutationState' |
6 | 6 | import { useQueryClient } from '../useQueryClient' |
@@ -716,4 +716,38 @@ describe('mutationOptions', () => { |
716 | 716 | expect(data.value).toEqual({ nested: { count: 0 } }) |
717 | 717 | expect(isReactive(data.value?.nested)).toBe(false) |
718 | 718 | }) |
| 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 | + }) |
719 | 753 | }) |
0 commit comments