1- import { HttpClientTestingModule } from '@angular/common/http/testing' ;
21import { inject , TestBed } from '@angular/core/testing' ;
32import { Account , UserAgentApplication } from 'msal' ;
43import { AzureAdB2CService } from './azure.ad.b2c.service' ;
54import { CookieService } from 'ngx-cookie-service' ;
6- import { of } from 'rxjs' ;
7-
85
96describe ( 'AzureAdB2CService' , ( ) => {
107 let service : AzureAdB2CService ;
@@ -13,7 +10,7 @@ describe('AzureAdB2CService', () => {
1310
1411 beforeEach ( ( ) => {
1512 TestBed . configureTestingModule ( {
16- imports : [ HttpClientTestingModule ] ,
13+ imports : [ ] ,
1714 } ) ;
1815 service = TestBed . inject ( AzureAdB2CService ) ;
1916 cookieService = TestBed . inject ( CookieService ) ;
@@ -49,12 +46,12 @@ describe('AzureAdB2CService', () => {
4946 it ( 'on logout should call msal logout and verify if user localStorage is removed' , ( ) => {
5047 spyOn ( UserAgentApplication . prototype , 'logout' ) . and . returnValue ( ) ;
5148 spyOn ( cookieService , 'deleteAll' ) ;
52- spyOn ( localStorage , 'clear ' ) ;
49+ spyOn ( localStorage , 'removeItem' ) . withArgs ( 'user ') ;
5350
5451 service . logout ( ) ;
5552
5653 expect ( cookieService . deleteAll ) . toHaveBeenCalled ( ) ;
57- expect ( localStorage . clear ) . toHaveBeenCalled ( ) ;
54+ expect ( localStorage . removeItem ) . toHaveBeenCalledWith ( 'user' ) ;
5855 expect ( UserAgentApplication . prototype . logout ) . toHaveBeenCalled ( ) ;
5956 } ) ;
6057
@@ -87,54 +84,56 @@ describe('AzureAdB2CService', () => {
8784 } ) ;
8885
8986 it ( 'isLogin returns true if UserAgentApplication has a defined Account and token cookie exist' , ( ) => {
87+ spyOn ( UserAgentApplication . prototype , 'getAccount' ) . and . returnValue ( account ) ;
9088 spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
91- spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( true ) ) ;
9289
93- service . isLogin ( ) . subscribe ( isLogin => {
94- expect ( isLogin ) . toEqual ( true ) ;
95- } ) ;
90+ const isLogin = service . isLogin ( ) ;
91+
92+ expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
93+ expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
94+ expect ( isLogin ) . toEqual ( true ) ;
9695 } ) ;
9796
9897 it ( 'isLogin returns false if UserAgentApplication has a defined Account and token cookie does not exist' , ( ) => {
99- spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( false ) ) ;
98+ spyOn ( UserAgentApplication . prototype , 'getAccount' ) . and . returnValue ( account ) ;
99+ spyOn ( cookieService , 'check' ) . and . returnValue ( false ) ;
100100
101+ const isLogin = service . isLogin ( ) ;
101102
102- service . isLogin ( ) . subscribe ( isLogin => {
103- expect ( isLogin ) . toEqual ( false ) ;
104- } ) ;
103+ expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
104+ expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
105+ expect ( isLogin ) . toEqual ( false ) ;
105106 } ) ;
106107
107108 it ( 'isLogin returns false if UserAgentApplication has a null value for Account' , ( ) => {
108109 spyOn ( UserAgentApplication . prototype , 'getAccount' ) . and . returnValue ( null ) ;
109- spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( false ) ) ;
110110
111- service . isLogin ( ) . subscribe ( isLogin => {
112- expect ( isLogin ) . toEqual ( false ) ;
113- } ) ;
111+ const isLogin = service . isLogin ( ) ;
112+
113+ expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
114+ expect ( isLogin ) . toEqual ( false ) ;
114115 } ) ;
115116
116117 it ( 'setTenantId should save a tenantId in local storage' , ( ) => {
117118 spyOn ( UserAgentApplication . prototype , 'getAccount' ) . and . returnValue ( account ) ;
118- spyOn ( service , 'isValidToken ' ) . and . returnValue ( of ( true ) ) ;
119+ spyOn ( cookieService , 'check ' ) . and . returnValue ( true ) ;
119120 spyOn ( localStorage , 'setItem' ) . withArgs ( 'tenant_id' , '12345' ) ;
120121
121- service . isLogin ( ) . subscribe ( isLogin => {
122- expect ( isLogin ) . toEqual ( true ) ;
123- } ) ;
124-
122+ const isLogin = service . isLogin ( ) ;
125123 service . setTenantId ( ) ;
126124
127125 expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
126+ expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
127+ expect ( isLogin ) . toEqual ( true ) ;
128128 expect ( localStorage . setItem ) . toHaveBeenCalledWith ( 'tenant_id' , '12345' ) ;
129129 } ) ;
130130
131131 it ( 'setTenantId should not save tenantId if login is false ' , ( ) => {
132- spyOn ( service , 'isValidToken ' ) . and . returnValue ( of ( false ) ) ;
132+ spyOn ( UserAgentApplication . prototype , 'getAccount ' ) . and . returnValue ( null ) ;
133133 spyOn ( localStorage , 'setItem' ) ;
134-
135- service . isLogin ( ) . subscribe ( isLogin => {
136- expect ( isLogin ) . toEqual ( false ) ;
137- } ) ;
134+ const isLogin = service . isLogin ( ) ;
135+ expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
136+ expect ( isLogin ) . toEqual ( false ) ;
138137 expect ( localStorage . setItem ) . not . toHaveBeenCalled ( ) ;
139138 } ) ;
140139
0 commit comments