Open History to see the last commit
1import { sleep, queryKey, mockConsoleError } from '../../react/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 = new QueryClient()
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: 'queryAdded' })
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: 'queryAdded' })
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 await queryClient.prefetchQuery(key1, () => 'data1')
82 await queryClient.prefetchQuery(key2, () => 'data2')
83 await queryClient.prefetchQuery([{ a: 'a', b: 'b' }], () => 'data3')
84 await queryClient.prefetchQuery(['posts', 1], () => 'data4')
85 queryClient.invalidateQueries(key2)
86 const query1 = queryCache.find(key1)!
87 const query2 = queryCache.find(key2)!
88 const query3 = queryCache.find([{ a: 'a', b: 'b' }])!
89 const query4 = queryCache.find(['posts', 1])!
90β
91 expect(queryCache.findAll(key1)).toEqual([query1])
92 expect(queryCache.findAll([key1])).toEqual([query1])
93 expect(queryCache.findAll()).toEqual([query1, query2, query3, query4])
94 expect(queryCache.findAll({})).toEqual([query1, query2, query3, query4])
95 expect(queryCache.findAll(key1, { active: false })).toEqual([query1])
96 expect(queryCache.findAll(key1, { active: true })).toEqual([])
97 expect(queryCache.findAll(key1, { stale: true })).toEqual([])
98 expect(queryCache.findAll(key1, { stale: false })).toEqual([query1])
99 expect(queryCache.findAll(key1, { stale: false, active: true })).toEqual(
100 []
101 )
102 expect(
103 queryCache.findAll(key1, { stale: false, active: false })
104 ).toEqual([query1])
105 expect(
106 queryCache.findAll(key1, {
107 stale: false,
108 active: false,
109 exact: true,
110 })
111 ).toEqual([query1])
112β
113 expect(queryCache.findAll(key2)).toEqual([query2])
114 expect(queryCache.findAll(key2, { stale: undefined })).toEqual([query2])
115 expect(queryCache.findAll(key2, { stale: true })).toEqual([query2])
116 expect(queryCache.findAll(key2, { stale: false })).toEqual([])
117 expect(queryCache.findAll([{ b: 'b' }])).toEqual([query3])
118 expect(queryCache.findAll([{ a: 'a' }], { exact: false })).toEqual([
119 query3,
120 ])
121 expect(queryCache.findAll([{ a: 'a' }], { exact: true })).toEqual([])
122 expect(
123 queryCache.findAll([{ a: 'a', b: 'b' }], { exact: true })
124 ).toEqual([query3])
125 expect(queryCache.findAll([{ a: 'a', b: 'b' }])).toEqual([query3])
126 expect(queryCache.findAll([{ a: 'a', b: 'b', c: 'c' }])).toEqual([])
127 expect(queryCache.findAll([{ a: 'a' }], { stale: false })).toEqual([
128 query3,
129 ])
130 expect(queryCache.findAll([{ a: 'a' }], { stale: true })).toEqual([])
131 expect(queryCache.findAll([{ a: 'a' }], { active: true })).toEqual([])
132 expect(queryCache.findAll([{ a: 'a' }], { inactive: true })).toEqual([
133 query3,
134 ])
135 expect(
136 queryCache.findAll({ predicate: query => query === query3 })
137 ).toEqual([query3])
138 expect(queryCache.findAll('posts')).toEqual([query4])
139 })
140β
141 test('should return all the queries when no filters are defined', async () => {
142 const key1 = queryKey()
143 const key2 = queryKey()
144 await queryClient.prefetchQuery(key1, () => 'data1')
145 await queryClient.prefetchQuery(key2, () => 'data2')
146 expect(queryCache.findAll().length).toBe(2)
147 })
148 })
149β
150 describe('QueryCacheConfig.onError', () => {
151 test('should be called when a query errors', async () => {
152 const consoleMock = mockConsoleError()
153 const key = queryKey()
154 const onError = jest.fn()
155 const testCache = new QueryCache({ onError })
156 const testClient = new QueryClient({ queryCache: testCache })
157 await testClient.prefetchQuery(key, () => Promise.reject('error'))
158 consoleMock.mockRestore()
159 const query = testCache.find(key)
160 expect(onError).toHaveBeenCalledWith('error', query)
161 })
162 })
163β
164 describe('QueryCacheConfig.onSuccess', () => {
165 test('should be called when a query is successful', async () => {
166 const consoleMock = mockConsoleError()
167 const key = queryKey()
168 const onSuccess = jest.fn()
169 const testCache = new QueryCache({ onSuccess })
170 const testClient = new QueryClient({ queryCache: testCache })
171 await testClient.prefetchQuery(key, () => Promise.resolve({ data: 5 }))
172 consoleMock.mockRestore()
173 const query = testCache.find(key)
174 expect(onSuccess).toHaveBeenCalledWith({ data: 5 }, query)
175 })
176 })
177β
178 describe('QueryCache.add', () => {
179 test('should not try to add a query already added to the cache', async () => {
180 const key = queryKey()
181 const hash = `["${key}"]`
182β
183 await queryClient.prefetchQuery(key, () => 'data1')
184β
185 // Directly add the query from the cache
186 // to simulate a race condition
187 const query = queryCache['queriesMap'][hash] as Query
188 const queryClone = Object.assign({}, query)
189β
190 // No error should be thrown when trying to add the query
191 queryCache.add(queryClone)
192 expect(queryCache['queries'].length).toEqual(1)
193β
194 // Clean-up to avoid an error when queryClient.clear()
195 delete queryCache['queriesMap'][hash]
196 })
197β
198 describe('QueryCache.remove', () => {
199 test('should not try to remove a query already removed from the cache', async () => {
200 const key = queryKey()
201 const hash = `["${key}"]`
202β
203 await queryClient.prefetchQuery(key, () => 'data1')
204β
205 // Directly remove 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 delete queryCache['queriesMap'][hash]
210β
211 // No error should be thrown when trying to remove the query
212 expect(() => queryCache.remove(queryClone)).not.toThrow()
213 })
214 })
215 })
216})