Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queriesObserver.test.tsx

330 lines10.7 KB

1import { waitFor } from '@testing-library/react'
2import {
3 sleep,
4 queryKey,
5 createQueryClient,
6 mockLogger,
7} from '../../reactjs/tests/utils'
8import {
9 QueryClient,
10 QueriesObserver,
11 QueryObserverResult,
12 QueryObserver,
13} from '../..'
14import { QueryKey } from '..'
15​
16describe('queriesObserver', () => {
17 let queryClient: QueryClient
18​
19 beforeEach(() => {
20 queryClient = createQueryClient()
21 queryClient.mount()
22 })
23​
24 afterEach(() => {
25 queryClient.clear()
26 })
27​
28 test('should return an array with all query results', async () => {
29 const key1 = queryKey()
30 const key2 = queryKey()
31 const queryFn1 = jest.fn().mockReturnValue(1)
32 const queryFn2 = jest.fn().mockReturnValue(2)
33 const observer = new QueriesObserver(queryClient, [
34 { queryKey: key1, queryFn: queryFn1 },
35 { queryKey: key2, queryFn: queryFn2 },
36 ])
37 let observerResult
38 const unsubscribe = observer.subscribe(result => {
39 observerResult = result
40 })
41 await sleep(1)
42 unsubscribe()
43 expect(observerResult).toMatchObject([{ data: 1 }, { data: 2 }])
44 })
45​
46 test('should still return value for undefined query key', async () => {
47 const key1 = queryKey()
48 const queryFn1 = jest.fn().mockReturnValue(1)
49 const queryFn2 = jest.fn().mockReturnValue(2)
50 const observer = new QueriesObserver(queryClient, [
51 { queryKey: key1, queryFn: queryFn1 },
52 { queryKey: undefined, queryFn: queryFn2 },
53 ])
54 let observerResult
55 const unsubscribe = observer.subscribe(result => {
56 observerResult = result
57 })
58 await sleep(1)
59 unsubscribe()
60 expect(observerResult).toMatchObject([{ data: 1 }, { data: 2 }])
61​
62 expect(mockLogger.error).toHaveBeenCalledTimes(1)
63 })
64​
65 test('should update when a query updates', async () => {
66 const key1 = queryKey()
67 const key2 = queryKey()
68 const queryFn1 = jest.fn().mockReturnValue(1)
69 const queryFn2 = jest.fn().mockReturnValue(2)
70 const observer = new QueriesObserver(queryClient, [
71 { queryKey: key1, queryFn: queryFn1 },
72 { queryKey: key2, queryFn: queryFn2 },
73 ])
74 const results: QueryObserverResult[][] = []
75 results.push(observer.getCurrentResult())
76 const unsubscribe = observer.subscribe(result => {
77 results.push(result)
78 })
79 await sleep(1)
80 queryClient.setQueryData(key2, 3)
81 await sleep(1)
82 unsubscribe()
83 expect(results.length).toBe(6)
84 expect(results[0]).toMatchObject([
85 { status: 'loading', fetchStatus: 'idle', data: undefined },
86 { status: 'loading', fetchStatus: 'idle', data: undefined },
87 ])
88 expect(results[1]).toMatchObject([
89 { status: 'loading', fetchStatus: 'fetching', data: undefined },
90 { status: 'loading', fetchStatus: 'idle', data: undefined },
91 ])
92 expect(results[2]).toMatchObject([
93 { status: 'loading', fetchStatus: 'fetching', data: undefined },
94 { status: 'loading', fetchStatus: 'fetching', data: undefined },
95 ])
96 expect(results[3]).toMatchObject([
97 { status: 'success', data: 1 },
98 { status: 'loading', fetchStatus: 'fetching', data: undefined },
99 ])
100 expect(results[4]).toMatchObject([
101 { status: 'success', data: 1 },
102 { status: 'success', data: 2 },
103 ])
104 expect(results[5]).toMatchObject([
105 { status: 'success', data: 1 },
106 { status: 'success', data: 3 },
107 ])
108 })
109​
110 test('should update when a query is removed', async () => {
111 const key1 = queryKey()
112 const key2 = queryKey()
113 const queryFn1 = jest.fn().mockReturnValue(1)
114 const queryFn2 = jest.fn().mockReturnValue(2)
115 const observer = new QueriesObserver(queryClient, [
116 { queryKey: key1, queryFn: queryFn1 },
117 { queryKey: key2, queryFn: queryFn2 },
118 ])
119 const results: QueryObserverResult[][] = []
120 results.push(observer.getCurrentResult())
121 const unsubscribe = observer.subscribe(result => {
122 results.push(result)
123 })
124 await sleep(1)
125 observer.setQueries([{ queryKey: key2, queryFn: queryFn2 }])
126 await sleep(1)
127 const queryCache = queryClient.getQueryCache()
128 expect(queryCache.find(key1, { type: 'active' })).toBeUndefined()
129 expect(queryCache.find(key2, { type: 'active' })).toBeDefined()
130 unsubscribe()
131 expect(queryCache.find(key1, { type: 'active' })).toBeUndefined()
132 expect(queryCache.find(key2, { type: 'active' })).toBeUndefined()
133 expect(results.length).toBe(6)
134 expect(results[0]).toMatchObject([
135 { status: 'loading', fetchStatus: 'idle', data: undefined },
136 { status: 'loading', fetchStatus: 'idle', data: undefined },
137 ])
138 expect(results[1]).toMatchObject([
139 { status: 'loading', fetchStatus: 'fetching', data: undefined },
140 { status: 'loading', fetchStatus: 'idle', data: undefined },
141 ])
142 expect(results[2]).toMatchObject([
143 { status: 'loading', fetchStatus: 'fetching', data: undefined },
144 { status: 'loading', fetchStatus: 'fetching', data: undefined },
145 ])
146 expect(results[3]).toMatchObject([
147 { status: 'success', data: 1 },
148 { status: 'loading', fetchStatus: 'fetching', data: undefined },
149 ])
150 expect(results[4]).toMatchObject([
151 { status: 'success', data: 1 },
152 { status: 'success', data: 2 },
153 ])
154 expect(results[5]).toMatchObject([{ status: 'success', data: 2 }])
155 })
156​
157 test('should update when a query changed position', async () => {
158 const key1 = queryKey()
159 const key2 = queryKey()
160 const queryFn1 = jest.fn().mockReturnValue(1)
161 const queryFn2 = jest.fn().mockReturnValue(2)
162 const observer = new QueriesObserver(queryClient, [
163 { queryKey: key1, queryFn: queryFn1 },
164 { queryKey: key2, queryFn: queryFn2 },
165 ])
166 const results: QueryObserverResult[][] = []
167 results.push(observer.getCurrentResult())
168 const unsubscribe = observer.subscribe(result => {
169 results.push(result)
170 })
171 await sleep(1)
172 observer.setQueries([
173 { queryKey: key2, queryFn: queryFn2 },
174 { queryKey: key1, queryFn: queryFn1 },
175 ])
176 await sleep(1)
177 unsubscribe()
178 expect(results.length).toBe(6)
179 expect(results[0]).toMatchObject([
180 { status: 'loading', fetchStatus: 'idle', data: undefined },
181 { status: 'loading', fetchStatus: 'idle', data: undefined },
182 ])
183 expect(results[1]).toMatchObject([
184 { status: 'loading', fetchStatus: 'fetching', data: undefined },
185 { status: 'loading', fetchStatus: 'idle', data: undefined },
186 ])
187 expect(results[2]).toMatchObject([
188 { status: 'loading', fetchStatus: 'fetching', data: undefined },
189 { status: 'loading', fetchStatus: 'fetching', data: undefined },
190 ])
191 expect(results[3]).toMatchObject([
192 { status: 'success', data: 1 },
193 { status: 'loading', fetchStatus: 'fetching', data: undefined },
194 ])
195 expect(results[4]).toMatchObject([
196 { status: 'success', data: 1 },
197 { status: 'success', data: 2 },
198 ])
199 expect(results[5]).toMatchObject([
200 { status: 'success', data: 2 },
201 { status: 'success', data: 1 },
202 ])
203 })
204​
205 test('should not update when nothing has changed', async () => {
206 const key1 = queryKey()
207 const key2 = queryKey()
208 const queryFn1 = jest.fn().mockReturnValue(1)
209 const queryFn2 = jest.fn().mockReturnValue(2)
210 const observer = new QueriesObserver(queryClient, [
211 { queryKey: key1, queryFn: queryFn1 },
212 { queryKey: key2, queryFn: queryFn2 },
213 ])
214 const results: QueryObserverResult[][] = []
215 results.push(observer.getCurrentResult())
216 const unsubscribe = observer.subscribe(result => {
217 results.push(result)
218 })
219 await sleep(1)
220 observer.setQueries([
221 { queryKey: key1, queryFn: queryFn1 },
222 { queryKey: key2, queryFn: queryFn2 },
223 ])
224 await sleep(1)
225 unsubscribe()
226 expect(results.length).toBe(5)
227 expect(results[0]).toMatchObject([
228 { status: 'loading', fetchStatus: 'idle', data: undefined },
229 { status: 'loading', fetchStatus: 'idle', data: undefined },
230 ])
231 expect(results[1]).toMatchObject([
232 { status: 'loading', fetchStatus: 'fetching', data: undefined },
233 { status: 'loading', fetchStatus: 'idle', data: undefined },
234 ])
235 expect(results[2]).toMatchObject([
236 { status: 'loading', fetchStatus: 'fetching', data: undefined },
237 { status: 'loading', fetchStatus: 'fetching', data: undefined },
238 ])
239 expect(results[3]).toMatchObject([
240 { status: 'success', data: 1 },
241 { status: 'loading', fetchStatus: 'fetching', data: undefined },
242 ])
243 expect(results[4]).toMatchObject([
244 { status: 'success', data: 1 },
245 { status: 'success', data: 2 },
246 ])
247 })
248​
249 test('should trigger all fetches when subscribed', async () => {
250 const key1 = queryKey()
251 const key2 = queryKey()
252 const queryFn1 = jest.fn().mockReturnValue(1)
253 const queryFn2 = jest.fn().mockReturnValue(2)
254 const observer = new QueriesObserver(queryClient, [
255 { queryKey: key1, queryFn: queryFn1 },
256 { queryKey: key2, queryFn: queryFn2 },
257 ])
258 const unsubscribe = observer.subscribe(() => undefined)
259 await sleep(1)
260 unsubscribe()
261 expect(queryFn1).toHaveBeenCalledTimes(1)
262 expect(queryFn2).toHaveBeenCalledTimes(1)
263 })
264​
265 test('should not destroy the observer if there is still a subscription', async () => {
266 const key1 = queryKey()
267 const observer = new QueriesObserver(queryClient, [
268 {
269 queryKey: key1,
270 queryFn: async () => {
271 await sleep(20)
272 return 1
273 },
274 },
275 ])
276​
277 const subscription1Handler = jest.fn()
278 const subscription2Handler = jest.fn()
279​
280 const unsubscribe1 = observer.subscribe(subscription1Handler)
281 const unsubscribe2 = observer.subscribe(subscription2Handler)
282​
283 unsubscribe1()
284​
285 await waitFor(() => {
286 // 1 call: loading
287 expect(subscription1Handler).toBeCalledTimes(1)
288 // 1 call: success
289 expect(subscription2Handler).toBeCalledTimes(1)
290 })
291​
292 // Clean-up
293 unsubscribe2()
294 })
295​
296 test('onUpdate should not update the result for an unknown observer', async () => {
297 const key1 = queryKey()
298 const key2 = queryKey()
299​
300 const queriesObserver = new QueriesObserver(queryClient, [
301 {
302 queryKey: key1,
303 queryFn: () => 1,
304 },
305 ])
306​
307 const newQueryObserver = new QueryObserver<
308 unknown,
309 unknown,
310 unknown,
311 unknown,
312 QueryKey
313 >(queryClient, {
314 queryKey: key2,
315 queryFn: () => 2,
316 })
317​
318 // Force onUpdate with an unknown QueryObserver
319 // because no existing use case has been found in the lib
320 queriesObserver['onUpdate'](
321 newQueryObserver,
322 // The current queries observer result is re-used here
323 // to use a typescript friendly result
324 queriesObserver.getCurrentResult()[0]!
325 )
326​
327 // Should not alter the result
328 expect(queriesObserver.getCurrentResult()[-1]).toBeUndefined()
329 })
330})