@@ -317,6 +317,7 @@ describe('core/auth/auth_impl', () => {
317317 let exchangeTokenStub : sinon . SinonStub ;
318318 let mockToken : FirebaseToken ;
319319 let expiredMockToken : FirebaseToken ;
320+ let soonToExpireMockToken : FirebaseToken ;
320321 let tokenRefreshHandler : TokenRefreshHandler ;
321322 const tokenKey = `firebase:persistence-token:${ FAKE_APP . options . apiKey ! } :${
322323 FAKE_APP . name
@@ -331,6 +332,10 @@ describe('core/auth/auth_impl', () => {
331332 token : 'test-token' ,
332333 expirationTime : Date . now ( ) + 300000 // 5 minutes from now
333334 } ;
335+ soonToExpireMockToken = {
336+ token : 'test-token' ,
337+ expirationTime : Date . now ( ) + 60000 // 1 minutes from now
338+ } ;
334339 expiredMockToken = {
335340 token : 'expired-test-token' ,
336341 expirationTime : Date . now ( ) - 1000 // 1 second ago
@@ -384,6 +389,28 @@ describe('core/auth/auth_impl', () => {
384389 expect ( token ) . to . eql ( 'test-token' ) ;
385390 } ) ;
386391
392+ it ( 'should refresh the token if token is expiring in next 1 minute and a token refresh handler is set' , async ( ) => {
393+ persistenceStub . _get
394+ . withArgs ( tokenKey )
395+ . resolves ( soonToExpireMockToken as any ) ;
396+ auth . setTokenRefreshHandler ( tokenRefreshHandler ) ;
397+
398+ exchangeTokenStub . callsFake ( async ( ) => {
399+ // When exchangeToken is called, simulate that the new token is persisted.
400+ persistenceStub . _get . withArgs ( tokenKey ) . resolves ( mockToken as any ) ;
401+ } ) ;
402+
403+ const token = await auth . getFirebaseAccessToken ( ) ;
404+
405+ expect ( tokenRefreshHandler . refreshIdpToken ) . to . have . been . calledOnce ;
406+ expect ( exchangeTokenStub ) . to . have . been . calledWith (
407+ auth ,
408+ 'test-idp' ,
409+ 'new-id-token'
410+ ) ;
411+ expect ( token ) . to . eql ( 'test-token' ) ;
412+ } ) ;
413+
387414 it ( 'should force refresh the token when forceRefresh is true' , async ( ) => {
388415 persistenceStub . _get . withArgs ( tokenKey ) . resolves ( mockToken as any ) ;
389416 auth . setTokenRefreshHandler ( tokenRefreshHandler ) ;
0 commit comments