@@ -69,11 +69,11 @@ class TestDatafileManager extends HttpPollingDatafileManager {
69
69
}
70
70
71
71
const testCache : PersistentKeyValueCache = {
72
- get ( key : string ) : Promise < any | null > {
73
- let val = null ;
72
+ get ( key : string ) : Promise < string > {
73
+ let val = '' ;
74
74
switch ( key ) {
75
75
case 'opt-datafile-keyThatExists' :
76
- val = { name : 'keyThatExists' } ;
76
+ val = JSON . stringify ( { name : 'keyThatExists' } ) ;
77
77
break ;
78
78
}
79
79
return Promise . resolve ( val ) ;
@@ -109,11 +109,11 @@ describe('httpPollingDatafileManager', () => {
109
109
110
110
describe ( 'when constructed with sdkKey and datafile and autoUpdate: true,' , ( ) => {
111
111
beforeEach ( ( ) => {
112
- manager = new TestDatafileManager ( { datafile : { foo : 'abcd' } , sdkKey : '123' , autoUpdate : true } ) ;
112
+ manager = new TestDatafileManager ( { datafile : JSON . stringify ( { foo : 'abcd' } ) , sdkKey : '123' , autoUpdate : true } ) ;
113
113
} ) ;
114
114
115
115
it ( 'returns the passed datafile from get' , ( ) => {
116
- expect ( manager . get ( ) ) . toEqual ( { foo : 'abcd' } ) ;
116
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'abcd' } ) ;
117
117
} ) ;
118
118
119
119
it ( 'after being started, fetches the datafile, updates itself, and updates itself again after a timeout' , async ( ) => {
@@ -134,28 +134,30 @@ describe('httpPollingDatafileManager', () => {
134
134
manager . start ( ) ;
135
135
expect ( manager . responsePromises . length ) . toBe ( 1 ) ;
136
136
await manager . responsePromises [ 0 ] ;
137
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
137
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
138
138
updateFn . mockReset ( ) ;
139
139
140
140
await advanceTimersByTime ( 300000 ) ;
141
141
142
142
expect ( manager . responsePromises . length ) . toBe ( 2 ) ;
143
143
await manager . responsePromises [ 1 ] ;
144
144
expect ( updateFn ) . toBeCalledTimes ( 1 ) ;
145
- expect ( updateFn ) . toBeCalledWith ( {
146
- datafile : { fooz : 'barz' } ,
147
- } ) ;
148
- expect ( manager . get ( ) ) . toEqual ( { fooz : 'barz' } ) ;
145
+ expect ( updateFn . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { datafile : '{"fooz": "barz"}' } ) ;
146
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { fooz : 'barz' } ) ;
149
147
} ) ;
150
148
} ) ;
151
149
152
150
describe ( 'when constructed with sdkKey and datafile and autoUpdate: false,' , ( ) => {
153
151
beforeEach ( ( ) => {
154
- manager = new TestDatafileManager ( { datafile : { foo : 'abcd' } , sdkKey : '123' , autoUpdate : false } ) ;
152
+ manager = new TestDatafileManager ( {
153
+ datafile : JSON . stringify ( { foo : 'abcd' } ) ,
154
+ sdkKey : '123' ,
155
+ autoUpdate : false ,
156
+ } ) ;
155
157
} ) ;
156
158
157
159
it ( 'returns the passed datafile from get' , ( ) => {
158
- expect ( manager . get ( ) ) . toEqual ( { foo : 'abcd' } ) ;
160
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'abcd' } ) ;
159
161
} ) ;
160
162
161
163
it ( 'after being started, fetches the datafile, updates itself once, but does not schedule a future update' , async ( ) => {
@@ -167,7 +169,7 @@ describe('httpPollingDatafileManager', () => {
167
169
manager . start ( ) ;
168
170
expect ( manager . responsePromises . length ) . toBe ( 1 ) ;
169
171
await manager . responsePromises [ 0 ] ;
170
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
172
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
171
173
expect ( getTimerCount ( ) ) . toBe ( 0 ) ;
172
174
} ) ;
173
175
} ) ;
@@ -179,7 +181,7 @@ describe('httpPollingDatafileManager', () => {
179
181
180
182
describe ( 'initial state' , ( ) => {
181
183
it ( 'returns null from get before becoming ready' , ( ) => {
182
- expect ( manager . get ( ) ) . toBeNull ( ) ;
184
+ expect ( manager . get ( ) ) . toEqual ( '' ) ;
183
185
} ) ;
184
186
} ) ;
185
187
@@ -205,28 +207,7 @@ describe('httpPollingDatafileManager', () => {
205
207
} ) ;
206
208
manager . start ( ) ;
207
209
await manager . onReady ( ) ;
208
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
209
- } ) ;
210
-
211
- it ( 'does not update if the response body is not valid json' , async ( ) => {
212
- manager . queuedResponses . push (
213
- {
214
- statusCode : 200 ,
215
- body : '{"foo" "' ,
216
- headers : { } ,
217
- } ,
218
- {
219
- statusCode : 200 ,
220
- body : '{"foo": "bar"}' ,
221
- headers : { } ,
222
- }
223
- ) ;
224
- manager . start ( ) ;
225
- await manager . onReady ( ) ;
226
- await advanceTimersByTime ( 1000 ) ;
227
- expect ( manager . responsePromises . length ) . toBe ( 2 ) ;
228
- await manager . responsePromises [ 1 ] ;
229
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
210
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
230
211
} ) ;
231
212
232
213
describe ( 'live updates' , ( ) => {
@@ -275,22 +256,22 @@ describe('httpPollingDatafileManager', () => {
275
256
276
257
manager . start ( ) ;
277
258
await manager . onReady ( ) ;
278
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
259
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
279
260
expect ( updateFn ) . toBeCalledTimes ( 0 ) ;
280
261
281
262
await advanceTimersByTime ( 1000 ) ;
282
263
await manager . responsePromises [ 1 ] ;
283
264
expect ( updateFn ) . toBeCalledTimes ( 1 ) ;
284
- expect ( updateFn . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { datafile : { foo2 : ' bar2' } } ) ;
285
- expect ( manager . get ( ) ) . toEqual ( { foo2 : 'bar2' } ) ;
265
+ expect ( updateFn . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { datafile : '{" foo2": " bar2"}' } ) ;
266
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo2 : 'bar2' } ) ;
286
267
287
268
updateFn . mockReset ( ) ;
288
269
289
270
await advanceTimersByTime ( 1000 ) ;
290
271
await manager . responsePromises [ 2 ] ;
291
272
expect ( updateFn ) . toBeCalledTimes ( 1 ) ;
292
- expect ( updateFn . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { datafile : { foo3 : ' bar3' } } ) ;
293
- expect ( manager . get ( ) ) . toEqual ( { foo3 : 'bar3' } ) ;
273
+ expect ( updateFn . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { datafile : '{" foo3": " bar3"}' } ) ;
274
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo3 : 'bar3' } ) ;
294
275
} ) ;
295
276
296
277
describe ( 'when the update interval time fires before the request is complete' , ( ) => {
@@ -351,15 +332,15 @@ describe('httpPollingDatafileManager', () => {
351
332
352
333
manager . start ( ) ;
353
334
await manager . onReady ( ) ;
354
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
335
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
355
336
356
337
advanceTimersByTime ( 1000 ) ;
357
338
358
339
expect ( manager . responsePromises . length ) . toBe ( 2 ) ;
359
340
manager . stop ( ) ;
360
341
await manager . responsePromises [ 1 ] ;
361
342
// Should not have updated datafile since manager was stopped
362
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
343
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
363
344
} ) ;
364
345
365
346
it ( 'calls abort on the current request if there is a current request when stop is called' , async ( ) => {
@@ -399,7 +380,7 @@ describe('httpPollingDatafileManager', () => {
399
380
// Trigger the update, should fetch the next response which should succeed, then we get ready
400
381
advanceTimersByTime ( 1000 ) ;
401
382
await manager . onReady ( ) ;
402
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
383
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
403
384
} ) ;
404
385
405
386
describe ( 'newness checking' , ( ) => {
@@ -424,7 +405,7 @@ describe('httpPollingDatafileManager', () => {
424
405
425
406
manager . start ( ) ;
426
407
await manager . onReady ( ) ;
427
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
408
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
428
409
// First response promise was for the initial 200 response
429
410
expect ( manager . responsePromises . length ) . toBe ( 1 ) ;
430
411
// Trigger the queued update
@@ -434,7 +415,7 @@ describe('httpPollingDatafileManager', () => {
434
415
await manager . responsePromises [ 1 ] ;
435
416
// Since the response was 304, updateFn should not have been called
436
417
expect ( updateFn ) . toBeCalledTimes ( 0 ) ;
437
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
418
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
438
419
} ) ;
439
420
440
421
it ( 'sends if-modified-since using the last observed response last-modified' , async ( ) => {
@@ -559,7 +540,7 @@ describe('httpPollingDatafileManager', () => {
559
540
} ) ;
560
541
manager . start ( ) ;
561
542
await manager . onReady ( ) ;
562
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
543
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
563
544
} ) ;
564
545
565
546
it ( 'does not schedule a live update after ready' , async ( ) => {
@@ -659,9 +640,9 @@ describe('httpPollingDatafileManager', () => {
659
640
manager . on ( 'update' , updateFn ) ;
660
641
manager . start ( ) ;
661
642
await manager . onReady ( ) ;
662
- expect ( manager . get ( ) ) . toEqual ( { name : 'keyThatExists' } ) ;
643
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { name : 'keyThatExists' } ) ;
663
644
await advanceTimersByTime ( 50 ) ;
664
- expect ( manager . get ( ) ) . toEqual ( { name : 'keyThatExists' } ) ;
645
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { name : 'keyThatExists' } ) ;
665
646
expect ( updateFn ) . toBeCalledTimes ( 0 ) ;
666
647
} ) ;
667
648
@@ -676,10 +657,10 @@ describe('httpPollingDatafileManager', () => {
676
657
manager . on ( 'update' , updateFn ) ;
677
658
manager . start ( ) ;
678
659
await manager . onReady ( ) ;
679
- expect ( manager . get ( ) ) . toEqual ( { name : 'keyThatExists' } ) ;
660
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { name : 'keyThatExists' } ) ;
680
661
expect ( updateFn ) . toBeCalledTimes ( 0 ) ;
681
662
await advanceTimersByTime ( 50 ) ;
682
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
663
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
683
664
expect ( updateFn ) . toBeCalledTimes ( 1 ) ;
684
665
} ) ;
685
666
@@ -693,8 +674,9 @@ describe('httpPollingDatafileManager', () => {
693
674
manager . start ( ) ;
694
675
await manager . onReady ( ) ;
695
676
await advanceTimersByTime ( 50 ) ;
696
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
697
- expect ( cacheSetSpy ) . toBeCalledWith ( 'opt-datafile-keyThatExists' , { foo : 'bar' } ) ;
677
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
678
+ expect ( cacheSetSpy . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'opt-datafile-keyThatExists' ) ;
679
+ expect ( JSON . parse ( cacheSetSpy . mock . calls [ 0 ] [ 1 ] ) ) . toEqual ( { foo : 'bar' } ) ;
698
680
} ) ;
699
681
} ) ;
700
682
@@ -721,7 +703,7 @@ describe('httpPollingDatafileManager', () => {
721
703
manager . start ( ) ;
722
704
await advanceTimersByTime ( 50 ) ;
723
705
await manager . onReady ( ) ;
724
- expect ( manager . get ( ) ) . toEqual ( { foo : 'bar' } ) ;
706
+ expect ( JSON . parse ( manager . get ( ) ) ) . toEqual ( { foo : 'bar' } ) ;
725
707
expect ( updateFn ) . toBeCalledTimes ( 0 ) ;
726
708
} ) ;
727
709
} ) ;
0 commit comments