@@ -4,8 +4,13 @@ describe('ChromeStore', () => {
4
4
const mockEntries : Record < string , string > = { key1 : 'value1' , key2 : 'value2' } ;
5
5
let chromeStore : ChromeStorageAsyncStore < string > ;
6
6
let extendedStorageLocal : chrome . storage . StorageArea ;
7
+ let now : number ;
7
8
8
9
beforeEach ( ( ) => {
10
+ now = Date . now ( ) ;
11
+
12
+ jest . useFakeTimers ( ) ;
13
+
9
14
const get = jest . fn ( ) ;
10
15
const set = jest . fn ( ) ;
11
16
extendedStorageLocal = {
@@ -30,20 +35,49 @@ describe('ChromeStore', () => {
30
35
} ) ;
31
36
32
37
afterEach ( ( ) => {
38
+ jest . useRealTimers ( ) ;
33
39
jest . clearAllMocks ( ) ;
34
40
} ) ;
35
41
36
- it ( 'is always expired' , async ( ) => {
42
+ it ( 'is always expired without cooldown' , async ( ) => {
43
+ chromeStore = new ChromeStorageAsyncStore ( extendedStorageLocal , undefined ) ;
37
44
expect ( await chromeStore . isExpired ( ) ) . toBe ( true ) ;
38
45
} ) ;
39
46
40
- it ( 'should return empty object when no entries are found' , async ( ) => {
47
+ it ( 'is not expired with cooldown' , async ( ) => {
48
+ chromeStore = new ChromeStorageAsyncStore ( extendedStorageLocal , 10 ) ;
49
+
41
50
( extendedStorageLocal . get as jest . Mock ) . mockImplementation ( ( ) => {
42
- return Promise . resolve ( { } ) ;
51
+ return Promise . resolve ( {
52
+ [ 'eppo-configuration' ] : JSON . stringify ( mockEntries ) ,
53
+ [ 'eppo-configuration-meta' ] : JSON . stringify ( {
54
+ lastUpdatedAtMs : new Date ( ) . getTime ( ) ,
55
+ } ) ,
56
+ } ) ;
43
57
} ) ;
44
58
45
- const entries = await chromeStore . getEntries ( ) ;
46
- expect ( entries ) . toEqual ( { } ) ;
59
+ expect ( await chromeStore . isExpired ( ) ) . toBe ( false ) ;
60
+ } ) ;
61
+
62
+ it ( 'is expired after cooldown' , async ( ) => {
63
+ chromeStore = new ChromeStorageAsyncStore ( extendedStorageLocal , 10 ) ;
64
+
65
+ ( extendedStorageLocal . get as jest . Mock ) . mockImplementation ( ( ) => {
66
+ return Promise . resolve ( {
67
+ [ 'eppo-configuration' ] : JSON . stringify ( mockEntries ) ,
68
+ [ 'eppo-configuration-meta' ] : JSON . stringify ( {
69
+ lastUpdatedAtMs : now ,
70
+ } ) ,
71
+ } ) ;
72
+ } ) ;
73
+
74
+ // advance time by 5 seconds
75
+ await jest . advanceTimersByTimeAsync ( 5 * 1000 ) ;
76
+ expect ( await chromeStore . isExpired ( ) ) . toBe ( false ) ;
77
+
78
+ // advance time by 6 seconds
79
+ await jest . advanceTimersByTimeAsync ( 6 * 1000 ) ;
80
+ expect ( await chromeStore . isExpired ( ) ) . toBe ( true ) ;
47
81
} ) ;
48
82
49
83
it ( 'should be initialized after setting entries' , async ( ) => {
0 commit comments