Open History to see the last commit
1import { makeQueryCache } from '../index'
2import { sleep } from './utils'
3β
4describe('queryCache', () => {
5 test('setQueryData does not crash if query could not be found', () => {
6 const queryCache = makeQueryCache()
7 expect(() =>
8 queryCache.setQueryData(['USER', { userId: 1 }], prevUser => ({
9 ...prevUser,
10 name: 'Edvin',
11 }))
12 ).not.toThrow()
13 })
14β
15 test('setQueryData does not crash when variable is null', () => {
16 const queryCache = makeQueryCache()
17 queryCache.setQueryData(['USER', { userId: null }], 'Old Data')
18β
19 expect(() =>
20 queryCache.setQueryData(['USER', { userId: null }], 'New Data')
21 ).not.toThrow()
22 })
23β
24 test('prefetchQuery returns the cached data on cache hits', async () => {
25 const queryCache = makeQueryCache()
26 const fetchFn = () => Promise.resolve('data')
27 const first = await queryCache.prefetchQuery('key', fetchFn)
28 const second = await queryCache.prefetchQuery('key', fetchFn)
29β
30 expect(second).toBe(first)
31 })
32β
33 test('prefetchQuery should force fetch', async () => {
34 const queryCache = makeQueryCache()
35 const fetchFn = () => Promise.resolve('fresh')
36 const first = await queryCache.prefetchQuery('key', fetchFn, {
37 initialData: 'initial',
38 force: true,
39 })
40β
41 expect(first).toBe('fresh')
42 })
43β
44 test('prefetchQuery should throw error when throwOnError is true', async () => {
45 const queryCache = makeQueryCache()
46 const fetchFn = () =>
47 new Promise(() => {
48 throw new Error('error')
49 })
50β
51 await expect(
52 queryCache.prefetchQuery('key', undefined, fetchFn, {
53 retry: false,
54 throwOnError: true,
55 })
56 ).rejects.toThrow(new Error('error'))
57 })
58β
59 test('should notify listeners when new query is added', () => {
60 const queryCache = makeQueryCache()
61 const callback = jest.fn()
62β
63 queryCache.subscribe(callback)
64β
65 queryCache.prefetchQuery('test', () => 'data')
66β
67 expect(callback).toHaveBeenCalled()
68 })
69β
70 test('should notify subsribers when new query with initialData is added', () => {
71 const queryCache = makeQueryCache()
72 const callback = jest.fn()
73β
74 queryCache.subscribe(callback)
75β
76 queryCache.prefetchQuery('test', () => {}, { initialData: 'initial' })
77β
78 expect(callback).toHaveBeenCalled()
79 })
80β
81 test('setQueryData creates a new query if query was not found, using exact', () => {
82 const queryCache = makeQueryCache()
83 queryCache.setQueryData('foo', 'bar', { exact: true })
84β
85 expect(queryCache.getQueryData('foo')).toBe('bar')
86 })
87β
88 test('setQueryData creates a new query if query was not found', () => {
89 const queryCache = makeQueryCache()
90 queryCache.setQueryData('baz', 'qux')
91β
92 expect(queryCache.getQueryData('baz')).toBe('qux')
93 })
94β
95 test('removeQueries does not crash when exact is provided', async () => {
96 const queryCache = makeQueryCache()
97 const fetchFn = () => Promise.resolve('data')
98β
99 // check the query was added to the cache
100 await queryCache.prefetchQuery('key', fetchFn)
101 expect(queryCache.getQuery('key')).toBeTruthy()
102β
103 // check the error doesn't occur
104 expect(() => queryCache.removeQueries('key', { exact: true })).not.toThrow()
105β
106 // check query was successful removed
107 expect(queryCache.getQuery('key')).toBeFalsy()
108 })
109β
110 test('setQueryData schedules stale timeouts appropriately', async () => {
111 const queryCache = makeQueryCache()
112 queryCache.setQueryData('key', 'test data', { staleTime: 100 })
113β
114 expect(queryCache.getQuery('key').state.data).toEqual('test data')
115 expect(queryCache.getQuery('key').state.isStale).toEqual(false)
116β
117 await sleep(50)
118β
119 expect(queryCache.getQuery('key').state.isStale).toEqual(false)
120β
121 await sleep(100)
122β
123 expect(queryCache.getQuery('key').state.isStale).toEqual(true)
124 })
125β
126 test('setQueryData updater function works as expected', () => {
127 const queryCache = makeQueryCache()
128 const updater = jest.fn(oldData => `new data + ${oldData}`)
129β
130 queryCache.setQueryData('updater', 'test data')
131 queryCache.setQueryData('updater', updater)
132β
133 expect(updater).toHaveBeenCalled()
134 expect(queryCache.getQuery('updater').state.data).toEqual(
135 'new data + test data'
136 )
137 })
138β
139 test('getQueries should return queries that partially match queryKey', async () => {
140 const queryCache = makeQueryCache()
141 const fetchData1 = () => Promise.resolve('data1')
142 const fetchData2 = () => Promise.resolve('data2')
143 const fetchDifferentData = () => Promise.resolve('data3')
144 await queryCache.prefetchQuery(['data', { page: 1 }], fetchData1)
145 await queryCache.prefetchQuery(['data', { page: 2 }], fetchData2)
146 await queryCache.prefetchQuery(['differentData'], fetchDifferentData)
147 const queries = queryCache.getQueries('data')
148 const data = queries.map(query => query.state.data)
149 expect(data).toEqual(['data1', 'data2'])
150 })
151β
152 test('stale timeout dispatch is not called if query is no longer in the query cache', async () => {
153 const queryCache = makeQueryCache()
154 const queryKey = 'key'
155 const fetchData = () => Promise.resolve('data')
156 await queryCache.prefetchQuery(queryKey, fetchData)
157 const query = queryCache.getQuery(queryKey)
158 expect(query.state.isStale).toBe(false)
159 queryCache.removeQueries(queryKey)
160 await sleep(50)
161 expect(query.state.isStale).toBe(false)
162 })
163β
164 test('query is garbage collected when unsubscribed to', async () => {
165 const queryCache = makeQueryCache()
166 const queryKey = 'key'
167 const fetchData = () => Promise.resolve('data')
168 await queryCache.prefetchQuery(queryKey, fetchData, { cacheTime: 0 })
169 const query = queryCache.getQuery(queryKey)
170 expect(query.state.markedForGarbageCollection).toBe(false)
171 const unsubscribe = query.subscribe(1)
172 unsubscribe()
173 expect(query.state.markedForGarbageCollection).toBe(true)
174 await sleep(1)
175 expect(queryCache.getQuery(queryKey)).toBeUndefined()
176 })
177β
178 test('query is not garbage collected unless markedForGarbageCollection is true', async () => {
179 const queryCache = makeQueryCache()
180 const queryKey = 'key'
181 const fetchData = () => Promise.resolve(undefined)
182 await queryCache.prefetchQuery(queryKey, fetchData, { cacheTime: 0 })
183 const query = queryCache.getQuery(queryKey)
184 expect(query.state.markedForGarbageCollection).toBe(false)
185 const unsubscribe = query.subscribe(1)
186 unsubscribe()
187 expect(query.state.markedForGarbageCollection).toBe(true)
188 queryCache.clear()
189 queryCache.setQueryData(queryKey, 'data')
190 await sleep(1)
191 const newQuery = queryCache.getQuery(queryKey)
192 expect(newQuery.state.markedForGarbageCollection).toBe(false)
193 expect(newQuery.state.data).toBe('data')
194 })
195β
196 // this covers bug https://github.com/tannerlinsley/react-query/issues/639
197 test('setQueryData of not instatiated query uses a resolved promise as queryFn', async () => {
198 let promiseIsResolved = false
199 const queryCache = makeQueryCache()
200 const queryKey = 'notInstantiated'
201 queryCache.setQueryData(queryKey, "data")
202 await queryCache.refetchQueries(queryKey, { force: true });
203 promiseIsResolved = true
204 expect(promiseIsResolved).toBe(true)
205 })
206})