Open History to see the last commit
1import { sleep, queryKey, createQueryClient } from '../../../../tests/utils'
2import { QueryCache, QueryClient } from '..'
3import { Query } from '.././query'
4β
5describe('queryCache', () => {
6 let queryClient: QueryClient
7 let queryCache: QueryCache
8β
9 beforeEach(() => {
10 queryClient = createQueryClient()
11 queryCache = queryClient.getQueryCache()
12 })
13β
14 afterEach(() => {
15 queryClient.clear()
16 })
17β
18 describe('subscribe', () => {
19 test('should pass the correct query', async () => {
20 const key = queryKey()
21 const subscriber = jest.fn()
22 const unsubscribe = queryCache.subscribe(subscriber)
23 queryClient.setQueryData(key, 'foo')
24 const query = queryCache.find(key)
25 await sleep(1)
26 expect(subscriber).toHaveBeenCalledWith({ query, type: 'added' })
27 unsubscribe()
28 })
29β
30 test('should notify listeners when new query is added', async () => {
31 const key = queryKey()
32 const callback = jest.fn()
33 queryCache.subscribe(callback)
34 queryClient.prefetchQuery(key, () => 'data')
35 await sleep(100)
36 expect(callback).toHaveBeenCalled()
37 })
38β
39 test('should include the queryCache and query when notifying listeners', async () => {
40 const key = queryKey()
41 const callback = jest.fn()
42 queryCache.subscribe(callback)
43 queryClient.prefetchQuery(key, () => 'data')
44 const query = queryCache.find(key)
45 await sleep(100)
46 expect(callback).toHaveBeenCalledWith({ query, type: 'added' })
47 })
48β
49 test('should notify subscribers when new query with initialData is added', async () => {
50 const key = queryKey()
51 const callback = jest.fn()
52 queryCache.subscribe(callback)
53 queryClient.prefetchQuery(key, () => 'data', {
54 initialData: 'initial',
55 })
56 await sleep(100)
57 expect(callback).toHaveBeenCalled()
58 })
59 })
60β
61 describe('find', () => {
62 test('find should filter correctly', async () => {
63 const key = queryKey()
64 await queryClient.prefetchQuery(key, () => 'data1')
65 const query = queryCache.find(key)!
66 expect(query).toBeDefined()
67 })
68β
69 test('find should filter correctly with exact set to false', async () => {
70 const key = queryKey()
71 await queryClient.prefetchQuery(key, () => 'data1')
72 const query = queryCache.find(key, { exact: false })!
73 expect(query).toBeDefined()
74 })
75 })
76β
77 describe('findAll', () => {
78 test('should filter correctly', async () => {
79 const key1 = queryKey()
80 const key2 = queryKey()
81 const keyFetching = queryKey()
82 await queryClient.prefetchQuery(key1, () => 'data1')
83 await queryClient.prefetchQuery(key2, () => 'data2')
84 await queryClient.prefetchQuery([{ a: 'a', b: 'b' }], () => 'data3')
85 await queryClient.prefetchQuery(['posts', 1], () => 'data4')
86 queryClient.invalidateQueries(key2)
87 const query1 = queryCache.find(key1)!
88 const query2 = queryCache.find(key2)!
89 const query3 = queryCache.find([{ a: 'a', b: 'b' }])!
90 const query4 = queryCache.find(['posts', 1])!
91β
92 expect(queryCache.findAll(key1)).toEqual([query1])
93 // wrapping in an extra array doesn't yield the same results anymore since v4 because keys need to be an array
94 expect(queryCache.findAll([key1])).toEqual([])
95 expect(queryCache.findAll()).toEqual([query1, query2, query3, query4])
96 expect(queryCache.findAll({})).toEqual([query1, query2, query3, query4])
97 expect(queryCache.findAll(key1, { type: 'inactive' })).toEqual([query1])
98 expect(queryCache.findAll(key1, { type: 'active' })).toEqual([])
99 expect(queryCache.findAll(key1, { stale: true })).toEqual([])
100 expect(queryCache.findAll(key1, { stale: false })).toEqual([query1])
101 expect(
102 queryCache.findAll(key1, { stale: false, type: 'active' }),
103 ).toEqual([])
104 expect(
105 queryCache.findAll(key1, { stale: false, type: 'inactive' }),
106 ).toEqual([query1])
107 expect(
108 queryCache.findAll(key1, {
109 stale: false,
110 type: 'inactive',
111 exact: true,
112 }),
113 ).toEqual([query1])
114β
115 expect(queryCache.findAll(key2)).toEqual([query2])
116 expect(queryCache.findAll(key2, { stale: undefined })).toEqual([query2])
117 expect(queryCache.findAll(key2, { stale: true })).toEqual([query2])
118 expect(queryCache.findAll(key2, { stale: false })).toEqual([])
119 expect(queryCache.findAll([{ b: 'b' }])).toEqual([query3])
120 expect(queryCache.findAll([{ a: 'a' }], { exact: false })).toEqual([
121 query3,
122 ])
123 expect(queryCache.findAll([{ a: 'a' }], { exact: true })).toEqual([])
124 expect(queryCache.findAll([{ a: 'a', b: 'b' }], { exact: true })).toEqual(
125 [query3],
126 )
127 expect(queryCache.findAll([{ a: 'a', b: 'b' }])).toEqual([query3])
128 expect(queryCache.findAll([{ a: 'a', b: 'b', c: 'c' }])).toEqual([])
129 expect(queryCache.findAll([{ a: 'a' }], { stale: false })).toEqual([
130 query3,
131 ])
132 expect(queryCache.findAll([{ a: 'a' }], { stale: true })).toEqual([])
133 expect(queryCache.findAll([{ a: 'a' }], { type: 'active' })).toEqual([])
134 expect(queryCache.findAll([{ a: 'a' }], { type: 'inactive' })).toEqual([
135 query3,
136 ])
137 expect(
138 queryCache.findAll({ predicate: (query) => query === query3 }),
139 ).toEqual([query3])
140 expect(queryCache.findAll(['posts'])).toEqual([query4])
141β
142 expect(queryCache.findAll({ fetchStatus: 'idle' })).toEqual([
143 query1,
144 query2,
145 query3,
146 query4,
147 ])
148 expect(queryCache.findAll(key2, { fetchStatus: undefined })).toEqual([
149 query2,
150 ])
151β
152 const promise = queryClient.prefetchQuery(keyFetching, async () => {
153 await sleep(20)
154 return 'dataFetching'
155 })
156 expect(queryCache.findAll({ fetchStatus: 'fetching' })).toEqual([
157 queryCache.find(keyFetching),
158 ])
159 await promise
160 expect(queryCache.findAll({ fetchStatus: 'fetching' })).toEqual([])
161 })
162β
163 test('should return all the queries when no filters are defined', async () => {
164 const key1 = queryKey()
165 const key2 = queryKey()
166 await queryClient.prefetchQuery(key1, () => 'data1')
167 await queryClient.prefetchQuery(key2, () => 'data2')
168 expect(queryCache.findAll().length).toBe(2)
169 })
170 })
171β
172 describe('QueryCacheConfig.onError', () => {
173 test('should be called when a query errors', async () => {
174 const key = queryKey()
175 const onError = jest.fn()
176 const testCache = new QueryCache({ onError })
177 const testClient = createQueryClient({ queryCache: testCache })
178 await testClient.prefetchQuery(key, () =>
179 Promise.reject<unknown>('error'),
180 )
181 const query = testCache.find(key)
182 expect(onError).toHaveBeenCalledWith('error', query)
183 })
184 })
185β
186 describe('QueryCacheConfig.onSuccess', () => {
187 test('should be called when a query is successful', async () => {
188 const key = queryKey()
189 const onSuccess = jest.fn()
190 const testCache = new QueryCache({ onSuccess })
191 const testClient = createQueryClient({ queryCache: testCache })
192 await testClient.prefetchQuery(key, () => Promise.resolve({ data: 5 }))
193 const query = testCache.find(key)
194 expect(onSuccess).toHaveBeenCalledWith({ data: 5 }, query)
195 })
196 })
197β
198 describe('QueryCache.add', () => {
199 test('should not try to add a query already added to the cache', async () => {
200 const key = queryKey()
201 const hash = `["${key}"]`
202β
203 await queryClient.prefetchQuery(key, () => 'data1')
204β
205 // Directly add the query from the cache
206 // to simulate a race condition
207 const query = queryCache['queriesMap'][hash] as Query
208 const queryClone = Object.assign({}, query)
209β
210 // No error should be thrown when trying to add the query
211 queryCache.add(queryClone)
212 expect(queryCache['queries'].length).toEqual(1)
213β
214 // Clean-up to avoid an error when queryClient.clear()
215 delete queryCache['queriesMap'][hash]
216 })
217β
218 describe('QueryCache.remove', () => {
219 test('should not try to remove a query already removed from the cache', async () => {
220 const key = queryKey()
221 const hash = `["${key}"]`
222β
223 await queryClient.prefetchQuery(key, () => 'data1')
224β
225 // Directly remove the query from the cache
226 // to simulate a race condition
227 const query = queryCache['queriesMap'][hash] as Query
228 const queryClone = Object.assign({}, query)
229 delete queryCache['queriesMap'][hash]
230β
231 // No error should be thrown when trying to remove the query
232 expect(() => queryCache.remove(queryClone)).not.toThrow()
233 })
234 })
235 })
236})