-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.InvalidCacheKey.test.ts
73 lines (64 loc) · 2.2 KB
/
Error.InvalidCacheKey.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
import type { CacheInterface } from '@soluble/cache-interop';
import {
InvalidCacheKeyException,
CacheItem,
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('When the cacheKey is invalid / unsupported format', () => {
const invalidKey = { errKey: true } as unknown as string;
describe('Adapter.delete(invalidKey)', () => {
it('should return InvalidCacheKeyException', async () => {
await expect(cache.delete(invalidKey)).resolves.toBeInstanceOf(
InvalidCacheKeyException
);
});
});
describe('Adapter.set(invalidKey)', () => {
it('should return InvalidCacheKeyException', async () => {
await expect(cache.set(invalidKey, 'cool')).resolves.toBeInstanceOf(
InvalidCacheKeyException
);
});
});
describe('Adapter.get(invalidKey)', () => {
it('should return error with InvalidCacheKeyException', async () => {
const item = await cache.get(invalidKey);
expect(item).toBeInstanceOf(CacheItem);
expect(item.error).toBeInstanceOf(InvalidCacheKeyException);
});
});
describe('Adapter.getOrSet(invalidKey)', () => {
it('should return InvalidCacheKeyException', async () => {
const item = await cache.getOrSet(invalidKey, 'hello');
expect(item).toBeInstanceOf(CacheItem);
expect(item.error).toBeInstanceOf(InvalidCacheKeyException);
});
});
describe('Adapter.has(invalidKey)', () => {
it('should call onError and return an InvalidCacheKeyException', async () => {
let error: Error | null = null;
await cache.has(invalidKey, {
onError: (err) => {
error = err;
},
});
expect(error).toBeInstanceOf(InvalidCacheKeyException);
});
});
});
});