Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@solidjs/testing-library'
3import { Loading } from 'solid-js'
4import { createKey, sleep } from '@tanstack/query-test-utils'
5import { IsRestoringContext, QueryCache, QueryClient, useQueries } from '..'
6import { renderWithClient } from './utils'
7β
8describe('useQueries', () => {
9 let queryCache: QueryCache
10 let queryClient: QueryClient
11β
12 beforeEach(() => {
13 vi.useFakeTimers()
14 queryCache = new QueryCache()
15 queryClient = new QueryClient({ queryCache })
16 })
17β
18 afterEach(() => {
19 queryClient.clear()
20 vi.useRealTimers()
21 })
22β
23 it('should render each result as it settles', async () => {
24 const key1 = createKey()
25 const key2 = createKey()
26β
27 function Page() {
28 const results = useQueries(() => ({
29 queries: [
30 {
31 queryKey: key1,
32 queryFn: () => sleep(10).then(() => 1),
33 },
34 {
35 queryKey: key2,
36 queryFn: () => sleep(100).then(() => 2),
37 },
38 ],
39 }))
40β
41 return (
42 <div>
43 <div>
44 status1: {results[0].status}, status2: {results[1].status}
45 </div>
46 <Loading fallback={<span>loading1</span>}>
47 <span>data1: {String(results[0].data)}</span>
48 </Loading>
49 <Loading fallback={<span>loading2</span>}>
50 <span>data2: {String(results[1].data)}</span>
51 </Loading>
52 </div>
53 )
54 }
55β
56 const rendered = renderWithClient(queryClient, () => <Page />)
57β
58 // Metadata reads never suspend; data reads park their own boundary.
59 expect(
60 rendered.getByText('status1: pending, status2: pending'),
61 ).toBeInTheDocument()
62 expect(rendered.getByText('loading1')).toBeInTheDocument()
63 expect(rendered.getByText('loading2')).toBeInTheDocument()
64β
65 await vi.advanceTimersByTimeAsync(10)
66 expect(rendered.getByText('data1: 1')).toBeInTheDocument()
67 expect(rendered.getByText('loading2')).toBeInTheDocument()
68 expect(
69 rendered.getByText('status1: success, status2: pending'),
70 ).toBeInTheDocument()
71β
72 await vi.advanceTimersByTimeAsync(90)
73 expect(rendered.getByText('data1: 1')).toBeInTheDocument()
74 expect(rendered.getByText('data2: 2')).toBeInTheDocument()
75 expect(
76 rendered.getByText('status1: success, status2: success'),
77 ).toBeInTheDocument()
78 })
79β
80 it('should use provided custom queryClient', async () => {
81 const key = createKey()
82 const queryFn = () => sleep(10).then(() => 'custom client')
83β
84 function Page() {
85 const queries = useQueries(
86 () => ({
87 queries: [
88 {
89 queryKey: key,
90 queryFn,
91 },
92 ],
93 }),
94 () => queryClient,
95 )
96β
97 return <div>data: {queries[0].data}</div>
98 }
99β
100 const rendered = render(() => (
101 <Loading fallback={<span>loading</span>}>
102 <Page />
103 </Loading>
104 ))
105β
106 expect(rendered.getByText('loading')).toBeInTheDocument()
107 await vi.advanceTimersByTimeAsync(10)
108 expect(rendered.getByText('data: custom client')).toBeInTheDocument()
109 })
110β
111 it('should not fetch for the duration of the restoring period when isRestoring is true', async () => {
112 const key1 = createKey()
113 const key2 = createKey()
114 const queryFn1 = vi.fn<() => Promise<string>>(() =>
115 sleep(10).then(() => 'data1'),
116 )
117 const queryFn2 = vi.fn<() => Promise<string>>(() =>
118 sleep(10).then(() => 'data2'),
119 )
120β
121 function Page() {
122 const results = useQueries(() => ({
123 queries: [
124 { queryKey: key1, queryFn: queryFn1 },
125 { queryKey: key2, queryFn: queryFn2 },
126 ],
127 }))
128β
129 return (
130 <div>
131 <div data-testid="status1">{results[0].status}</div>
132 <div data-testid="status2">{results[1].status}</div>
133 <div data-testid="fetchStatus1">{results[0].fetchStatus}</div>
134 <div data-testid="fetchStatus2">{results[1].fetchStatus}</div>
135 </div>
136 )
137 }
138β
139 const rendered = renderWithClient(queryClient, () => (
140 <IsRestoringContext value={() => true}>
141 <Page />
142 </IsRestoringContext>
143 ))
144β
145 await vi.advanceTimersByTimeAsync(0)
146 expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
147 expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
148 expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
149 expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
150 expect(queryFn1).toHaveBeenCalledTimes(0)
151 expect(queryFn2).toHaveBeenCalledTimes(0)
152 await vi.advanceTimersByTimeAsync(10)
153 expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
154 expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
155 expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
156 expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
157 expect(queryFn1).toHaveBeenCalledTimes(0)
158 expect(queryFn2).toHaveBeenCalledTimes(0)
159 })
160})