-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathaxios-cache-interceptor.test.js
More file actions
52 lines (46 loc) · 2.12 KB
/
axios-cache-interceptor.test.js
File metadata and controls
52 lines (46 loc) · 2.12 KB
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
47
48
49
50
51
52
require('dotenv').config()
const Axios = require('axios')
const { setupCache } = require('axios-cache-interceptor')
// 1. we test that the configuration we set is valid and available to check
describe('test Axios configuration', () => {
test('tests argument composition', () => {
const axios = Axios.create()
const withAxios = setupCache(axios)
// we expect axios to be properly configured with setupCache
expect(withAxios).not.toBeUndefined()
// we expect the cache to be properly configured and contain our properties after initialization
const withConfig = setupCache(axios, { ttl: 1234 })
expect(withConfig).not.toBeUndefined()
expect(withConfig.defaults.cache.ttl).toBe(1234)
})
})
// 2. we test that the cache is working as expected with concurrent requests
describe('test Axios cache with two requests', () => {
test('tests cache', async () => {
const axios = Axios.create()
const cache_interceptor = setupCache(axios)
// we make two sequential requests to the same api endpoint
const req1 = cache_interceptor.get('https://github.com/OpenSourceFellows/amplify')
const req2 = cache_interceptor.get('https://github.com/OpenSourceFellows/amplify/')
const res1 = await req1
const res2 = await req2
// assertions: we expect the second response to be cached
expect(res1.cached).toBe(false)
expect(res2.cached).toBe(true)
})
})
// 3. we test that the cache is invalidated when the ttl expires
describe('test Axios cache invalidation option', () => {
test('tests cache invalidation', async () => {
// we set the ttl to 10 seconds
const axios = Axios.create()
const cache_interceptor = setupCache(axios, { ttl: 10000 })
// make a simple request
await cache_interceptor.get('https://github.com/OpenSourceFellows/amplify')
// wait for 11 seconds and make another call
await new Promise((resolve) => setTimeout(() => resolve(), 11000))
const res3 = await cache_interceptor.get('https://github.com/OpenSourceFellows/amplify')
// assertions: the second request is expected not to be cached, due to the ttl
expect(res3.cached).toBe(false)
}, 70000)
})