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