-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.decorator.spec.ts
More file actions
82 lines (72 loc) · 2.85 KB
/
cache.decorator.spec.ts
File metadata and controls
82 lines (72 loc) · 2.85 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
import { cache } from './cache.decorator';
describe('cache decorator', () => {
it('should cache async function results and prevent redundant calls', async () => {
let memoizationCheckCount = 0;
let memoizedCalls = 0;
let totalFunctionCalls = 0;
class DataService {
@cache({
ttl: 3000,
onCacheEvent: (cacheContext) => {
memoizationCheckCount++;
if (cacheContext.isCached) {
memoizedCalls++;
}
}
})
async fetchData(id: number): Promise<string> {
totalFunctionCalls++;
return new Promise((resolve) => setTimeout(() => resolve(`Data for ID: ${id}`), 100));
}
@cache({
ttl: 3000,
onCacheEvent: (cacheContext) => {
memoizationCheckCount++;
if (cacheContext.isCached) {
memoizedCalls++;
}
}
})
async throwData(name: string): Promise<string> {
totalFunctionCalls++;
throw new Error(`hello ${name} but I throw!`);
}
}
const service = new DataService();
memoizationCheckCount = 0;
memoizedCalls = 0;
totalFunctionCalls = 0;
const result1 = await service.fetchData(1);
expect(result1).toBe('Data for ID: 1');
expect(memoizedCalls).toBe(0);
expect(totalFunctionCalls).toBe(1);
expect(memoizationCheckCount).toBe(1); // Called once
const result2 = await service.fetchData(1);
expect(result2).toBe('Data for ID: 1');
expect(memoizedCalls).toBe(1); // Now it should be memoized
expect(totalFunctionCalls).toBe(1); // No new calls
expect(memoizationCheckCount).toBe(2); // Checked twice
const result3 = await service.fetchData(2);
expect(result3).toBe('Data for ID: 2');
expect(memoizedCalls).toBe(1); // No extra memoized calls yet
expect(totalFunctionCalls).toBe(2); // New call for different ID
expect(memoizationCheckCount).toBe(3); // Three checks (1st, 2nd for ID 1, and 3rd for ID 2)
const result4 = await service.fetchData(2);
expect(result4).toBe('Data for ID: 2');
expect(memoizedCalls).toBe(2); // ID 2 result is now memoized
expect(totalFunctionCalls).toBe(2); // No extra new calls
expect(memoizationCheckCount).toBe(4); // 4 checks in total
// test NO cache for a throwing async method
memoizationCheckCount = 0;
memoizedCalls = 0;
totalFunctionCalls = 0;
await Promise.all([
expect(service.throwData('akram')).rejects.toThrow('hello akram but I throw!'),
expect(service.throwData('akram')).rejects.toThrow('hello akram but I throw!'),
expect(service.throwData('akram')).rejects.toThrow('hello akram but I throw!')
]);
expect(memoizationCheckCount).toEqual(totalFunctionCalls + memoizedCalls);
expect(memoizedCalls).toEqual(0); // No cache
expect(totalFunctionCalls).toBe(3); // we call everytime we get a throw
});
});