-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.getOrSet.test.ts
75 lines (70 loc) · 2.38 KB
/
Adapter.getOrSet.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
import type { CacheInterface } from '@soluble/cache-interop';
import { Guards } from '@soluble/cache-interop';
import { getTestAdapters } from '../setup/getTestAdapters';
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.getOrSet()', () => {
describe('when key is not in cache', () => {
it('should execute the function and persist its return value', async () => {
const fct = jest.fn(async (_) => 'hello');
expect(await cache.getOrSet('k', fct)).toMatchObject({
isSuccess: true,
isHit: false,
isPersisted: true,
data: 'hello',
error: undefined,
metadata: {
key: 'k',
},
});
expect(fct).toHaveBeenCalledTimes(1);
expect((await cache.get('k')).data).toStrictEqual('hello');
});
});
describe('when key is already in cache', () => {
it('should not execute the function provider and return the entry', async () => {
const fct = jest.fn(async (_) => 'value_from_promise');
await cache.set('k', 'initial_value');
expect(await cache.getOrSet('k', fct)).toMatchObject({
isHit: true,
isPersisted: null,
data: 'initial_value',
});
expect(fct).toHaveBeenCalledTimes(0);
});
});
describe('when disableCache is set to true (read/write)', () => {
describe('when a cache entry exists', () => {
it('should execute the fn but ignore cache in read / write', async () => {
const fct = jest.fn(async (_) => 'from_promise');
await cache.set('k', 'initial_value');
expect(
await cache.getOrSet('k', fct, {
disableCache: true,
})
).toMatchObject({
isSuccess: true,
isHit: false,
isPersisted: false,
data: 'from_promise',
error: undefined,
});
expect(fct).toHaveBeenCalledTimes(1);
expect((await cache.get('k')).data).toStrictEqual('initial_value');
});
});
});
});
});