@@ -126,28 +126,39 @@ describe('Integrated test - Switcher:', function () {
126126 // first API call
127127 given ( fetchStub , 0 , { json : ( ) => generateAuth ( '[auth_token]' , 1 ) , status : 200 } ) ;
128128 given ( fetchStub , 1 , { json : ( ) => generateResult ( true ) , status : 200 } ) ; // before token expires
129- given ( fetchStub , 2 , { json : ( ) => generateResult ( false ) , status : 200 } ) ; // after token expires
130-
129+
131130 // test
132131 Switcher . buildContext ( contextSettings ) ;
133-
132+
134133 let switcher = Switcher . factory ( ) ;
135134 switcher . throttle ( 500 ) ;
136-
135+
137136 const spyPrepare = spy ( switcher , 'prepare' ) ;
138-
137+
139138 // first API call - stores result in cache
140139 let result = await switcher . isItOn ( 'FLAG_3' ) ;
141140 assert . isTrue ( result ) ;
142141 assert . equal ( spyPrepare . callCount , 1 ) ;
143-
142+
144143 // first async API call
145144 result = await switcher . isItOn ( 'FLAG_3' ) ;
146145 assert . isTrue ( result ) ;
147146 assert . equal ( spyPrepare . callCount , 1 ) ;
148-
147+
149148 // Next call should call the API again - token has expired
150149 await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
150+
151+ // given
152+ given ( fetchStub , 3 , { json : ( ) => generateAuth ( '[auth_token]' , 5 ) , status : 200 } ) ;
153+ given ( fetchStub , 4 , { json : ( ) => generateResult ( false ) , status : 200 } ) ; // after token expires
154+
155+ // test - stores result in cache after token renewal
156+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
157+ result = await switcher . isItOn ( 'FLAG_3' ) ;
158+ assert . isTrue ( result ) ;
159+ assert . equal ( spyPrepare . callCount , 2 ) ;
160+
161+ await new Promise ( resolve => setTimeout ( resolve , 50 ) ) ;
151162 result = await switcher . isItOn ( 'FLAG_3' ) ;
152163 assert . isFalse ( result ) ;
153164 assert . equal ( spyPrepare . callCount , 2 ) ;
0 commit comments