@@ -22,7 +22,8 @@ import {
22
22
openDatabase ,
23
23
APP_NAMESPACE_STORE ,
24
24
IndexedDbStorage ,
25
- InMemoryStorage
25
+ InMemoryStorage ,
26
+ Storage
26
27
} from '../../src/storage/storage' ;
27
28
import { FetchResponse } from '../../src/client/remote_config_fetch_client' ;
28
29
@@ -34,15 +35,15 @@ async function clearDatabase(): Promise<void> {
34
35
. clear ( ) ;
35
36
}
36
37
37
- describe ( 'IndexedDbStorage ' , ( ) => {
38
+ describe ( 'Storage ' , ( ) => {
38
39
39
40
const indexedDbTestCase = {
40
- storage : new IndexedDbStorage ( 'appId' , 'appName' , 'namespace' ) ,
41
+ getStorage : ( ) => new IndexedDbStorage ( 'appId' , 'appName' , 'namespace' ) ,
41
42
name : 'IndexedDbStorage' ,
42
43
} ;
43
44
44
45
const inMemoryStorage = {
45
- storage : new InMemoryStorage ( ) ,
46
+ getStorage : ( ) => new InMemoryStorage ( ) ,
46
47
name : 'InMemoryStorage' ,
47
48
} ;
48
49
@@ -52,135 +53,143 @@ describe('IndexedDbStorage', () => {
52
53
53
54
it ( `${ indexedDbTestCase . name } constructs a composite key` , async ( ) => {
54
55
// This is defensive, but the cost of accidentally changing the key composition is high.
55
- expect ( indexedDbTestCase . storage . createCompositeKey ( 'throttle_metadata' ) ) . to . eq (
56
+ expect ( indexedDbTestCase . getStorage ( ) . createCompositeKey ( 'throttle_metadata' ) ) . to . eq (
56
57
'appId,appName,namespace,throttle_metadata'
57
58
) ;
58
59
} ) ;
59
60
60
- for ( const { name, storage } of [ indexedDbTestCase , inMemoryStorage ] ) {
61
- it ( ` ${ name } sets and gets last fetch attempt status` , async ( ) => {
62
- const expectedStatus = 'success' ;
61
+ for ( const { name, getStorage } of [ indexedDbTestCase , inMemoryStorage ] ) {
62
+ describe ( name , ( ) => {
63
+ let storage : Storage ;
63
64
64
- await storage . setLastFetchStatus ( expectedStatus ) ;
65
+ beforeEach ( ( ) => {
66
+ storage = getStorage ( ) ;
67
+ } ) ;
65
68
66
- const actualStatus = await storage . getLastFetchStatus ( ) ;
69
+ it ( `sets and gets last fetch attempt status` , async ( ) => {
70
+ const expectedStatus = 'success' ;
67
71
68
- expect ( actualStatus ) . to . deep . eq ( expectedStatus ) ;
69
- } ) ;
72
+ await storage . setLastFetchStatus ( expectedStatus ) ;
70
73
71
- it ( `${ name } sets and gets last fetch success timestamp` , async ( ) => {
72
- const lastSuccessfulFetchTimestampMillis = 123 ;
74
+ const actualStatus = await storage . getLastFetchStatus ( ) ;
73
75
74
- await storage . setLastSuccessfulFetchTimestampMillis (
75
- lastSuccessfulFetchTimestampMillis
76
- ) ;
76
+ expect ( actualStatus ) . to . deep . eq ( expectedStatus ) ;
77
+ } ) ;
77
78
78
- const actualMetadata =
79
- await storage . getLastSuccessfulFetchTimestampMillis ( ) ;
79
+ it ( `sets and gets last fetch success timestamp` , async ( ) => {
80
+ const lastSuccessfulFetchTimestampMillis = 123 ;
80
81
81
- expect ( actualMetadata ) . to . deep . eq ( lastSuccessfulFetchTimestampMillis ) ;
82
- } ) ;
82
+ await storage . setLastSuccessfulFetchTimestampMillis (
83
+ lastSuccessfulFetchTimestampMillis
84
+ ) ;
83
85
84
- it ( ` ${ name } sets and gets last successful fetch response` , async ( ) => {
85
- const lastSuccessfulFetchResponse = { status : 200 } as FetchResponse ;
86
+ const actualMetadata =
87
+ await storage . getLastSuccessfulFetchTimestampMillis ( ) ;
86
88
87
- await storage . setLastSuccessfulFetchResponse ( lastSuccessfulFetchResponse ) ;
89
+ expect ( actualMetadata ) . to . deep . eq ( lastSuccessfulFetchTimestampMillis ) ;
90
+ } ) ;
88
91
89
- const actualConfig = await storage . getLastSuccessfulFetchResponse ( ) ;
92
+ it ( `sets and gets last successful fetch response` , async ( ) => {
93
+ const lastSuccessfulFetchResponse = { status : 200 } as FetchResponse ;
90
94
91
- expect ( actualConfig ) . to . deep . eq ( lastSuccessfulFetchResponse ) ;
92
- } ) ;
95
+ await storage . setLastSuccessfulFetchResponse ( lastSuccessfulFetchResponse ) ;
93
96
94
- it ( `${ name } sets and gets active config` , async ( ) => {
95
- const expectedConfig = { key : 'value' } ;
97
+ const actualConfig = await storage . getLastSuccessfulFetchResponse ( ) ;
96
98
97
- await storage . setActiveConfig ( expectedConfig ) ;
99
+ expect ( actualConfig ) . to . deep . eq ( lastSuccessfulFetchResponse ) ;
100
+ } ) ;
98
101
99
- const storedConfig = await storage . getActiveConfig ( ) ;
102
+ it ( `sets and gets active config` , async ( ) => {
103
+ const expectedConfig = { key : 'value' } ;
100
104
101
- expect ( storedConfig ) . to . deep . eq ( expectedConfig ) ;
102
- } ) ;
105
+ await storage . setActiveConfig ( expectedConfig ) ;
103
106
104
- it ( `${ name } sets and gets active config etag` , async ( ) => {
105
- const expectedEtag = 'etag' ;
107
+ const storedConfig = await storage . getActiveConfig ( ) ;
106
108
107
- await storage . setActiveConfigEtag ( expectedEtag ) ;
109
+ expect ( storedConfig ) . to . deep . eq ( expectedConfig ) ;
110
+ } ) ;
108
111
109
- const storedConfigEtag = await storage . getActiveConfigEtag ( ) ;
112
+ it ( `sets and gets active config etag` , async ( ) => {
113
+ const expectedEtag = 'etag' ;
110
114
111
- expect ( storedConfigEtag ) . to . deep . eq ( expectedEtag ) ;
112
- } ) ;
115
+ await storage . setActiveConfigEtag ( expectedEtag ) ;
113
116
114
- it ( `${ name } sets, gets and deletes throttle metadata` , async ( ) => {
115
- const expectedMetadata = {
116
- throttleEndTimeMillis : 1
117
- } as ThrottleMetadata ;
117
+ const storedConfigEtag = await storage . getActiveConfigEtag ( ) ;
118
118
119
- await storage . setThrottleMetadata ( expectedMetadata ) ;
119
+ expect ( storedConfigEtag ) . to . deep . eq ( expectedEtag ) ;
120
+ } ) ;
120
121
121
- let actualMetadata = await storage . getThrottleMetadata ( ) ;
122
+ it ( `sets, gets and deletes throttle metadata` , async ( ) => {
123
+ const expectedMetadata = {
124
+ throttleEndTimeMillis : 1
125
+ } as ThrottleMetadata ;
122
126
123
- expect ( actualMetadata ) . to . deep . eq ( expectedMetadata ) ;
127
+ await storage . setThrottleMetadata ( expectedMetadata ) ;
124
128
125
- await storage . deleteThrottleMetadata ( ) ;
129
+ let actualMetadata = await storage . getThrottleMetadata ( ) ;
126
130
127
- actualMetadata = await storage . getThrottleMetadata ( ) ;
131
+ expect ( actualMetadata ) . to . deep . eq ( expectedMetadata ) ;
128
132
129
- expect ( actualMetadata ) . to . be . undefined ;
130
- } ) ;
133
+ await storage . deleteThrottleMetadata ( ) ;
131
134
132
- it ( 'sets and gets custom signals' , async ( ) => {
133
- const customSignals = { key : 'value' , key1 : 'value1' , key2 : 1 } ;
134
- const customSignalsInStorage = {
135
- key : 'value' ,
136
- key1 : 'value1' ,
137
- key2 : '1'
138
- } ;
135
+ actualMetadata = await storage . getThrottleMetadata ( ) ;
139
136
140
- await storage . setCustomSignals ( customSignals ) ;
137
+ expect ( actualMetadata ) . to . be . undefined ;
138
+ } ) ;
141
139
142
- const storedCustomSignals = await storage . getCustomSignals ( ) ;
140
+ it ( `sets and gets custom signals` , async ( ) => {
141
+ const customSignals = { key : 'value' , key1 : 'value1' , key2 : 1 } ;
142
+ const customSignalsInStorage = {
143
+ key : 'value' ,
144
+ key1 : 'value1' ,
145
+ key2 : '1'
146
+ } ;
143
147
144
- expect ( storedCustomSignals ) . to . deep . eq ( customSignalsInStorage ) ;
145
- } ) ;
148
+ await storage . setCustomSignals ( customSignals ) ;
146
149
147
- it ( 'upserts custom signals when key is present in storage' , async ( ) => {
148
- const customSignals = { key : 'value' , key1 : 'value1' } ;
149
- const updatedSignals = { key : 'value' , key1 : 'value2' } ;
150
+ const storedCustomSignals = await storage . getCustomSignals ( ) ;
150
151
151
- await storage . setCustomSignals ( customSignals ) ;
152
+ expect ( storedCustomSignals ) . to . deep . eq ( customSignalsInStorage ) ;
153
+ } ) ;
152
154
153
- await storage . setCustomSignals ( { key1 : 'value2' } ) ;
155
+ it ( `upserts custom signals when key is present in storage` , async ( ) => {
156
+ const customSignals = { key : 'value' , key1 : 'value1' } ;
157
+ const updatedSignals = { key : 'value' , key1 : 'value2' } ;
154
158
155
- const storedCustomSignals = await storage . getCustomSignals ( ) ;
159
+ await storage . setCustomSignals ( customSignals ) ;
156
160
157
- expect ( storedCustomSignals ) . to . deep . eq ( updatedSignals ) ;
158
- } ) ;
161
+ await storage . setCustomSignals ( { key1 : 'value2' } ) ;
159
162
160
- it ( 'deletes custom signal when value supplied is null' , async ( ) => {
161
- const customSignals = { key : 'value' , key1 : 'value1' } ;
162
- const updatedSignals = { key : 'value' } ;
163
+ const storedCustomSignals = await storage . getCustomSignals ( ) ;
163
164
164
- await storage . setCustomSignals ( customSignals ) ;
165
+ expect ( storedCustomSignals ) . to . deep . eq ( updatedSignals ) ;
166
+ } ) ;
165
167
166
- await storage . setCustomSignals ( { key1 : null } ) ;
168
+ it ( `deletes custom signal when value supplied is null` , async ( ) => {
169
+ const customSignals = { key : 'value' , key1 : 'value1' } ;
170
+ const updatedSignals = { key : 'value' } ;
167
171
168
- const storedCustomSignals = await storage . getCustomSignals ( ) ;
172
+ await storage . setCustomSignals ( customSignals ) ;
169
173
170
- expect ( storedCustomSignals ) . to . deep . eq ( updatedSignals ) ;
171
- } ) ;
174
+ await storage . setCustomSignals ( { key1 : null } ) ;
175
+
176
+ const storedCustomSignals = await storage . getCustomSignals ( ) ;
177
+
178
+ expect ( storedCustomSignals ) . to . deep . eq ( updatedSignals ) ;
179
+ } ) ;
180
+
181
+ it ( `throws an error when supplied with excess custom signals` , async ( ) => {
182
+ const customSignals : { [ key : string ] : string } = { } ;
183
+ for ( let i = 0 ; i < 101 ; i ++ ) {
184
+ customSignals [ `key${ i } ` ] = `value${ i } ` ;
185
+ }
172
186
173
- it ( 'throws an error when supplied with excess custom signals' , async ( ) => {
174
- const customSignals : { [ key : string ] : string } = { } ;
175
- for ( let i = 0 ; i < 101 ; i ++ ) {
176
- customSignals [ `key${ i } ` ] = `value${ i } ` ;
177
- }
178
-
179
- await expect (
180
- storage . setCustomSignals ( customSignals )
181
- ) . to . eventually . be . rejectedWith (
182
- 'Remote Config: Setting more than 100 custom signals is not supported.'
183
- ) ;
187
+ await expect (
188
+ storage . setCustomSignals ( customSignals )
189
+ ) . to . eventually . be . rejectedWith (
190
+ 'Remote Config: Setting more than 100 custom signals is not supported.'
191
+ ) ;
192
+ } ) ;
184
193
} ) ;
185
194
}
186
195
} ) ;
0 commit comments