@@ -5,13 +5,13 @@ describe('cache decorator', () => {
55 let memoizationCheckCount = 0 ;
66 let memoizedCalls = 0 ;
77 let totalFunctionCalls = 0 ;
8-
8+ let bypassCache = false ;
99 class DataService {
1010 @cache ( {
1111 ttl : 3000 ,
1212 onCacheEvent : ( cacheContext ) => {
1313 memoizationCheckCount ++ ;
14- if ( cacheContext . isCached ) {
14+ if ( cacheContext . isCached && ! cacheContext . isBypassed ) {
1515 memoizedCalls ++ ;
1616 }
1717 }
@@ -21,6 +21,21 @@ describe('cache decorator', () => {
2121 return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( `Data for ID: ${ id } ` ) , 100 ) ) ;
2222 }
2323
24+ @cache ( {
25+ ttl : 3000 ,
26+ bypass : ( ) => bypassCache ,
27+ onCacheEvent : ( cacheContext ) => {
28+ memoizationCheckCount ++ ;
29+ if ( cacheContext . isCached && ! cacheContext . isBypassed ) {
30+ memoizedCalls ++ ;
31+ }
32+ }
33+ } )
34+ async fetchDataWithByPassedCacheFunction ( id : number ) : Promise < string > {
35+ totalFunctionCalls ++ ;
36+ return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( `Data for ID: ${ id } ` ) , 100 ) ) ;
37+ }
38+
2439 @cache ( {
2540 ttl : 3000 ,
2641 onCacheEvent : ( cacheContext ) => {
@@ -66,6 +81,32 @@ describe('cache decorator', () => {
6681 expect ( totalFunctionCalls ) . toBe ( 2 ) ; // No extra new calls
6782 expect ( memoizationCheckCount ) . toBe ( 4 ) ; // 4 checks in total
6883
84+ // test NO cache for a Bypassed cache function
85+ memoizationCheckCount = 0 ;
86+ memoizedCalls = 0 ;
87+ totalFunctionCalls = 0 ;
88+ const result21 = await service . fetchDataWithByPassedCacheFunction ( 2 ) ;
89+ expect ( result21 ) . toBe ( 'Data for ID: 2' ) ;
90+ expect ( memoizedCalls ) . toBe ( 0 ) ; // ID 2 result is now memoized
91+ expect ( totalFunctionCalls ) . toBe ( 1 ) ; // extra new call
92+ expect ( memoizationCheckCount ) . toBe ( 1 ) ; // 5 checks in total
93+
94+ bypassCache = false ;
95+ const result22 = await service . fetchDataWithByPassedCacheFunction ( 2 ) ;
96+ expect ( result22 ) . toBe ( 'Data for ID: 2' ) ;
97+ expect ( memoizedCalls ) . toBe ( 1 ) ; // ID 2 result is now memoized
98+ expect ( totalFunctionCalls ) . toBe ( 1 ) ; // NO extra new call
99+ expect ( memoizationCheckCount ) . toBe ( 2 ) ; // 2 checks in total
100+
101+ bypassCache = true ;
102+ const result23 = await service . fetchDataWithByPassedCacheFunction ( 2 ) ;
103+ expect ( result23 ) . toBe ( 'Data for ID: 2' ) ;
104+ expect ( memoizedCalls ) . toBe ( 1 ) ; // ID 2 result is NOT RETRIEVED FROM CACHE AS THEY ARE BYPASSED
105+ expect ( totalFunctionCalls ) . toBe ( 2 ) ; // extra new call as bypassCache = true
106+ expect ( memoizationCheckCount ) . toBe ( 3 ) ; // 5 checks in total
107+
108+
109+
69110 // test NO cache for a throwing async method
70111 memoizationCheckCount = 0 ;
71112 memoizedCalls = 0 ;
0 commit comments