|
1 | 1 | import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' |
| 2 | +import { flushSync } from 'svelte' |
2 | 3 | import { fireEvent, render } from '@testing-library/svelte' |
| 4 | +import { QueryClient } from '@tanstack/query-core' |
3 | 5 | import { sleep } from '@tanstack/query-test-utils' |
| 6 | +import { createMutation } from '../../src/index.js' |
| 7 | +import { withEffectRoot } from '../utils.svelte.js' |
4 | 8 | import ResetExample from './ResetExample.svelte' |
5 | 9 | import OnSuccessExample from './OnSuccessExample.svelte' |
6 | 10 | import FailureExample from './FailureExample.svelte' |
@@ -95,4 +99,33 @@ describe('createMutation', () => { |
95 | 99 | expect(rendered.getByText('Failure Count: 0')).toBeInTheDocument() |
96 | 100 | expect(rendered.getByText('Failure Reason: undefined')).toBeInTheDocument() |
97 | 101 | }) |
| 102 | + |
| 103 | + test( |
| 104 | + 'should recreate observer when queryClient changes', |
| 105 | + withEffectRoot(async () => { |
| 106 | + const queryClient1 = new QueryClient() |
| 107 | + const queryClient2 = new QueryClient() |
| 108 | + |
| 109 | + let queryClient = $state(queryClient1) |
| 110 | + |
| 111 | + const mutation = createMutation( |
| 112 | + () => ({ |
| 113 | + mutationFn: (params: string) => sleep(10).then(() => params), |
| 114 | + }), |
| 115 | + () => queryClient, |
| 116 | + ) |
| 117 | + |
| 118 | + mutation.mutate('first') |
| 119 | + await vi.advanceTimersByTimeAsync(11) |
| 120 | + |
| 121 | + expect(mutation.status).toBe('success') |
| 122 | + expect(mutation.data).toBe('first') |
| 123 | + |
| 124 | + queryClient = queryClient2 |
| 125 | + flushSync() |
| 126 | + |
| 127 | + expect(mutation.status).toBe('idle') |
| 128 | + expect(mutation.data).toBeUndefined() |
| 129 | + }), |
| 130 | + ) |
98 | 131 | }) |
0 commit comments