Skip to content

Commit c79a191

Browse files
authored
test(svelte-query/createMutation): add test for recreating observer when 'queryClient' changes (#10244)
1 parent 50eecb2 commit c79a191

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

packages/svelte-query/tests/createMutation/createMutation.svelte.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2+
import { flushSync } from 'svelte'
23
import { fireEvent, render } from '@testing-library/svelte'
4+
import { QueryClient } from '@tanstack/query-core'
35
import { sleep } from '@tanstack/query-test-utils'
6+
import { createMutation } from '../../src/index.js'
7+
import { withEffectRoot } from '../utils.svelte.js'
48
import ResetExample from './ResetExample.svelte'
59
import OnSuccessExample from './OnSuccessExample.svelte'
610
import FailureExample from './FailureExample.svelte'
@@ -95,4 +99,33 @@ describe('createMutation', () => {
9599
expect(rendered.getByText('Failure Count: 0')).toBeInTheDocument()
96100
expect(rendered.getByText('Failure Reason: undefined')).toBeInTheDocument()
97101
})
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+
)
98131
})

0 commit comments

Comments
 (0)