@@ -147,20 +147,24 @@ describe('hooks', () => {
147
147
} ) ;
148
148
149
149
it ( 'should respect the timeout option passed' , async ( ) => {
150
- activateMock . mockReturnValue ( '12345' ) ;
150
+ activateMock . mockReturnValue ( null ) ;
151
151
readySuccess = false ;
152
+
152
153
const component = Enzyme . mount (
153
154
< OptimizelyProvider optimizely = { optimizelyMock } >
154
155
< MyExperimentComponent options = { { timeout : mockDelay } } />
155
156
</ OptimizelyProvider >
156
157
) ;
157
158
expect ( component . text ( ) ) . toBe ( 'null|false|false' ) ; // initial render
159
+
158
160
await optimizelyMock . onReady ( ) ;
159
161
component . update ( ) ;
160
162
expect ( component . text ( ) ) . toBe ( 'null|false|true' ) ; // when didTimeout
161
- readySuccess = true ;
162
- // Simulate CONFIG_UPDATE notification, causing decision state to recompute
163
- notificationListenerCallbacks [ 0 ] ( ) ;
163
+
164
+ // Simulate datafile fetch completing after timeout has already passed
165
+ // Activate now returns a variation
166
+ activateMock . mockReturnValue ( '12345' ) ;
167
+ // Wait for completion of dataReadyPromise
164
168
await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
165
169
component . update ( ) ;
166
170
expect ( component . text ( ) ) . toBe ( '12345|true|true' ) ; // when clientReady
@@ -243,7 +247,7 @@ describe('hooks', () => {
243
247
expect ( mockLog ) . toHaveBeenCalledWith ( '12345' ) ;
244
248
} ) ;
245
249
246
- it ( 'should re-render after the client becomes ready and triggers a config update notification ' , async ( ) => {
250
+ it ( 'should re-render after the client becomes ready' , async ( ) => {
247
251
readySuccess = false ;
248
252
let resolveReadyPromise : ( result : { success : boolean ; dataReadyPromise : Promise < any > } ) => void ;
249
253
const readyPromise : Promise < any > = new Promise ( res => {
@@ -266,10 +270,14 @@ describe('hooks', () => {
266
270
expect ( mockLog ) . toHaveBeenCalledWith ( null ) ;
267
271
268
272
mockLog . mockReset ( ) ;
273
+
274
+ // Simulate datafile fetch completing after timeout has already passed
275
+ // Activate now returns a variation
269
276
activateMock . mockReturnValue ( '12345' ) ;
270
- // Simulate CONFIG_UPDATE notification, causing decision state to recompute
271
- notificationListenerCallbacks [ 0 ] ( ) ;
272
- resolveReadyPromise ! ( { success : true , dataReadyPromise : Promise . resolve ( ) } ) ;
277
+ // Wait for completion of dataReadyPromise
278
+ const dataReadyPromise = Promise . resolve ( ) ;
279
+ resolveReadyPromise ! ( { success : true , dataReadyPromise } ) ;
280
+ await dataReadyPromise ;
273
281
component . update ( ) ;
274
282
275
283
expect ( mockLog ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -380,22 +388,36 @@ describe('hooks', () => {
380
388
} ) ;
381
389
382
390
it ( 'should respect the timeout option passed' , async ( ) => {
383
- isFeatureEnabledMock . mockReturnValue ( true ) ;
391
+ isFeatureEnabledMock . mockReturnValue ( false ) ;
392
+ featureVariables = { } ;
384
393
readySuccess = false ;
394
+
385
395
const component = Enzyme . mount (
386
396
< OptimizelyProvider optimizely = { optimizelyMock } >
387
397
< MyFeatureComponent options = { { timeout : mockDelay } } />
388
398
</ OptimizelyProvider >
389
399
) ;
390
400
expect ( component . text ( ) ) . toBe ( 'false|{}|false|false' ) ; // initial render
401
+
391
402
await optimizelyMock . onReady ( ) ;
392
403
component . update ( ) ;
393
404
expect ( component . text ( ) ) . toBe ( 'false|{}|false|true' ) ; // when didTimeout
394
- readySuccess = true ;
395
- // Simulate CONFIG_UPDATE notification, causing decision state to recompute
396
- notificationListenerCallbacks [ 0 ] ( ) ;
405
+
406
+ // Simulate datafile fetch completing after timeout has already passed
407
+ // isFeatureEnabled now returns true, getFeatureVariables returns variable values
408
+ isFeatureEnabledMock . mockReturnValue ( true ) ;
409
+ featureVariables = mockFeatureVariables ;
410
+ // Wait for completion of dataReadyPromise
397
411
await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
398
412
component . update ( ) ;
413
+
414
+ // Simulate datafile fetch completing after timeout has already passed
415
+ // Activate now returns a variation
416
+ activateMock . mockReturnValue ( '12345' ) ;
417
+ // Wait for completion of dataReadyPromise
418
+ await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
419
+ component . update ( ) ;
420
+
399
421
expect ( component . text ( ) ) . toBe ( 'true|{"foo":"bar"}|true|true' ) ; // when clientReady
400
422
} ) ;
401
423
@@ -480,7 +502,7 @@ describe('hooks', () => {
480
502
expect ( mockLog ) . toHaveBeenCalledWith ( false ) ;
481
503
} ) ;
482
504
483
- it ( 'should re-render after the client becomes ready and triggers a config update notification ' , async ( ) => {
505
+ it ( 'should re-render after the client becomes ready' , async ( ) => {
484
506
readySuccess = false ;
485
507
let resolveReadyPromise : ( result : { success : boolean ; dataReadyPromise : Promise < any > } ) => void ;
486
508
const readyPromise : Promise < any > = new Promise ( res => {
@@ -503,10 +525,14 @@ describe('hooks', () => {
503
525
expect ( mockLog ) . toHaveBeenCalledWith ( false ) ;
504
526
505
527
mockLog . mockReset ( ) ;
528
+
529
+ // Simulate datafile fetch completing after timeout has already passed
530
+ // isFeatureEnabled now returns true
506
531
isFeatureEnabledMock . mockReturnValue ( true ) ;
507
- // Simulate CONFIG_UPDATE notification, causing decision state to recompute
508
- notificationListenerCallbacks [ 0 ] ( ) ;
509
- resolveReadyPromise ! ( { success : true , dataReadyPromise : Promise . resolve ( ) } ) ;
532
+ // Wait for completion of dataReadyPromise
533
+ const dataReadyPromise = Promise . resolve ( ) ;
534
+ resolveReadyPromise ! ( { success : true , dataReadyPromise } ) ;
535
+ await dataReadyPromise ;
510
536
component . update ( ) ;
511
537
512
538
expect ( mockLog ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments