-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.has.test.ts
67 lines (62 loc) · 2.06 KB
/
Adapter.has.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
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.has()', () => {
describe('when value is not in cache', () => {
it('should return false', async () => {
expect(await cache.has('not_exist')).toStrictEqual(false);
});
});
describe('when value is in cache', () => {
it('should return true', async () => {
await cache.set('k', 'cool');
expect(await cache.has('k')).toStrictEqual(true);
});
});
describe('when an item was set with 0 expiry (forever)', () => {
it('should always return true', async () => {
await cache.set('k', 'hello world', { ttl: 0 });
expect(await cache.has('k')).toStrictEqual(true);
});
});
describe('when an item was set with 1 second expiry', () => {
it('should return false if a second has passed', async () => {
await cache.set('k', 'hello world', { ttl: 1 });
await sleep(1_005);
expect(await cache.has('k')).toStrictEqual(false);
});
});
describe('when disableCache is set to true', () => {
it('should always return false whether the item exists or not', async () => {
expect(
await cache.has('k', {
disableCache: true,
})
).toStrictEqual(false);
await cache.set('k', 'hello world');
expect(
await cache.has('k', {
disableCache: true,
})
).toStrictEqual(false);
});
});
});
});