@@ -6,16 +6,17 @@ import { SplitsCacheInLocal } from '../SplitsCacheInLocal';
6
6
import { nearlyEqual } from '../../../__tests__/testUtils' ;
7
7
import { MySegmentsCacheInLocal } from '../MySegmentsCacheInLocal' ;
8
8
import { RBSegmentsCacheInLocal } from '../RBSegmentsCacheInLocal' ;
9
+ import { storages , PREFIX } from './wrapper.mock' ;
9
10
10
11
const FULL_SETTINGS_HASH = 'dc1f9817' ;
11
12
12
- describe ( 'validateCache' , ( ) => {
13
- const keys = new KeyBuilderCS ( 'SPLITIO' , 'user' ) ;
13
+ describe . each ( storages ) ( 'validateCache' , ( storage ) => {
14
+ const keys = new KeyBuilderCS ( PREFIX , 'user' ) ;
14
15
const logSpy = jest . spyOn ( fullSettings . log , 'info' ) ;
15
- const segments = new MySegmentsCacheInLocal ( fullSettings . log , keys , localStorage ) ;
16
- const largeSegments = new MySegmentsCacheInLocal ( fullSettings . log , keys , localStorage ) ;
17
- const splits = new SplitsCacheInLocal ( fullSettings , keys , localStorage ) ;
18
- const rbSegments = new RBSegmentsCacheInLocal ( fullSettings , keys , localStorage ) ;
16
+ const segments = new MySegmentsCacheInLocal ( fullSettings . log , keys , storage ) ;
17
+ const largeSegments = new MySegmentsCacheInLocal ( fullSettings . log , keys , storage ) ;
18
+ const splits = new SplitsCacheInLocal ( fullSettings , keys , storage ) ;
19
+ const rbSegments = new RBSegmentsCacheInLocal ( fullSettings , keys , storage ) ;
19
20
20
21
jest . spyOn ( splits , 'getChangeNumber' ) ;
21
22
jest . spyOn ( splits , 'clear' ) ;
@@ -25,11 +26,11 @@ describe('validateCache', () => {
25
26
26
27
beforeEach ( ( ) => {
27
28
jest . clearAllMocks ( ) ;
28
- localStorage . clear ( ) ;
29
+ for ( let i = 0 ; i < storage . length ; i ++ ) storage . removeItem ( storage . key ( i ) as string ) ;
29
30
} ) ;
30
31
31
32
test ( 'if there is no cache, it should return false' , async ( ) => {
32
- expect ( await validateCache ( { } , localStorage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
33
+ expect ( await validateCache ( { } , storage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
33
34
34
35
expect ( logSpy ) . not . toHaveBeenCalled ( ) ;
35
36
@@ -39,15 +40,15 @@ describe('validateCache', () => {
39
40
expect ( largeSegments . clear ) . not . toHaveBeenCalled ( ) ;
40
41
expect ( splits . getChangeNumber ) . toHaveBeenCalledTimes ( 1 ) ;
41
42
42
- expect ( localStorage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( FULL_SETTINGS_HASH ) ;
43
- expect ( localStorage . getItem ( keys . buildLastClear ( ) ) ) . toBeNull ( ) ;
43
+ expect ( storage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( FULL_SETTINGS_HASH ) ;
44
+ expect ( storage . getItem ( keys . buildLastClear ( ) ) ) . toBeNull ( ) ;
44
45
} ) ;
45
46
46
47
test ( 'if there is cache and it must not be cleared, it should return true' , async ( ) => {
47
- localStorage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
48
- localStorage . setItem ( keys . buildHashKey ( ) , FULL_SETTINGS_HASH ) ;
48
+ storage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
49
+ storage . setItem ( keys . buildHashKey ( ) , FULL_SETTINGS_HASH ) ;
49
50
50
- expect ( await validateCache ( { } , localStorage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( true ) ;
51
+ expect ( await validateCache ( { } , storage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( true ) ;
51
52
52
53
expect ( logSpy ) . not . toHaveBeenCalled ( ) ;
53
54
@@ -57,16 +58,16 @@ describe('validateCache', () => {
57
58
expect ( largeSegments . clear ) . not . toHaveBeenCalled ( ) ;
58
59
expect ( splits . getChangeNumber ) . toHaveBeenCalledTimes ( 1 ) ;
59
60
60
- expect ( localStorage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( FULL_SETTINGS_HASH ) ;
61
- expect ( localStorage . getItem ( keys . buildLastClear ( ) ) ) . toBeNull ( ) ;
61
+ expect ( storage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( FULL_SETTINGS_HASH ) ;
62
+ expect ( storage . getItem ( keys . buildLastClear ( ) ) ) . toBeNull ( ) ;
62
63
} ) ;
63
64
64
65
test ( 'if there is cache and it has expired, it should clear cache and return false' , async ( ) => {
65
- localStorage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
66
- localStorage . setItem ( keys . buildHashKey ( ) , FULL_SETTINGS_HASH ) ;
67
- localStorage . setItem ( keys . buildLastUpdatedKey ( ) , Date . now ( ) - 1000 * 60 * 60 * 24 * 2 + '' ) ; // 2 days ago
66
+ storage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
67
+ storage . setItem ( keys . buildHashKey ( ) , FULL_SETTINGS_HASH ) ;
68
+ storage . setItem ( keys . buildLastUpdatedKey ( ) , Date . now ( ) - 1000 * 60 * 60 * 24 * 2 + '' ) ; // 2 days ago
68
69
69
- expect ( await validateCache ( { expirationDays : 1 } , localStorage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
70
+ expect ( await validateCache ( { expirationDays : 1 } , storage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
70
71
71
72
expect ( logSpy ) . toHaveBeenCalledWith ( 'storage:localstorage: Cache expired more than 1 days ago. Cleaning up cache' ) ;
72
73
@@ -75,15 +76,15 @@ describe('validateCache', () => {
75
76
expect ( segments . clear ) . toHaveBeenCalledTimes ( 1 ) ;
76
77
expect ( largeSegments . clear ) . toHaveBeenCalledTimes ( 1 ) ;
77
78
78
- expect ( localStorage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( FULL_SETTINGS_HASH ) ;
79
- expect ( nearlyEqual ( parseInt ( localStorage . getItem ( keys . buildLastClear ( ) ) as string ) , Date . now ( ) ) ) . toBe ( true ) ;
79
+ expect ( storage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( FULL_SETTINGS_HASH ) ;
80
+ expect ( nearlyEqual ( parseInt ( storage . getItem ( keys . buildLastClear ( ) ) as string ) , Date . now ( ) ) ) . toBe ( true ) ;
80
81
} ) ;
81
82
82
83
test ( 'if there is cache and its hash has changed, it should clear cache and return false' , async ( ) => {
83
- localStorage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
84
- localStorage . setItem ( keys . buildHashKey ( ) , FULL_SETTINGS_HASH ) ;
84
+ storage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
85
+ storage . setItem ( keys . buildHashKey ( ) , FULL_SETTINGS_HASH ) ;
85
86
86
- expect ( await validateCache ( { } , localStorage , { ...fullSettings , core : { ...fullSettings . core , authorizationKey : 'another-sdk-key' } } , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
87
+ expect ( await validateCache ( { } , storage , { ...fullSettings , core : { ...fullSettings . core , authorizationKey : 'another-sdk-key' } } , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
87
88
88
89
expect ( logSpy ) . toHaveBeenCalledWith ( 'storage:localstorage: SDK key, flags filter criteria, or flags spec version has changed. Cleaning up cache' ) ;
89
90
@@ -92,16 +93,16 @@ describe('validateCache', () => {
92
93
expect ( segments . clear ) . toHaveBeenCalledTimes ( 1 ) ;
93
94
expect ( largeSegments . clear ) . toHaveBeenCalledTimes ( 1 ) ;
94
95
95
- expect ( localStorage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( '45c6ba5d' ) ;
96
- expect ( nearlyEqual ( parseInt ( localStorage . getItem ( keys . buildLastClear ( ) ) as string ) , Date . now ( ) ) ) . toBe ( true ) ;
96
+ expect ( storage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( '45c6ba5d' ) ;
97
+ expect ( nearlyEqual ( parseInt ( storage . getItem ( keys . buildLastClear ( ) ) as string ) , Date . now ( ) ) ) . toBe ( true ) ;
97
98
} ) ;
98
99
99
100
test ( 'if there is cache and clearOnInit is true, it should clear cache and return false' , async ( ) => {
100
101
// Older cache version (without last clear)
101
- localStorage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
102
- localStorage . setItem ( keys . buildHashKey ( ) , FULL_SETTINGS_HASH ) ;
102
+ storage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
103
+ storage . setItem ( keys . buildHashKey ( ) , FULL_SETTINGS_HASH ) ;
103
104
104
- expect ( await validateCache ( { clearOnInit : true } , localStorage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
105
+ expect ( await validateCache ( { clearOnInit : true } , storage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
105
106
106
107
expect ( logSpy ) . toHaveBeenCalledWith ( 'storage:localstorage: clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache' ) ;
107
108
@@ -110,25 +111,25 @@ describe('validateCache', () => {
110
111
expect ( segments . clear ) . toHaveBeenCalledTimes ( 1 ) ;
111
112
expect ( largeSegments . clear ) . toHaveBeenCalledTimes ( 1 ) ;
112
113
113
- expect ( localStorage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( FULL_SETTINGS_HASH ) ;
114
- const lastClear = localStorage . getItem ( keys . buildLastClear ( ) ) ;
114
+ expect ( storage . getItem ( keys . buildHashKey ( ) ) ) . toBe ( FULL_SETTINGS_HASH ) ;
115
+ const lastClear = storage . getItem ( keys . buildLastClear ( ) ) ;
115
116
expect ( nearlyEqual ( parseInt ( lastClear as string ) , Date . now ( ) ) ) . toBe ( true ) ;
116
117
117
118
// If cache is cleared, it should not clear again until a day has passed
118
119
logSpy . mockClear ( ) ;
119
- localStorage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
120
- expect ( await validateCache ( { clearOnInit : true } , localStorage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( true ) ;
120
+ storage . setItem ( keys . buildSplitsTillKey ( ) , '1' ) ;
121
+ expect ( await validateCache ( { clearOnInit : true } , storage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( true ) ;
121
122
expect ( logSpy ) . not . toHaveBeenCalled ( ) ;
122
- expect ( localStorage . getItem ( keys . buildLastClear ( ) ) ) . toBe ( lastClear ) ; // Last clear should not have changed
123
+ expect ( storage . getItem ( keys . buildLastClear ( ) ) ) . toBe ( lastClear ) ; // Last clear should not have changed
123
124
124
125
// If a day has passed, it should clear again
125
- localStorage . setItem ( keys . buildLastClear ( ) , ( Date . now ( ) - 1000 * 60 * 60 * 24 - 1 ) + '' ) ;
126
- expect ( await validateCache ( { clearOnInit : true } , localStorage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
126
+ storage . setItem ( keys . buildLastClear ( ) , ( Date . now ( ) - 1000 * 60 * 60 * 24 - 1 ) + '' ) ;
127
+ expect ( await validateCache ( { clearOnInit : true } , storage , fullSettings , keys , splits , rbSegments , segments , largeSegments ) ) . toBe ( false ) ;
127
128
expect ( logSpy ) . toHaveBeenCalledWith ( 'storage:localstorage: clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache' ) ;
128
129
expect ( splits . clear ) . toHaveBeenCalledTimes ( 2 ) ;
129
130
expect ( rbSegments . clear ) . toHaveBeenCalledTimes ( 2 ) ;
130
131
expect ( segments . clear ) . toHaveBeenCalledTimes ( 2 ) ;
131
132
expect ( largeSegments . clear ) . toHaveBeenCalledTimes ( 2 ) ;
132
- expect ( nearlyEqual ( parseInt ( localStorage . getItem ( keys . buildLastClear ( ) ) as string ) , Date . now ( ) ) ) . toBe ( true ) ;
133
+ expect ( nearlyEqual ( parseInt ( storage . getItem ( keys . buildLastClear ( ) ) as string ) , Date . now ( ) ) ) . toBe ( true ) ;
133
134
} ) ;
134
135
} ) ;
0 commit comments