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 { queryKey, 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 = queryKey()
25 const key2 = queryKey()
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 = queryKey()
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 = queryKey()
113 const key2 = queryKey()
114 const queryFn1 = vi.fn(() => sleep(10).then(() => 'data1'))
115 const queryFn2 = vi.fn(() => sleep(10).then(() => 'data2'))
116β
117 function Page() {
118 const results = useQueries(() => ({
119 queries: [
120 { queryKey: key1, queryFn: queryFn1 },
121 { queryKey: key2, queryFn: queryFn2 },
122 ],
123 }))
124β
125 return (
126 <div>
127 <div data-testid="status1">{results[0].status}</div>
128 <div data-testid="status2">{results[1].status}</div>
129 <div data-testid="fetchStatus1">{results[0].fetchStatus}</div>
130 <div data-testid="fetchStatus2">{results[1].fetchStatus}</div>
131 </div>
132 )
133 }
134β
135 const rendered = renderWithClient(queryClient, () => (
136 <IsRestoringContext value={() => true}>
137 <Page />
138 </IsRestoringContext>
139 ))
140β
141 await vi.advanceTimersByTimeAsync(0)
142β
143 expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
144 expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
145 expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
146 expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
147 expect(queryFn1).toHaveBeenCalledTimes(0)
148 expect(queryFn2).toHaveBeenCalledTimes(0)
149β
150 await vi.advanceTimersByTimeAsync(10)
151β
152 expect(rendered.getByTestId('status1')).toHaveTextContent('pending')
153 expect(rendered.getByTestId('status2')).toHaveTextContent('pending')
154 expect(rendered.getByTestId('fetchStatus1')).toHaveTextContent('idle')
155 expect(rendered.getByTestId('fetchStatus2')).toHaveTextContent('idle')
156 expect(queryFn1).toHaveBeenCalledTimes(0)
157 expect(queryFn2).toHaveBeenCalledTimes(0)
158 })
159})