Open History to see the last commit
1// useQueries under the 2.0 read layer: every result is a full useQuery-shaped
2// read (suspending data, non-nullable when settled), positionally keyed so
3// option changes flow into existing rows and length changes create/dispose
4// tail rows. `combine` is a derived read over the results.
5import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
6import { For, Loading, createSignal } from 'solid-js'
7import { createKey, sleep } from '@tanstack/query-test-utils'
8import { QueryCache, QueryClient, useQueries } from '..'
9import { renderWithClient } from './utils'
10β
11describe('useQueries 2.0 read semantics', () => {
12 let queryCache: QueryCache
13 let queryClient: QueryClient
14β
15 beforeEach(() => {
16 vi.useFakeTimers()
17 queryCache = new QueryCache()
18 queryClient = new QueryClient({ queryCache })
19 })
20β
21 afterEach(() => {
22 queryClient.clear()
23 vi.useRealTimers()
24 })
25β
26 it('suspends all first loads into <Loading> and renders when settled', async () => {
27 const key1 = createKey()
28 const key2 = createKey()
29β
30 function Page() {
31 const results = useQueries(() => ({
32 queries: [
33 { queryKey: key1, queryFn: () => sleep(10).then(() => 'one') },
34 { queryKey: key2, queryFn: () => sleep(20).then(() => 'two') },
35 ],
36 }))
37 return (
38 <div>
39 <span>{results[0].data}</span>
40 <span>{results[1].data}</span>
41 </div>
42 )
43 }
44β
45 const rendered = renderWithClient(queryClient, () => (
46 <Loading fallback={<span>loading</span>}>
47 <Page />
48 </Loading>
49 ))
50β
51 expect(rendered.getByText('loading')).toBeInTheDocument()
52 await vi.advanceTimersByTimeAsync(20)
53 expect(rendered.getByText('one')).toBeInTheDocument()
54 expect(rendered.getByText('two')).toBeInTheDocument()
55 })
56β
57 it('derives combine reactively over the results', async () => {
58 const key1 = createKey()
59 const key2 = createKey()
60β
61 function Page() {
62 const summary = useQueries(() => ({
63 queries: [
64 { queryKey: key1, queryFn: () => sleep(10).then(() => 1) },
65 { queryKey: key2, queryFn: () => sleep(10).then(() => 2) },
66 ],
67 combine: (results) => ({
68 total: results.reduce((sum, r) => sum + r.data, 0),
69 }),
70 }))
71 return <span>total: {summary.total}</span>
72 }
73β
74 const rendered = renderWithClient(queryClient, () => (
75 <Loading fallback={<span>loading</span>}>
76 <Page />
77 </Loading>
78 ))
79β
80 expect(rendered.getByText('loading')).toBeInTheDocument()
81 await vi.advanceTimersByTimeAsync(10)
82 expect(rendered.getByText('total: 3')).toBeInTheDocument()
83β
84 queryClient.setQueryData(key2, 10)
85 await vi.advanceTimersByTimeAsync(0)
86 expect(rendered.getByText('total: 11')).toBeInTheDocument()
87 })
88β
89 it('creates and disposes rows as the queries array grows and shrinks', async () => {
90 const key1 = createKey()
91 const key2 = createKey()
92 const [keys, setKeys] = createSignal([key1])
93β
94 function Page() {
95 const results = useQueries(() => ({
96 queries: keys().map((key, i) => ({
97 queryKey: key,
98 queryFn: () => sleep(10).then(() => `value${i + 1}`),
99 })),
100 }))
101 return (
102 <div>
103 <For each={results}>{(result) => <span>{result.data}</span>}</For>
104 <span>count: {results.length}</span>
105 </div>
106 )
107 }
108β
109 const rendered = renderWithClient(queryClient, () => (
110 <Loading fallback={<span>loading</span>}>
111 <Page />
112 </Loading>
113 ))
114β
115 await vi.advanceTimersByTimeAsync(10)
116 expect(rendered.getByText('value1')).toBeInTheDocument()
117 expect(rendered.getByText('count: 1')).toBeInTheDocument()
118β
119 setKeys([key1, key2])
120 await vi.advanceTimersByTimeAsync(10)
121 expect(rendered.getByText('value1')).toBeInTheDocument()
122 expect(rendered.getByText('value2')).toBeInTheDocument()
123 expect(rendered.getByText('count: 2')).toBeInTheDocument()
124β
125 setKeys([key1])
126 await vi.advanceTimersByTimeAsync(0)
127 expect(rendered.queryByText('value2')).not.toBeInTheDocument()
128 expect(rendered.getByText('count: 1')).toBeInTheDocument()
129 })
130})