Open History to see the last commit
1import {
2 afterAll,
3 afterEach,
4 beforeAll,
5 describe,
6 expect,
7 it,
8 vi,
9} from 'vitest'
10import { act, render } from '@testing-library/react'
11import * as React from 'react'
12import { useSuspenseQueries } from '..'
13import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
14import type { UseSuspenseQueryOptions } from '..'
15β
16type NumberQueryOptions = UseSuspenseQueryOptions<number>
17β
18const QUERY_DURATION = 1000
19β
20const createQuery: (id: number) => NumberQueryOptions = (id) => ({
21 queryKey: [id],
22 queryFn: async () => {
23 await sleep(QUERY_DURATION)
24 return id
25 },
26})
27const resolveQueries = () => vi.advanceTimersByTimeAsync(QUERY_DURATION)
28β
29const queryClient = createQueryClient()
30β
31describe('useSuspenseQueries', () => {
32 const onSuspend = vi.fn()
33 const onQueriesResolution = vi.fn()
34β
35 beforeAll(() => {
36 vi.useFakeTimers()
37 })
38β
39 afterAll(() => {
40 vi.useRealTimers()
41 })
42β
43 afterEach(() => {
44 queryClient.clear()
45 onSuspend.mockClear()
46 onQueriesResolution.mockClear()
47 })
48β
49 function SuspenseFallback() {
50 React.useEffect(() => {
51 onSuspend()
52 }, [])
53β
54 return null
55 }
56β
57 const withSuspenseWrapper = <T extends object>(Component: React.FC<T>) => {
58 function SuspendedComponent(props: T) {
59 return (
60 <React.Suspense fallback={<SuspenseFallback />}>
61 <Component {...props} />
62 </React.Suspense>
63 )
64 }
65β
66 return SuspendedComponent
67 }
68β
69 function QueriesContainer({
70 queries,
71 }: {
72 queries: Array<NumberQueryOptions>
73 }) {
74 const queriesResults = useSuspenseQueries(
75 { queries, combine: (results) => results.map((r) => r.data) },
76 queryClient,
77 )
78β
79 React.useEffect(() => {
80 onQueriesResolution(queriesResults)
81 }, [queriesResults])
82β
83 return null
84 }
85β
86 const TestComponent = withSuspenseWrapper(QueriesContainer)
87β
88 it('should suspend on mount', () => {
89 render(<TestComponent queries={[1, 2].map(createQuery)} />)
90β
91 expect(onSuspend).toHaveBeenCalledOnce()
92 })
93β
94 it('should resolve queries', async () => {
95 render(<TestComponent queries={[1, 2].map(createQuery)} />)
96β
97 await act(resolveQueries)
98β
99 expect(onQueriesResolution).toHaveBeenCalledTimes(1)
100 expect(onQueriesResolution).toHaveBeenLastCalledWith([1, 2])
101 })
102β
103 it('should not suspend on mount if query has been already fetched', () => {
104 const query = createQuery(1)
105β
106 queryClient.setQueryData(query.queryKey, query.queryFn)
107β
108 render(<TestComponent queries={[query]} />)
109β
110 expect(onSuspend).not.toHaveBeenCalled()
111 })
112β
113 it('should not break suspense when queries change without resolving', async () => {
114 const initQueries = [1, 2].map(createQuery)
115 const nextQueries = [3, 4, 5, 6].map(createQuery)
116β
117 const { rerender } = render(<TestComponent queries={initQueries} />)
118β
119 rerender(<TestComponent queries={nextQueries} />)
120β
121 await act(resolveQueries)
122β
123 expect(onSuspend).toHaveBeenCalledTimes(1)
124 expect(onQueriesResolution).toHaveBeenCalledTimes(1)
125 expect(onQueriesResolution).toHaveBeenLastCalledWith([3, 4, 5, 6])
126 })
127β
128 it('should suspend only once per queries change', async () => {
129 const initQueries = [1, 2].map(createQuery)
130 const nextQueries = [3, 4, 5, 6].map(createQuery)
131β
132 const { rerender } = render(<TestComponent queries={initQueries} />)
133β
134 await act(resolveQueries)
135β
136 rerender(<TestComponent queries={nextQueries} />)
137β
138 await act(resolveQueries)
139β
140 expect(onSuspend).toHaveBeenCalledTimes(2)
141 expect(onQueriesResolution).toHaveBeenCalledTimes(2)
142 expect(onQueriesResolution).toHaveBeenLastCalledWith([3, 4, 5, 6])
143 })
144β
145 it('should only call combine after resolving', async () => {
146 const spy = vi.fn()
147 const key = queryKey()
148β
149 function Page() {
150 const data = useSuspenseQueries({
151 queries: [1, 2, 3].map((value) => ({
152 queryKey: [...key, { value }],
153 queryFn: async () => {
154 await sleep(value * 10)
155 return { value: value * 10 }
156 },
157 })),
158 combine: (result) => {
159 spy(result)
160 return 'data'
161 },
162 })
163β
164 return <h1>{data}</h1>
165 }
166β
167 const rendered = renderWithClient(
168 queryClient,
169 <React.Suspense fallback="loading...">
170 <Page />
171 </React.Suspense>,
172 )
173β
174 await act(() => vi.advanceTimersByTimeAsync(10))
175β
176 rendered.getByText('loading...')
177β
178 expect(spy).not.toHaveBeenCalled()
179β
180 await act(() => vi.advanceTimersByTimeAsync(30))
181 rendered.getByText('data')
182β
183 expect(spy).toHaveBeenCalled()
184 })
185})