@@ -12,8 +12,17 @@ jest.mock('./msal-app.ts', () => {
1212 const msalApplication = {
1313 account : null ,
1414 getAccount : jest . fn ( ) ,
15+ getAccountByHomeId : jest . fn ( ( id ) => ( {
16+ homeAccountId : id ,
17+ environment : 'environment' ,
18+ tenantId : 'tenantId' ,
19+ username : 'username' ,
20+ idTokenClaims :
{ sid :
'test-sid' , login_hint :
'[email protected] ' } 21+ } ) ) ,
1522 logoutRedirect : jest . fn ( ) ,
1623 logoutPopup : jest . fn ( ) ,
24+ // Mock getAllAccounts but don't set a default return value
25+ // Each test will configure this as needed
1726 getAllAccounts : jest . fn ( ) ,
1827 loginPopup : jest . fn ( ( ) => {
1928 return Promise . resolve ( {
@@ -43,6 +52,20 @@ jest.mock('./msal-app.ts', () => {
4352} )
4453describe ( 'AuthenticationWrapper should' , ( ) => {
4554
55+ const mockAccount = {
56+ homeAccountId : 'homeAccountId' ,
57+ environment : 'environment' ,
58+ tenantId : 'tenantId' ,
59+ username : 'username'
60+ } ;
61+
62+ beforeEach ( ( ) => {
63+ jest . clearAllMocks ( ) ;
64+ // Set default mock implementation for most tests
65+ const { msalApplication } = require ( './msal-app.ts' ) ;
66+ msalApplication . getAllAccounts . mockReturnValue ( [ mockAccount ] ) ;
67+ } ) ;
68+
4669 it ( 'log out a user and call removeItem with the home_account_key' , ( ) => {
4770 authenticationWrapper . logOut ( ) ;
4871 expect ( window . localStorage . removeItem ) . toHaveBeenCalledWith ( HOME_ACCOUNT_KEY ) ;
@@ -59,8 +82,36 @@ describe('AuthenticationWrapper should', () => {
5982 } ) ;
6083
6184 it ( 'clear the cache by calling removeItem with all available msal keys' , ( ) => {
85+ // Mock the environment to have MSAL keys in localStorage
86+
87+ // First save original implementation of localStorage methods
88+ const originalGetItem = window . localStorage . getItem ;
89+ const originalKeys = Object . keys ;
90+
91+ // Mock Object.keys to return MSAL-like keys when called on localStorage
92+ Object . keys = jest . fn ( ) . mockImplementation ( ( obj ) => {
93+ if ( obj === localStorage ) {
94+ return [ 'homeAccountId-login.windows.net-idtoken' , 'other-key' ] ;
95+ }
96+ return originalKeys ( obj ) ;
97+ } ) ;
98+
99+ // Make sure getHomeAccountId returns a value that will match our keys
100+ jest . spyOn ( window . localStorage , 'getItem' ) . mockImplementation ( ( key ) => {
101+ if ( key === HOME_ACCOUNT_KEY ) {
102+ return 'homeAccountId' ;
103+ }
104+ return originalGetItem . call ( window . localStorage , key ) ;
105+ } ) ;
106+
62107 authenticationWrapper . clearCache ( ) ;
63- expect ( window . localStorage . removeItem ) . toHaveBeenCalled ( ) ;
108+
109+ // Verify removeItem was called with the expected key
110+ expect ( window . localStorage . removeItem ) . toHaveBeenCalledWith ( 'homeAccountId-login.windows.net-idtoken' ) ;
111+
112+ // Restore original implementations
113+ Object . keys = originalKeys ;
114+ jest . spyOn ( window . localStorage , 'getItem' ) . mockRestore ( ) ;
64115 } ) ;
65116
66117 it ( 'clear user current session, calling removeItem from localStorage and window.sessionStorage.clear' , ( ) => {
@@ -75,21 +126,27 @@ describe('AuthenticationWrapper should', () => {
75126 } ) ;
76127
77128 it ( 'return undefined when getAccount is called and number of accounts is zero' , ( ) => {
129+ const { msalApplication } = require ( './msal-app.ts' ) ;
130+ msalApplication . getAllAccounts . mockReturnValueOnce ( [ ] ) ;
78131 const account = authenticationWrapper . getAccount ( ) ;
79132 expect ( account ) . toBeUndefined ( ) ;
80- } )
133+ } ) ;
81134
82135 it ( 'Log a user in with the appropriate homeAccountId as returned by the auth call' , async ( ) => {
83136 const logIn = await authenticationWrapper . logIn ( ) ;
84137 expect ( logIn . account ! . homeAccountId ) . toBe ( 'homeAccountId' ) ;
85138 } ) ;
86139
87140 it ( 'get consented scopes along with a valid homeAccountId as returned by the auth call' , async ( ) => {
141+ const { msalApplication } = require ( './msal-app.ts' ) ;
142+ msalApplication . getAllAccounts . mockReturnValue ( [ mockAccount ] ) ;
88143 const consentToScopes = await authenticationWrapper . consentToScopes ( ) ;
89144 expect ( consentToScopes . account ! . homeAccountId ) . toBe ( 'homeAccountId' ) ;
90145 } ) ;
91146
92147 it ( 'get auth token with a valid homeAccountId as returned by the auth call' , async ( ) => {
148+ const { msalApplication } = require ( './msal-app.ts' ) ;
149+ msalApplication . getAllAccounts . mockReturnValue ( [ mockAccount ] ) ;
93150 const token = await authenticationWrapper . getToken ( ) ;
94151 expect ( token . account ! . homeAccountId ) . toBe ( 'homeAccountId' ) ;
95152 } ) ;
0 commit comments