11import { faker } from '@faker-js/faker' ;
22import { DateTimeFormatter , ZonedDateTime , nativeJs } from '@js-joda/core' ;
3- import axios , { AxiosError } from 'axios' ;
3+ import { AxiosError } from 'axios' ;
44import { saveAs } from 'file-saver' ;
55import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
66import { REPORT_STATUS } from '../../utils/constant' ;
77import ApiService , { ApiServicePrivate } from '../apiService' ;
8- import authService from '../authService.js ' ;
8+ import * as AuthStoreComplete from '../../store/modules/auth ' ;
99
1010//Mock the interceptor used by the ApiService so it no longer depends on
1111//HTTP calls to the backend.
@@ -523,7 +523,6 @@ describe('ApiServicePrivate', () => {
523523 expect ( result ) . toBe ( expected ) ;
524524 } ) ;
525525 } ) ;
526-
527526 describe ( 'responseErrorInterceptor' , ( ) => {
528527 describe ( 'when response status is 401' , ( ) => {
529528 it ( 'it tries to refresh the token, and then retries the original request (with the new token)' , async ( ) => {
@@ -543,30 +542,40 @@ describe('ApiServicePrivate', () => {
543542 correlationID : faker . string . alpha ( 50 ) ,
544543 } ;
545544
546- //mock the request to the backend to refresh the token.
547- const refreshTokenRequestSpy = vi
548- . spyOn ( authService , 'refreshAuthToken' )
549- . mockResolvedValue ( mockRefreshTokenResponse ) ;
545+ // Mock the auth store
546+ const mockAuthStore = {
547+ getJwtToken : vi . fn ( ) . mockResolvedValue ( undefined ) ,
548+ isAuthenticated : true ,
549+ jwtToken : mockRefreshTokenResponse . jwtFrontend ,
550+ } ;
551+ vi . spyOn ( AuthStoreComplete , 'authStore' ) . mockReturnValue ( mockAuthStore ) ;
550552
551- //mock the resubmission of the original request (which occurs after
552- //receiving the new token)
553+ // Mock localStorage
554+ vi . spyOn ( Storage . prototype , 'getItem' ) . mockReturnValue (
555+ mockRefreshTokenResponse . correlationID ,
556+ ) ;
557+
558+ // Spy on the apiAxios instance instead of global axios
553559 const axiosRequestSpy = vi
554- . spyOn ( axios , 'request' )
560+ . spyOn ( ApiService . apiAxios , 'request' )
555561 . mockResolvedValue ( { } ) ;
556562
563+ // Mock processQueue if needed
564+ global . processQueue = vi . fn ( ) ;
565+
557566 await expect (
558567 ApiServicePrivate . responseErrorInterceptor ( mockUnauthorizedError ) ,
559568 ) . resolves ;
560569
561- //expect a request to refresh the token
562- expect ( refreshTokenRequestSpy ) . toHaveBeenCalledOnce ( ) ;
570+ // Verify the auth store's getJwtToken was called
571+ expect ( mockAuthStore . getJwtToken ) . toHaveBeenCalledOnce ( ) ;
563572
564- //after token refresh, the original request is resubmitted (but with
565- //a new token in the authorization header)
573+ // After token refresh, the original request is resubmitted
566574 expect ( axiosRequestSpy ) . toHaveBeenCalledOnce ( ) ;
567- const resubmittedRequest : any = axiosRequestSpy . mock . calls [ 0 ] [ 0 ] ;
575+ const resubmittedRequest = axiosRequestSpy . mock . calls [ 0 ] [ 0 ] ;
568576 const expectedResubmittedRequest = {
569577 ...originalRequest ,
578+ authAlreadyRetried : true ,
570579 } ;
571580 expectedResubmittedRequest . headers [ 'Authorization' ] =
572581 `Bearer ${ mockRefreshTokenResponse . jwtFrontend } ` ;
0 commit comments