@@ -22,6 +22,7 @@ import { getMockAsyncCache } from '../tests/mock/mock_cache';
22
22
import { isVuid } from './vuid' ;
23
23
import { resolvablePromise } from '../utils/promise/resolvablePromise' ;
24
24
import { exhaustMicrotasks } from '../tests/testUtils' ;
25
+ import { get } from 'http' ;
25
26
26
27
const vuidCacheKey = 'optimizely-vuid' ;
27
28
@@ -76,7 +77,7 @@ describe('VuidCacheManager', () => {
76
77
const vuid1 = await manager . load ( ) ;
77
78
const vuid2 = await manager . load ( ) ;
78
79
expect ( vuid1 ) . toBe ( 'vuid_valid' ) ;
79
- expect ( vuid1 ) . toBe ( 'vuid_valid' ) ;
80
+ expect ( vuid2 ) . toBe ( 'vuid_valid' ) ;
80
81
const vuidInCache = await cache . get ( vuidCacheKey ) ;
81
82
expect ( vuidInCache ) . toBe ( 'vuid_valid' ) ;
82
83
} ) ;
@@ -96,6 +97,10 @@ describe('VuidCacheManager', () => {
96
97
await manager . load ( ) ;
97
98
const vuid2 = await cache2 . get ( vuidCacheKey ) ;
98
99
expect ( vuid2 ) . toBe ( 'vuid_456' ) ;
100
+
101
+ await manager . remove ( ) ;
102
+ const vuidInCache = await cache2 . get ( vuidCacheKey ) ;
103
+ expect ( vuidInCache ) . toBeUndefined ( ) ;
99
104
} ) ;
100
105
101
106
it ( 'should sequence remove and load calls' , async ( ) => {
@@ -151,28 +156,43 @@ describe('VuidCacheManager', () => {
151
156
} ) ;
152
157
153
158
describe ( 'DefaultVuidManager' , ( ) => {
154
- it ( 'should return undefined for getVuid () before initialization' , async ( ) => {
155
- const vuidCacheManager = {
156
- remove : vi . fn ( ) ,
157
- load : vi . fn ( ) ,
158
- } as unknown as VuidCacheManager ;
159
+ const getMockCacheManager = ( ) => ( {
160
+ remove : vi . fn ( ) ,
161
+ load : vi . fn ( ) ,
162
+ setCache : vi . fn ( ) ,
163
+ } ) ;
159
164
165
+ it ( 'should return undefined for getVuid() before initialization' , async ( ) => {
160
166
const manager = new DefaultVuidManager ( {
161
- vuidCacheManager,
167
+ vuidCache : getMockAsyncCache < string > ( ) ,
168
+ vuidCacheManager : getMockCacheManager ( ) as unknown as VuidCacheManager ,
162
169
enableVuid : true
163
170
} ) ;
164
171
165
172
expect ( manager . getVuid ( ) ) . toBeUndefined ( ) ;
166
173
} ) ;
167
174
175
+ it ( 'should set the cache on vuidCacheManager' , async ( ) => {
176
+ const vuidCacheManager = getMockCacheManager ( ) ;
177
+
178
+ const cache = getMockAsyncCache < string > ( ) ;
179
+
180
+ const manager = new DefaultVuidManager ( {
181
+ vuidCache : cache ,
182
+ vuidCacheManager : vuidCacheManager as unknown as VuidCacheManager ,
183
+ enableVuid : false
184
+ } ) ;
185
+
186
+ await manager . initialize ( ) ;
187
+ expect ( vuidCacheManager . setCache ) . toHaveBeenCalledWith ( cache ) ;
188
+ } ) ;
189
+
168
190
it ( 'should call remove on VuidCacheManager if enableVuid is false' , async ( ) => {
169
- const vuidCacheManager = {
170
- remove : vi . fn ( ) ,
171
- load : vi . fn ( ) ,
172
- } as unknown as VuidCacheManager ;
191
+ const vuidCacheManager = getMockCacheManager ( ) ;
173
192
174
193
const manager = new DefaultVuidManager ( {
175
- vuidCacheManager,
194
+ vuidCache : getMockAsyncCache < string > ( ) ,
195
+ vuidCacheManager : vuidCacheManager as unknown as VuidCacheManager ,
176
196
enableVuid : false
177
197
} ) ;
178
198
@@ -181,13 +201,11 @@ describe('DefaultVuidManager', () => {
181
201
} ) ;
182
202
183
203
it ( 'should return undefined for getVuid() after initialization if enableVuid is false' , async ( ) => {
184
- const vuidCacheManager = {
185
- remove : vi . fn ( ) ,
186
- load : vi . fn ( ) ,
187
- } as unknown as VuidCacheManager ;
204
+ const vuidCacheManager = getMockCacheManager ( ) ;
188
205
189
206
const manager = new DefaultVuidManager ( {
190
- vuidCacheManager,
207
+ vuidCache : getMockAsyncCache < string > ( ) ,
208
+ vuidCacheManager : vuidCacheManager as unknown as VuidCacheManager ,
191
209
enableVuid : false
192
210
} ) ;
193
211
@@ -196,17 +214,12 @@ describe('DefaultVuidManager', () => {
196
214
} ) ;
197
215
198
216
it ( 'should load vuid using VuidCacheManger if enableVuid=true' , async ( ) => {
199
- const load = vi . fn ( ) ;
200
-
201
- const vuidCacheManager = {
202
- remove : vi . fn ( ) ,
203
- load,
204
- } as unknown as VuidCacheManager ;
205
-
206
- load . mockResolvedValue ( 'vuid_valid' ) ;
217
+ const vuidCacheManager = getMockCacheManager ( ) ;
218
+ vuidCacheManager . load . mockResolvedValue ( 'vuid_valid' ) ;
207
219
208
220
const manager = new DefaultVuidManager ( {
209
- vuidCacheManager,
221
+ vuidCache : getMockAsyncCache < string > ( ) ,
222
+ vuidCacheManager : vuidCacheManager as unknown as VuidCacheManager ,
210
223
enableVuid : true
211
224
} ) ;
212
225
0 commit comments