@@ -85,6 +85,25 @@ describe('wrap', () => {
8585 expect ( await cache . wrap ( data . key , async ( ) => 5 , undefined , 500 ) ) . toEqual ( 4 ) ;
8686 } ) ;
8787
88+ it ( 'should allow refreshThreshold function on wrap function' , async ( ) => {
89+ const config = { ttl : ( v :number ) => v * 1000 , refreshThreshold : ( v :number ) => v * 500 } ;
90+
91+ // 1st call should be cached
92+ expect ( await cache . wrap ( data . key , async ( ) => 1 , config . ttl , config . refreshThreshold ) ) . toEqual ( 1 ) ;
93+ await sleep ( 501 ) ;
94+ // Background refresh, but stale value returned
95+ expect ( await cache . wrap ( data . key , async ( ) => 2 , config . ttl , config . refreshThreshold ) ) . toEqual ( 1 ) ;
96+ // New value in cache
97+ expect ( await cache . wrap ( data . key , async ( ) => 2 , config . ttl , config . refreshThreshold ) ) . toEqual ( 2 ) ;
98+ await sleep ( 1001 ) ;
99+ // No background refresh with the new override params
100+ expect ( await cache . wrap ( data . key , async ( ) => 3 , undefined , 500 ) ) . toEqual ( 2 ) ;
101+ await sleep ( 500 ) ;
102+ // Background refresh, but stale value returned
103+ expect ( await cache . wrap ( data . key , async ( ) => 4 , undefined , 500 ) ) . toEqual ( 2 ) ;
104+ expect ( await cache . wrap ( data . key , async ( ) => 5 , undefined , 500 ) ) . toEqual ( 4 ) ;
105+ } ) ;
106+
88107 it ( 'should support nested calls of other caches - no mutual state' , async ( ) => {
89108 const getValueA = vi . fn ( ( ) => 'A' ) ;
90109 const getValueB = vi . fn ( ( ) => 'B' ) ;
0 commit comments