Open History to see the last commit
1import { sleep, queryKey, mockConsoleError } from '../../react/tests/utils'
2import { QueryCache, QueryClient } from '../..'
3β
4describe('queryCache', () => {
5 let queryClient: QueryClient
6 let queryCache: QueryCache
7β
8 beforeEach(() => {
9 queryClient = new QueryClient()
10 queryCache = queryClient.getQueryCache()
11 })
12β
13 afterEach(() => {
14 queryClient.clear()
15 })
16β
17 describe('subscribe', () => {
18 test('should pass the correct query', async () => {
19 const key = queryKey()
20 const subscriber = jest.fn()
21 const unsubscribe = queryCache.subscribe(subscriber)
22 queryClient.setQueryData(key, 'foo')
23 const query = queryCache.find(key)
24 await sleep(1)
25 expect(subscriber).toHaveBeenCalledWith({ query, type: 'queryAdded' })
26 unsubscribe()
27 })
28β
29 test('should notify listeners when new query is added', async () => {
30 const key = queryKey()
31 const callback = jest.fn()
32 queryCache.subscribe(callback)
33 queryClient.prefetchQuery(key, () => 'data')
34 await sleep(100)
35 expect(callback).toHaveBeenCalled()
36 })
37β
38 test('should include the queryCache and query when notifying listeners', async () => {
39 const key = queryKey()
40 const callback = jest.fn()
41 queryCache.subscribe(callback)
42 queryClient.prefetchQuery(key, () => 'data')
43 const query = queryCache.find(key)
44 await sleep(100)
45 expect(callback).toHaveBeenCalledWith({ query, type: 'queryAdded' })
46 })
47β
48 test('should notify subscribers when new query with initialData is added', async () => {
49 const key = queryKey()
50 const callback = jest.fn()
51 queryCache.subscribe(callback)
52 queryClient.prefetchQuery(key, () => 'data', {
53 initialData: 'initial',
54 })
55 await sleep(100)
56 expect(callback).toHaveBeenCalled()
57 })
58 })
59β
60 describe('find', () => {
61 test('find should filter correctly', async () => {
62 const key = queryKey()
63 await queryClient.prefetchQuery(key, () => 'data1')
64 const query = queryCache.find(key)!
65 expect(query).toBeDefined()
66 })
67 })
68β
69 describe('findAll', () => {
70 test('should filter correctly', async () => {
71 const key1 = queryKey()
72 const key2 = queryKey()
73 await queryClient.prefetchQuery(key1, () => 'data1')
74 await queryClient.prefetchQuery(key2, () => 'data2')
75 await queryClient.prefetchQuery([{ a: 'a', b: 'b' }], () => 'data3')
76 await queryClient.prefetchQuery(['posts', 1], () => 'data4')
77 queryClient.invalidateQueries(key2)
78 const query1 = queryCache.find(key1)!
79 const query2 = queryCache.find(key2)!
80 const query3 = queryCache.find([{ a: 'a', b: 'b' }])!
81 const query4 = queryCache.find(['posts', 1])!
82β
83 expect(queryCache.findAll(key1)).toEqual([query1])
84 expect(queryCache.findAll([key1])).toEqual([query1])
85 expect(queryCache.findAll()).toEqual([query1, query2, query3, query4])
86 expect(queryCache.findAll({})).toEqual([query1, query2, query3, query4])
87 expect(queryCache.findAll(key1, { active: false })).toEqual([query1])
88 expect(queryCache.findAll(key1, { active: true })).toEqual([])
89 expect(queryCache.findAll(key1, { stale: true })).toEqual([])
90 expect(queryCache.findAll(key1, { stale: false })).toEqual([query1])
91 expect(queryCache.findAll(key1, { stale: false, active: true })).toEqual(
92 []
93 )
94 expect(
95 queryCache.findAll(key1, { stale: false, active: false })
96 ).toEqual([query1])
97 expect(
98 queryCache.findAll(key1, {
99 stale: false,
100 active: false,
101 exact: true,
102 })
103 ).toEqual([query1])
104β
105 expect(queryCache.findAll(key2)).toEqual([query2])
106 expect(queryCache.findAll(key2, { stale: undefined })).toEqual([query2])
107 expect(queryCache.findAll(key2, { stale: true })).toEqual([query2])
108 expect(queryCache.findAll(key2, { stale: false })).toEqual([])
109 expect(queryCache.findAll([{ b: 'b' }])).toEqual([query3])
110 expect(queryCache.findAll([{ a: 'a' }], { exact: false })).toEqual([
111 query3,
112 ])
113 expect(queryCache.findAll([{ a: 'a' }], { exact: true })).toEqual([])
114 expect(
115 queryCache.findAll([{ a: 'a', b: 'b' }], { exact: true })
116 ).toEqual([query3])
117 expect(queryCache.findAll([{ a: 'a', b: 'b' }])).toEqual([query3])
118 expect(queryCache.findAll([{ a: 'a', b: 'b', c: 'c' }])).toEqual([])
119 expect(queryCache.findAll([{ a: 'a' }], { stale: false })).toEqual([
120 query3,
121 ])
122 expect(queryCache.findAll([{ a: 'a' }], { stale: true })).toEqual([])
123 expect(queryCache.findAll([{ a: 'a' }], { active: true })).toEqual([])
124 expect(queryCache.findAll([{ a: 'a' }], { inactive: true })).toEqual([
125 query3,
126 ])
127 expect(
128 queryCache.findAll({ predicate: query => query === query3 })
129 ).toEqual([query3])
130 expect(queryCache.findAll('posts')).toEqual([query4])
131 })
132 })
133β
134 describe('QueryCacheConfig.onError', () => {
135 test('should be called when a query errors', async () => {
136 const consoleMock = mockConsoleError()
137 const key = queryKey()
138 const onError = jest.fn()
139 const testCache = new QueryCache({ onError })
140 const testClient = new QueryClient({ queryCache: testCache })
141 await testClient.prefetchQuery(key, () => Promise.reject('error'))
142 consoleMock.mockRestore()
143 const query = testCache.find(key)
144 expect(onError).toHaveBeenCalledWith('error', query)
145 })
146 })
147β
148 describe('QueryCacheConfig.onSuccess', () => {
149 test('should be called when a query is successful', async () => {
150 const consoleMock = mockConsoleError()
151 const key = queryKey()
152 const onSuccess = jest.fn()
153 const testCache = new QueryCache({ onSuccess })
154 const testClient = new QueryClient({ queryCache: testCache })
155 await testClient.prefetchQuery(key, () => Promise.resolve({ data: 5 }))
156 consoleMock.mockRestore()
157 const query = testCache.find(key)
158 expect(onSuccess).toHaveBeenCalledWith({ data: 5 }, query)
159 })
160 })
161})