-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.set.test.ts
119 lines (111 loc) · 4.03 KB
/
Adapter.set.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
import type { CacheInterface } from '@soluble/cache-interop';
import {
CacheException,
CacheProviderException,
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.set()', () => {
describe('when setting a string value', () => {
it('should return true', async () => {
expect(await cache.set('k', 'cool')).toStrictEqual(true);
});
it('should persist the value', async () => {
await cache.set('k2', 'cool2');
expect((await cache.get('k2')).data).toStrictEqual('cool2');
});
});
describe('when setting a null value', () => {
it('should return true', async () => {
expect(await cache.set('k', null)).toStrictEqual(true);
});
it('should persist the value', async () => {
await cache.set('k2', null);
expect((await cache.get('k2')).data).toBeNull();
});
});
describe('when disableCache is true', () => {
it('should not persist entry and return false', async () => {
expect(
await cache.set('k', 'cool', {
disableCache: true,
})
).toStrictEqual(false);
expect((await cache.get('k')).data).toStrictEqual(null);
});
it('should not execute the function provider', async () => {
const fct = jest.fn((_) => 'cool');
await cache.set('k', 'cool', {
disableCache: true,
});
expect(fct).toHaveBeenCalledTimes(0);
});
});
describe('when value is a function returning a string', () => {
it('should call the function and return true', async () => {
const fct = jest.fn((_) => 'cool');
expect(await cache.set('k', fct)).toStrictEqual(true);
expect(fct).toHaveBeenCalledTimes(1);
});
it('should persist the value', async () => {
const fct = jest.fn((_) => 'cool');
await cache.set('k2', fct);
expect((await cache.get('k2')).data).toStrictEqual('cool');
});
});
describe('when value is a function returning null', () => {
it('should call the function and return true', async () => {
const fct = jest.fn((_) => 'cool');
expect(await cache.set('k', fct)).toStrictEqual(true);
expect(fct).toHaveBeenCalledTimes(1);
});
it('should persist the value', async () => {
const fct = jest.fn((_) => null);
await cache.set('k2', fct);
expect((await cache.get('k2')).data).toBeNull();
});
});
describe('when function throws', () => {
it('should return a CacheProviderException when function throws', async () => {
const fct = jest.fn((_) => {
throw new Error('error');
});
const ret = await cache.set('k', fct);
expect(fct).toHaveBeenCalledTimes(1);
expect(ret).toBeInstanceOf(CacheProviderException);
// expect((ret as CacheProviderException).message).toMatch('[]');
});
});
describe('when value is a native async function', () => {
it('should call the function with params and return true', async () => {
const fct = jest.fn(async (_) => 'native');
expect(await cache.set('k', fct)).toStrictEqual(true);
expect(fct).toHaveBeenCalledTimes(1);
});
});
describe('when value is an async function that throws', () => {
it('should return a CacheProviderException when promise fails', async () => {
const fct = jest.fn(async (_) => {
throw new Error('fetch-error');
});
const ret = await cache.set('k', fct);
expect(fct).toHaveBeenCalledTimes(1);
expect(ret).toBeInstanceOf(CacheException);
});
});
});
});