-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.get.test.ts
124 lines (117 loc) · 3.56 KB
/
Adapter.get.test.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import type { CacheInterface } from '@soluble/cache-interop';
import { Guards } from '@soluble/cache-interop';
import { getTestAdapters } from '../setup/getTestAdapters';
const sleep = async (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const adapters = getTestAdapters();
describe.each(adapters)('Adapter: %s', (name, adapterFactory) => {
let cache: CacheInterface;
beforeAll(async () => {
cache = await adapterFactory();
});
afterEach(async () => {
await cache.clear();
});
afterAll(async () => {
if (Guards.isConnectedCache(cache)) {
await cache.getConnection().quit();
}
});
describe('Adapter.get()', () => {
describe('when value is not in cache', () => {
it('should return empty non-error cacheitem', async () => {
expect(await cache.get('k')).toMatchObject({
isSuccess: true,
isHit: false,
error: undefined,
data: null,
metadata: {
key: 'k',
},
});
});
});
describe('when value is in cache', () => {
it('should return cacheitem with data', async () => {
await cache.set('k', 'hello');
expect(await cache.get('k')).toMatchObject({
isSuccess: true,
isHit: true,
data: 'hello',
});
});
});
describe('when a defaultValue is given', () => {
it('should return defaultValue if nothing in cache', async () => {
expect(await cache.get('k', { defaultValue: 'default' })).toMatchObject(
{
isHit: false,
data: 'default',
}
);
});
it('should give priority to existing cache entry', async () => {
await cache.set('k', 'initial');
expect(await cache.get('k', { defaultValue: 'default' })).toMatchObject(
{
isHit: true,
data: 'initial',
}
);
});
});
describe('when an item was set with 0 expiry (forever)', () => {
it('should always return a cache hit', async () => {
await cache.set('k', 'hello world', { ttl: 0 });
expect(await cache.get('k')).toMatchObject({
isHit: true,
data: 'hello world',
});
});
});
describe('when an item was set with 1 second expiry', () => {
it('should always return a cache miss if a second has passed', async () => {
await cache.set('k', 'hello world', { ttl: 1 });
await sleep(1001);
expect(await cache.get('k')).toMatchObject({
isSuccess: true,
isHit: false,
data: null,
});
});
});
describe('when disableCache is true', () => {
describe('when no defaultValue provided', () => {
it('should never return the cache entry', async () => {
await cache.set('k', 'cool');
expect(
await cache.get('k', {
disableCache: true,
})
).toMatchObject({
isSuccess: true,
isHit: false,
data: null,
error: undefined,
});
});
});
describe('when defaultValue value is provided', () => {
it('should never return the default', async () => {
await cache.set('k', 'cool');
expect(
await cache.get('k', {
defaultValue: 'default',
disableCache: true,
})
).toMatchObject({
isSuccess: true,
isHit: false,
data: 'default',
});
});
});
});
});
});