@@ -126,28 +126,39 @@ describe('Integrated test - Switcher:', function () {
126
126
// first API call
127
127
given ( fetchStub , 0 , { json : ( ) => generateAuth ( '[auth_token]' , 1 ) , status : 200 } ) ;
128
128
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
+
131
130
// test
132
131
Switcher . buildContext ( contextSettings ) ;
133
-
132
+
134
133
let switcher = Switcher . factory ( ) ;
135
134
switcher . throttle ( 500 ) ;
136
-
135
+
137
136
const spyPrepare = spy ( switcher , 'prepare' ) ;
138
-
137
+
139
138
// first API call - stores result in cache
140
139
let result = await switcher . isItOn ( 'FLAG_3' ) ;
141
140
assert . isTrue ( result ) ;
142
141
assert . equal ( spyPrepare . callCount , 1 ) ;
143
-
142
+
144
143
// first async API call
145
144
result = await switcher . isItOn ( 'FLAG_3' ) ;
146
145
assert . isTrue ( result ) ;
147
146
assert . equal ( spyPrepare . callCount , 1 ) ;
148
-
147
+
149
148
// Next call should call the API again - token has expired
150
149
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 ) ) ;
151
162
result = await switcher . isItOn ( 'FLAG_3' ) ;
152
163
assert . isFalse ( result ) ;
153
164
assert . equal ( spyPrepare . callCount , 2 ) ;
0 commit comments