Skip to content

Commit 5f5cfab

Browse files
authored
test(svelte-query/createQueries): add test for not fetching when 'isRestoring' is true (#10382)
* test(svelte-query/createQueries): add test for not fetching when 'isRestoring' is true * test(svelte-query/createQueries): remove '(isRestoring)' from describe block * test(svelte-query/createQueries): move 'queryClient' inside 'IsRestoringExample' component
1 parent 5ca721f commit 5f5cfab

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<script lang="ts">
2+
import { QueryClient } from '@tanstack/query-core'
3+
import { setIsRestoringContext } from '../../src/context.js'
4+
import { createQueries } from '../../src/index.js'
5+
6+
let {
7+
queryFn1,
8+
queryFn2,
9+
}: {
10+
queryFn1: () => Promise<string>
11+
queryFn2: () => Promise<string>
12+
} = $props()
13+
14+
const queryClient = new QueryClient()
15+
16+
setIsRestoringContext({ current: true })
17+
18+
const result = createQueries(
19+
() => ({
20+
queries: [
21+
{ queryKey: ['restoring-1'], queryFn: queryFn1 },
22+
{ queryKey: ['restoring-2'], queryFn: queryFn2 },
23+
],
24+
}),
25+
() => queryClient,
26+
)
27+
</script>
28+
29+
<div>
30+
<div data-testid="status1">{result[0].status}</div>
31+
<div data-testid="status2">{result[1].status}</div>
32+
<div data-testid="fetchStatus1">{result[0].fetchStatus}</div>
33+
<div data-testid="fetchStatus2">{result[1].fetchStatus}</div>
34+
<div data-testid="data1">{result[0].data ?? 'undefined'}</div>
35+
<div data-testid="data2">{result[1].data ?? 'undefined'}</div>
36+
</div>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { render } from '@testing-library/svelte'
3+
import { sleep } from '@tanstack/query-test-utils'
4+
import IsRestoringExample from './IsRestoringExample.svelte'
5+
6+
describe('createQueries', () => {
7+
beforeEach(() => {
8+
vi.useFakeTimers()
9+
})
10+
11+
afterEach(() => {
12+
vi.useRealTimers()
13+
})
14+
15+
it('should not fetch for the duration of the restoring period when isRestoring is true', async () => {
16+
const queryFn1 = vi.fn(() => sleep(10).then(() => 'data1'))
17+
const queryFn2 = vi.fn(() => sleep(10).then(() => 'data2'))
18+
19+
const rendered = render(IsRestoringExample, {
20+
props: { queryFn1, queryFn2 },
21+
})
22+
23+
await vi.advanceTimersByTimeAsync(0)
24+
25+
expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
26+
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
27+
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
28+
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
29+
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
30+
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
31+
expect(queryFn1).toHaveBeenCalledTimes(0)
32+
expect(queryFn2).toHaveBeenCalledTimes(0)
33+
34+
await vi.advanceTimersByTimeAsync(11)
35+
36+
expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
37+
expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
38+
expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
39+
expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
40+
expect(rendered.getByTestId('data1')).toHaveTextContent('undefined')
41+
expect(rendered.getByTestId('data2')).toHaveTextContent('undefined')
42+
expect(queryFn1).toHaveBeenCalledTimes(0)
43+
expect(queryFn2).toHaveBeenCalledTimes(0)
44+
})
45+
})

0 commit comments

Comments
 (0)