Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { waitFor } from '@testing-library/react'
3import { QueriesObserver } from '..'
4import { createQueryClient, queryKey, sleep } from './utils'
5import type { QueryClient, QueryObserverResult } 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 = vi.fn().mockReturnValue(1)
23 const queryFn2 = vi.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 consoleMock = vi.spyOn(console, 'error')
39 consoleMock.mockImplementation(() => undefined)
40 const key1 = queryKey()
41 const queryFn1 = vi.fn().mockReturnValue(1)
42 const queryFn2 = vi.fn().mockReturnValue(2)
43 const observer = new QueriesObserver(queryClient, [
44 { queryKey: key1, queryFn: queryFn1 },
45 { queryKey: undefined, queryFn: queryFn2 },
46 ])
47 let observerResult
48 const unsubscribe = observer.subscribe((result) => {
49 observerResult = result
50 })
51 await sleep(1)
52 unsubscribe()
53 expect(observerResult).toMatchObject([{ data: 1 }, { data: 2 }])
54β
55 expect(consoleMock).toHaveBeenCalledTimes(1)
56 expect(consoleMock).toHaveBeenCalledWith(
57 "As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']",
58 )
59 consoleMock.mockRestore()
60 })
61β
62 test('should update when a query updates', async () => {
63 const key1 = queryKey()
64 const key2 = queryKey()
65 const queryFn1 = vi.fn().mockReturnValue(1)
66 const queryFn2 = vi.fn().mockReturnValue(2)
67 const observer = new QueriesObserver(queryClient, [
68 { queryKey: key1, queryFn: queryFn1 },
69 { queryKey: key2, queryFn: queryFn2 },
70 ])
71 const results: Array<Array<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: 'pending', fetchStatus: 'idle', data: undefined },
83 { status: 'pending', fetchStatus: 'idle', data: undefined },
84 ])
85 expect(results[1]).toMatchObject([
86 { status: 'pending', fetchStatus: 'fetching', data: undefined },
87 { status: 'pending', fetchStatus: 'idle', data: undefined },
88 ])
89 expect(results[2]).toMatchObject([
90 { status: 'pending', fetchStatus: 'fetching', data: undefined },
91 { status: 'pending', fetchStatus: 'fetching', data: undefined },
92 ])
93 expect(results[3]).toMatchObject([
94 { status: 'success', data: 1 },
95 { status: 'pending', 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 = vi.fn().mockReturnValue(1)
111 const queryFn2 = vi.fn().mockReturnValue(2)
112 const observer = new QueriesObserver(queryClient, [
113 { queryKey: key1, queryFn: queryFn1 },
114 { queryKey: key2, queryFn: queryFn2 },
115 ])
116 const results: Array<Array<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({ queryKey: key1, type: 'active' })).toBeUndefined()
126 expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeDefined()
127 unsubscribe()
128 expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
129 expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeUndefined()
130 expect(results.length).toBe(6)
131 expect(results[0]).toMatchObject([
132 { status: 'pending', fetchStatus: 'idle', data: undefined },
133 { status: 'pending', fetchStatus: 'idle', data: undefined },
134 ])
135 expect(results[1]).toMatchObject([
136 { status: 'pending', fetchStatus: 'fetching', data: undefined },
137 { status: 'pending', fetchStatus: 'idle', data: undefined },
138 ])
139 expect(results[2]).toMatchObject([
140 { status: 'pending', fetchStatus: 'fetching', data: undefined },
141 { status: 'pending', fetchStatus: 'fetching', data: undefined },
142 ])
143 expect(results[3]).toMatchObject([
144 { status: 'success', data: 1 },
145 { status: 'pending', 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 = vi.fn().mockReturnValue(1)
158 const queryFn2 = vi.fn().mockReturnValue(2)
159 const observer = new QueriesObserver(queryClient, [
160 { queryKey: key1, queryFn: queryFn1 },
161 { queryKey: key2, queryFn: queryFn2 },
162 ])
163 const results: Array<Array<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: 'pending', fetchStatus: 'idle', data: undefined },
178 { status: 'pending', fetchStatus: 'idle', data: undefined },
179 ])
180 expect(results[1]).toMatchObject([
181 { status: 'pending', fetchStatus: 'fetching', data: undefined },
182 { status: 'pending', fetchStatus: 'idle', data: undefined },
183 ])
184 expect(results[2]).toMatchObject([
185 { status: 'pending', fetchStatus: 'fetching', data: undefined },
186 { status: 'pending', fetchStatus: 'fetching', data: undefined },
187 ])
188 expect(results[3]).toMatchObject([
189 { status: 'success', data: 1 },
190 { status: 'pending', 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 = vi.fn().mockReturnValue(1)
206 const queryFn2 = vi.fn().mockReturnValue(2)
207 const observer = new QueriesObserver(queryClient, [
208 { queryKey: key1, queryFn: queryFn1 },
209 { queryKey: key2, queryFn: queryFn2 },
210 ])
211 const results: Array<Array<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: 'pending', fetchStatus: 'idle', data: undefined },
226 { status: 'pending', fetchStatus: 'idle', data: undefined },
227 ])
228 expect(results[1]).toMatchObject([
229 { status: 'pending', fetchStatus: 'fetching', data: undefined },
230 { status: 'pending', fetchStatus: 'idle', data: undefined },
231 ])
232 expect(results[2]).toMatchObject([
233 { status: 'pending', fetchStatus: 'fetching', data: undefined },
234 { status: 'pending', fetchStatus: 'fetching', data: undefined },
235 ])
236 expect(results[3]).toMatchObject([
237 { status: 'success', data: 1 },
238 { status: 'pending', 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 = vi.fn().mockReturnValue(1)
250 const queryFn2 = vi.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 = vi.fn()
275 const subscription2Handler = vi.fn()
276β
277 const unsubscribe1 = observer.subscribe(subscription1Handler)
278 const unsubscribe2 = observer.subscribe(subscription2Handler)
279β
280 unsubscribe1()
281β
282 await waitFor(() => {
283 // 1 call: pending
284 expect(subscription1Handler).toBeCalledTimes(1)
285 // 1 call: success
286 expect(subscription2Handler).toBeCalledTimes(1)
287 })
288β
289 // Clean-up
290 unsubscribe2()
291 })
292})