-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinMemoryCache.test.ts
More file actions
117 lines (98 loc) · 3.83 KB
/
inMemoryCache.test.ts
File metadata and controls
117 lines (98 loc) · 3.83 KB
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
import { CacheFactory } from "./../src/factories/CacheFactory";
import { Cacheable } from "./../src/interfaces/Cache";
import { TestDatabaseCacheFactory } from "../__mocks__/DatabaseFactory";
import { InMemoryCacheable } from "../src/adapters/InMemoryCacheable";
describe("MemoryCacheable", () => {
let cache: Cacheable;
beforeEach(() => {
cache = CacheFactory.createCache();
});
it("should set and get a value", () => {
cache.set("key", "value", 1000);
expect(cache.get("key")).toBe("value");
});
it("should return null if the key does not exist", () => {
expect(cache.get("key")).toBeNull();
});
it("should return null if the key is expired", (done) => {
cache.set("key", "value", 100);
// Wait for the value to expire by advancing the time
setTimeout(() => {
const value = cache.get<string>("key");
expect(value).toBeNull();
done();
}, 400);
});
it("should remember a value", () => {
const callback = jest.fn(() => "value");
expect(cache.remember("key", 1000, callback)).toBe("value");
expect(callback).toHaveBeenCalledTimes(1);
expect(cache.get("key")).toBe("value");
});
it("should not call the callback if the value is cached", () => {
const callback = jest.fn(() => "value");
cache.set("key", "value", 1000);
expect(cache.remember("key", 1000, callback)).toBe("value");
expect(callback).not.toHaveBeenCalled();
});
it("should invalidate a value", () => {
cache.set("key", "value", 1000);
cache.invalidate("key");
expect(cache.get("key")).toBeNull();
});
it("should clear the cache", () => {
cache.set("key", "value", 1000);
cache.clear();
expect(cache.get("key")).toBe(null);
});
it("should remember the cached value and cache it again if expired", (done) => {
const callback = jest.fn(() => "value");
cache.set("key", "value", 100);
expect(cache.remember("key", 1000, callback)).toBe("value");
expect(callback).not.toHaveBeenCalled();
setTimeout(() => {
expect(cache.remember("key", 1000, callback)).toBe("value");
expect(callback).toHaveBeenCalledTimes(1);
done();
}, 200);
});
it("should remove the value if the ttl is less than or equal to 0", () => {
cache.set("key", "value", 1000);
expect(cache.get("key")).toBe("value");
cache.set("key", "value", 0);
expect(cache.get("key")).toBeNull();
cache.set("key", "value", 1000);
expect(cache.get("key")).toBe("value");
cache.set("key", "value", -1);
expect(cache.get("key")).toBeNull();
});
it("should evaluate and cache the value if it is a function", () => {
const callback = jest.fn(() => "value");
cache.set("key", callback, 1000);
expect(cache.get("key")).toBe("value");
expect(callback).toHaveBeenCalledTimes(1);
});
it("should create a cache instance with a custom strategy", () => {
const cacheStrategy = new TestDatabaseCacheFactory();
const cache = CacheFactory.createCache({
cacheStrategy: cacheStrategy,
});
expect(cache).toBeInstanceOf(TestDatabaseCacheFactory);
});
it("should use a cache instance", () => {
const cacheStrategy = new TestDatabaseCacheFactory();
const cache = CacheFactory.use(cacheStrategy);
expect(cache).toBeInstanceOf(TestDatabaseCacheFactory);
});
it("should check if a key exists in the cache", () => {
cache.set("key", "value", 1000);
expect(cache.has("key")).toBe(true);
expect(cache.has("non-existent-key")).toBe(false);
});
it("should remember a value forever", () => {
const callback = jest.fn(() => "value");
expect(cache.rememberForever("key", callback)).toBe("value");
expect(callback).toHaveBeenCalledTimes(1);
expect(cache.get("key")).toBe("value");
});
});