|
| 1 | +import { cache } from './cache.decorator'; |
| 2 | + |
| 3 | +describe('cache decorator', () => { |
| 4 | + it('should cache async function results and prevent redundant calls', async () => { |
| 5 | + let memoizationCheckCount = 0; |
| 6 | + let memoizedCalls = 0; |
| 7 | + let totalFunctionCalls = 0; |
| 8 | + |
| 9 | + class DataService { |
| 10 | + @cache({ |
| 11 | + ttl: 3000, |
| 12 | + onCacheEvent: (cacheContext) => { |
| 13 | + memoizationCheckCount++; |
| 14 | + if (cacheContext.isCached) { |
| 15 | + memoizedCalls++; |
| 16 | + } |
| 17 | + } |
| 18 | + }) |
| 19 | + async fetchData(id: number): Promise<string> { |
| 20 | + totalFunctionCalls++; |
| 21 | + return new Promise((resolve) => setTimeout(() => resolve(`Data for ID: ${id}`), 100)); |
| 22 | + } |
| 23 | + |
| 24 | + @cache({ |
| 25 | + ttl: 3000, |
| 26 | + onCacheEvent: (cacheContext) => { |
| 27 | + memoizationCheckCount++; |
| 28 | + if (cacheContext.isCached) { |
| 29 | + memoizedCalls++; |
| 30 | + } |
| 31 | + } |
| 32 | + }) |
| 33 | + async throwData(name: string): Promise<string> { |
| 34 | + totalFunctionCalls++; |
| 35 | + throw new Error(`hello ${name} but I throw!`); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const service = new DataService(); |
| 40 | + |
| 41 | + memoizationCheckCount = 0; |
| 42 | + memoizedCalls = 0; |
| 43 | + totalFunctionCalls = 0; |
| 44 | + |
| 45 | + const result1 = await service.fetchData(1); |
| 46 | + expect(result1).toBe('Data for ID: 1'); |
| 47 | + expect(memoizedCalls).toBe(0); |
| 48 | + expect(totalFunctionCalls).toBe(1); |
| 49 | + expect(memoizationCheckCount).toBe(1); // Called once |
| 50 | + |
| 51 | + const result2 = await service.fetchData(1); |
| 52 | + expect(result2).toBe('Data for ID: 1'); |
| 53 | + expect(memoizedCalls).toBe(1); // Now it should be memoized |
| 54 | + expect(totalFunctionCalls).toBe(1); // No new calls |
| 55 | + expect(memoizationCheckCount).toBe(2); // Checked twice |
| 56 | + |
| 57 | + const result3 = await service.fetchData(2); |
| 58 | + expect(result3).toBe('Data for ID: 2'); |
| 59 | + expect(memoizedCalls).toBe(1); // No extra memoized calls yet |
| 60 | + expect(totalFunctionCalls).toBe(2); // New call for different ID |
| 61 | + expect(memoizationCheckCount).toBe(3); // Three checks (1st, 2nd for ID 1, and 3rd for ID 2) |
| 62 | + |
| 63 | + const result4 = await service.fetchData(2); |
| 64 | + expect(result4).toBe('Data for ID: 2'); |
| 65 | + expect(memoizedCalls).toBe(2); // ID 2 result is now memoized |
| 66 | + expect(totalFunctionCalls).toBe(2); // No extra new calls |
| 67 | + expect(memoizationCheckCount).toBe(4); // 4 checks in total |
| 68 | + |
| 69 | + // test NO cache for a throwing async method |
| 70 | + memoizationCheckCount = 0; |
| 71 | + memoizedCalls = 0; |
| 72 | + totalFunctionCalls = 0; |
| 73 | + await Promise.all([ |
| 74 | + expect(service.throwData('akram')).rejects.toThrow('hello akram but I throw!'), |
| 75 | + expect(service.throwData('akram')).rejects.toThrow('hello akram but I throw!'), |
| 76 | + expect(service.throwData('akram')).rejects.toThrow('hello akram but I throw!') |
| 77 | + ]); |
| 78 | + expect(memoizationCheckCount).toEqual(totalFunctionCalls + memoizedCalls); |
| 79 | + expect(memoizedCalls).toEqual(0); // No cache |
| 80 | + expect(totalFunctionCalls).toBe(3); // we call everytime we get a throw |
| 81 | + }); |
| 82 | +}); |
0 commit comments