@@ -147,20 +147,24 @@ describe('hooks', () => {
147147 } ) ;
148148
149149 it ( 'should respect the timeout option passed' , async ( ) => {
150- activateMock . mockReturnValue ( '12345' ) ;
150+ activateMock . mockReturnValue ( null ) ;
151151 readySuccess = false ;
152+
152153 const component = Enzyme . mount (
153154 < OptimizelyProvider optimizely = { optimizelyMock } >
154155 < MyExperimentComponent options = { { timeout : mockDelay } } />
155156 </ OptimizelyProvider >
156157 ) ;
157158 expect ( component . text ( ) ) . toBe ( 'null|false|false' ) ; // initial render
159+
158160 await optimizelyMock . onReady ( ) ;
159161 component . update ( ) ;
160162 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
164168 await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
165169 component . update ( ) ;
166170 expect ( component . text ( ) ) . toBe ( '12345|true|true' ) ; // when clientReady
@@ -243,7 +247,7 @@ describe('hooks', () => {
243247 expect ( mockLog ) . toHaveBeenCalledWith ( '12345' ) ;
244248 } ) ;
245249
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 ( ) => {
247251 readySuccess = false ;
248252 let resolveReadyPromise : ( result : { success : boolean ; dataReadyPromise : Promise < any > } ) => void ;
249253 const readyPromise : Promise < any > = new Promise ( res => {
@@ -266,10 +270,14 @@ describe('hooks', () => {
266270 expect ( mockLog ) . toHaveBeenCalledWith ( null ) ;
267271
268272 mockLog . mockReset ( ) ;
273+
274+ // Simulate datafile fetch completing after timeout has already passed
275+ // Activate now returns a variation
269276 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 ;
273281 component . update ( ) ;
274282
275283 expect ( mockLog ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -380,22 +388,36 @@ describe('hooks', () => {
380388 } ) ;
381389
382390 it ( 'should respect the timeout option passed' , async ( ) => {
383- isFeatureEnabledMock . mockReturnValue ( true ) ;
391+ isFeatureEnabledMock . mockReturnValue ( false ) ;
392+ featureVariables = { } ;
384393 readySuccess = false ;
394+
385395 const component = Enzyme . mount (
386396 < OptimizelyProvider optimizely = { optimizelyMock } >
387397 < MyFeatureComponent options = { { timeout : mockDelay } } />
388398 </ OptimizelyProvider >
389399 ) ;
390400 expect ( component . text ( ) ) . toBe ( 'false|{}|false|false' ) ; // initial render
401+
391402 await optimizelyMock . onReady ( ) ;
392403 component . update ( ) ;
393404 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
397411 await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
398412 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+
399421 expect ( component . text ( ) ) . toBe ( 'true|{"foo":"bar"}|true|true' ) ; // when clientReady
400422 } ) ;
401423
@@ -480,7 +502,7 @@ describe('hooks', () => {
480502 expect ( mockLog ) . toHaveBeenCalledWith ( false ) ;
481503 } ) ;
482504
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 ( ) => {
484506 readySuccess = false ;
485507 let resolveReadyPromise : ( result : { success : boolean ; dataReadyPromise : Promise < any > } ) => void ;
486508 const readyPromise : Promise < any > = new Promise ( res => {
@@ -503,10 +525,14 @@ describe('hooks', () => {
503525 expect ( mockLog ) . toHaveBeenCalledWith ( false ) ;
504526
505527 mockLog . mockReset ( ) ;
528+
529+ // Simulate datafile fetch completing after timeout has already passed
530+ // isFeatureEnabled now returns true
506531 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 ;
510536 component . update ( ) ;
511537
512538 expect ( mockLog ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments