-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachedApi.test.js
46 lines (43 loc) · 1.29 KB
/
cachedApi.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const cachedApi = require('./cachedApi')
const mockRedisStorage = {}
// Mock for redis, to test caching
const redisGet = jest.fn().mockImplementation(
key =>
new Promise((resolve, reject) => {
if (mockRedisStorage[key]) {
resolve(mockRedisStorage[key])
}
reject(new Error('Not found'))
})
)
const redisSet = jest.fn().mockImplementation(
(key, value) =>
new Promise(resolve => {
mockRedisStorage[key] = { cached: value }
resolve('OK')
})
)
const options = {
https: false,
host: 'localhost',
port: '3210',
path: '/api/test/apitest',
cache: {
get: redisGet,
set: redisSet,
},
}
const testCachedApi = cachedApi(options)
describe('cachedApi calls works as expected', () => {
it('should return second call from cache when calling promisedGetText and promisedApiCall', async () => {
const apiData = await testCachedApi.promisedApiCall()
expect(apiData).toBe('*/*')
const cachedApiData = await testCachedApi.promisedApiCall()
expect(cachedApiData.cached).toBe('*/*')
delete mockRedisStorage['/api/test/apitest']
const data = await testCachedApi.promisedGetText()
expect(data).toBe('text/plain')
const cachedData = await testCachedApi.promisedGetText()
expect(cachedData.cached).toBe('text/plain')
})
})