Open History to see the last commit
1import { sleep, queryKey } from '../../react/tests/utils'
2import { QueryClient, QueriesObserver, QueryObserverResult } from '../..'
3β
4describe('queriesObserver', () => {
5 let queryClient: QueryClient
6β
7 beforeEach(() => {
8 queryClient = new QueryClient()
9 queryClient.mount()
10 })
11β
12 afterEach(() => {
13 queryClient.clear()
14 })
15β
16 test('should return an array with all query results', async () => {
17 const key1 = queryKey()
18 const key2 = queryKey()
19 const queryFn1 = jest.fn().mockReturnValue(1)
20 const queryFn2 = jest.fn().mockReturnValue(2)
21 const observer = new QueriesObserver(queryClient, [
22 { queryKey: key1, queryFn: queryFn1 },
23 { queryKey: key2, queryFn: queryFn2 },
24 ])
25 let observerResult
26 const unsubscribe = observer.subscribe(result => {
27 observerResult = result
28 })
29 await sleep(1)
30 unsubscribe()
31 expect(observerResult).toMatchObject([{ data: 1 }, { data: 2 }])
32 })
33β
34 test('should update when a query updates', async () => {
35 const key1 = queryKey()
36 const key2 = queryKey()
37 const queryFn1 = jest.fn().mockReturnValue(1)
38 const queryFn2 = jest.fn().mockReturnValue(2)
39 const observer = new QueriesObserver(queryClient, [
40 { queryKey: key1, queryFn: queryFn1 },
41 { queryKey: key2, queryFn: queryFn2 },
42 ])
43 const results: QueryObserverResult[][] = []
44 results.push(observer.getCurrentResult())
45 const unsubscribe = observer.subscribe(result => {
46 results.push(result)
47 })
48 await sleep(1)
49 queryClient.setQueryData(key2, 3)
50 await sleep(1)
51 unsubscribe()
52 expect(results.length).toBe(6)
53 expect(results[0]).toMatchObject([
54 { status: 'idle', data: undefined },
55 { status: 'idle', data: undefined },
56 ])
57 expect(results[1]).toMatchObject([
58 { status: 'loading', data: undefined },
59 { status: 'idle', data: undefined },
60 ])
61 expect(results[2]).toMatchObject([
62 { status: 'loading', data: undefined },
63 { status: 'loading', data: undefined },
64 ])
65 expect(results[3]).toMatchObject([
66 { status: 'success', data: 1 },
67 { status: 'loading', data: undefined },
68 ])
69 expect(results[4]).toMatchObject([
70 { status: 'success', data: 1 },
71 { status: 'success', data: 2 },
72 ])
73 expect(results[5]).toMatchObject([
74 { status: 'success', data: 1 },
75 { status: 'success', data: 3 },
76 ])
77 })
78β
79 test('should update when a query is removed', async () => {
80 const key1 = queryKey()
81 const key2 = queryKey()
82 const queryFn1 = jest.fn().mockReturnValue(1)
83 const queryFn2 = jest.fn().mockReturnValue(2)
84 const observer = new QueriesObserver(queryClient, [
85 { queryKey: key1, queryFn: queryFn1 },
86 { queryKey: key2, queryFn: queryFn2 },
87 ])
88 const results: QueryObserverResult[][] = []
89 results.push(observer.getCurrentResult())
90 const unsubscribe = observer.subscribe(result => {
91 results.push(result)
92 })
93 await sleep(1)
94 observer.setQueries([{ queryKey: key2, queryFn: queryFn2 }])
95 await sleep(1)
96 const queryCache = queryClient.getQueryCache()
97 expect(queryCache.find(key1, { active: true })).toBeUndefined()
98 expect(queryCache.find(key2, { active: true })).toBeDefined()
99 unsubscribe()
100 expect(queryCache.find(key1, { active: true })).toBeUndefined()
101 expect(queryCache.find(key2, { active: true })).toBeUndefined()
102 expect(results.length).toBe(6)
103 expect(results[0]).toMatchObject([
104 { status: 'idle', data: undefined },
105 { status: 'idle', data: undefined },
106 ])
107 expect(results[1]).toMatchObject([
108 { status: 'loading', data: undefined },
109 { status: 'idle', data: undefined },
110 ])
111 expect(results[2]).toMatchObject([
112 { status: 'loading', data: undefined },
113 { status: 'loading', data: undefined },
114 ])
115 expect(results[3]).toMatchObject([
116 { status: 'success', data: 1 },
117 { status: 'loading', data: undefined },
118 ])
119 expect(results[4]).toMatchObject([
120 { status: 'success', data: 1 },
121 { status: 'success', data: 2 },
122 ])
123 expect(results[5]).toMatchObject([{ status: 'success', data: 2 }])
124 })
125β
126 test('should update when a query changed position', async () => {
127 const key1 = queryKey()
128 const key2 = queryKey()
129 const queryFn1 = jest.fn().mockReturnValue(1)
130 const queryFn2 = jest.fn().mockReturnValue(2)
131 const observer = new QueriesObserver(queryClient, [
132 { queryKey: key1, queryFn: queryFn1 },
133 { queryKey: key2, queryFn: queryFn2 },
134 ])
135 const results: QueryObserverResult[][] = []
136 results.push(observer.getCurrentResult())
137 const unsubscribe = observer.subscribe(result => {
138 results.push(result)
139 })
140 await sleep(1)
141 observer.setQueries([
142 { queryKey: key2, queryFn: queryFn2 },
143 { queryKey: key1, queryFn: queryFn1 },
144 ])
145 await sleep(1)
146 unsubscribe()
147 expect(results.length).toBe(6)
148 expect(results[0]).toMatchObject([
149 { status: 'idle', data: undefined },
150 { status: 'idle', data: undefined },
151 ])
152 expect(results[1]).toMatchObject([
153 { status: 'loading', data: undefined },
154 { status: 'idle', data: undefined },
155 ])
156 expect(results[2]).toMatchObject([
157 { status: 'loading', data: undefined },
158 { status: 'loading', data: undefined },
159 ])
160 expect(results[3]).toMatchObject([
161 { status: 'success', data: 1 },
162 { status: 'loading', data: undefined },
163 ])
164 expect(results[4]).toMatchObject([
165 { status: 'success', data: 1 },
166 { status: 'success', data: 2 },
167 ])
168 expect(results[5]).toMatchObject([
169 { status: 'success', data: 2 },
170 { status: 'success', data: 1 },
171 ])
172 })
173β
174 test('should not update when nothing has changed', async () => {
175 const key1 = queryKey()
176 const key2 = queryKey()
177 const queryFn1 = jest.fn().mockReturnValue(1)
178 const queryFn2 = jest.fn().mockReturnValue(2)
179 const observer = new QueriesObserver(queryClient, [
180 { queryKey: key1, queryFn: queryFn1 },
181 { queryKey: key2, queryFn: queryFn2 },
182 ])
183 const results: QueryObserverResult[][] = []
184 results.push(observer.getCurrentResult())
185 const unsubscribe = observer.subscribe(result => {
186 results.push(result)
187 })
188 await sleep(1)
189 observer.setQueries([
190 { queryKey: key1, queryFn: queryFn1 },
191 { queryKey: key2, queryFn: queryFn2 },
192 ])
193 await sleep(1)
194 unsubscribe()
195 expect(results.length).toBe(5)
196 expect(results[0]).toMatchObject([
197 { status: 'idle', data: undefined },
198 { status: 'idle', data: undefined },
199 ])
200 expect(results[1]).toMatchObject([
201 { status: 'loading', data: undefined },
202 { status: 'idle', data: undefined },
203 ])
204 expect(results[2]).toMatchObject([
205 { status: 'loading', data: undefined },
206 { status: 'loading', data: undefined },
207 ])
208 expect(results[3]).toMatchObject([
209 { status: 'success', data: 1 },
210 { status: 'loading', data: undefined },
211 ])
212 expect(results[4]).toMatchObject([
213 { status: 'success', data: 1 },
214 { status: 'success', data: 2 },
215 ])
216 })
217β
218 test('should trigger all fetches when subscribed', async () => {
219 const key1 = queryKey()
220 const key2 = queryKey()
221 const queryFn1 = jest.fn()
222 const queryFn2 = jest.fn()
223 const observer = new QueriesObserver(queryClient, [
224 { queryKey: key1, queryFn: queryFn1 },
225 { queryKey: key2, queryFn: queryFn2 },
226 ])
227 const unsubscribe = observer.subscribe()
228 await sleep(1)
229 unsubscribe()
230 expect(queryFn1).toHaveBeenCalledTimes(1)
231 expect(queryFn2).toHaveBeenCalledTimes(1)
232 })
233})