1+ import { HttpClientTestingModule } from '@angular/common/http/testing' ;
12import { inject , TestBed } from '@angular/core/testing' ;
23import { Account , UserAgentApplication } from 'msal' ;
34import { AzureAdB2CService } from './azure.ad.b2c.service' ;
45import { CookieService } from 'ngx-cookie-service' ;
6+ import { of } from 'rxjs' ;
7+
58
69describe ( 'AzureAdB2CService' , ( ) => {
710 let service : AzureAdB2CService ;
@@ -10,7 +13,7 @@ describe('AzureAdB2CService', () => {
1013
1114 beforeEach ( ( ) => {
1215 TestBed . configureTestingModule ( {
13- imports : [ ] ,
16+ imports : [ HttpClientTestingModule ] ,
1417 } ) ;
1518 service = TestBed . inject ( AzureAdB2CService ) ;
1619 cookieService = TestBed . inject ( CookieService ) ;
@@ -46,12 +49,12 @@ describe('AzureAdB2CService', () => {
4649 it ( 'on logout should call msal logout and verify if user localStorage is removed' , ( ) => {
4750 spyOn ( UserAgentApplication . prototype , 'logout' ) . and . returnValue ( ) ;
4851 spyOn ( cookieService , 'deleteAll' ) ;
49- spyOn ( localStorage , 'removeItem' ) . withArgs ( 'user ') ;
52+ spyOn ( localStorage , 'clear ' ) ;
5053
5154 service . logout ( ) ;
5255
5356 expect ( cookieService . deleteAll ) . toHaveBeenCalled ( ) ;
54- expect ( localStorage . removeItem ) . toHaveBeenCalledWith ( 'user' ) ;
57+ expect ( localStorage . clear ) . toHaveBeenCalled ( ) ;
5558 expect ( UserAgentApplication . prototype . logout ) . toHaveBeenCalled ( ) ;
5659 } ) ;
5760
@@ -84,56 +87,54 @@ describe('AzureAdB2CService', () => {
8487 } ) ;
8588
8689 it ( 'isLogin returns true if UserAgentApplication has a defined Account and token cookie exist' , ( ) => {
87- spyOn ( UserAgentApplication . prototype , 'getAccount' ) . and . returnValue ( account ) ;
8890 spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
91+ spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( true ) ) ;
8992
90- const isLogin = service . isLogin ( ) ;
91-
92- expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
93- expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
94- expect ( isLogin ) . toEqual ( true ) ;
93+ service . isLogin ( ) . subscribe ( isLogin => {
94+ expect ( isLogin ) . toEqual ( true ) ;
95+ } ) ;
9596 } ) ;
9697
9798 it ( 'isLogin returns false if UserAgentApplication has a defined Account and token cookie does not exist' , ( ) => {
98- spyOn ( UserAgentApplication . prototype , 'getAccount' ) . and . returnValue ( account ) ;
99- spyOn ( cookieService , 'check' ) . and . returnValue ( false ) ;
99+ spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( false ) ) ;
100100
101- const isLogin = service . isLogin ( ) ;
102101
103- expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
104- expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
105- expect ( isLogin ) . toEqual ( false ) ;
102+ service . isLogin ( ) . subscribe ( isLogin => {
103+ expect ( isLogin ) . toEqual ( false ) ;
104+ } ) ;
106105 } ) ;
107106
108107 it ( 'isLogin returns false if UserAgentApplication has a null value for Account' , ( ) => {
109108 spyOn ( UserAgentApplication . prototype , 'getAccount' ) . and . returnValue ( null ) ;
109+ spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( false ) ) ;
110110
111- const isLogin = service . isLogin ( ) ;
112-
113- expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
114- expect ( isLogin ) . toEqual ( false ) ;
111+ service . isLogin ( ) . subscribe ( isLogin => {
112+ expect ( isLogin ) . toEqual ( false ) ;
113+ } ) ;
115114 } ) ;
116115
117116 it ( 'setTenantId should save a tenantId in local storage' , ( ) => {
118117 spyOn ( UserAgentApplication . prototype , 'getAccount' ) . and . returnValue ( account ) ;
119- spyOn ( cookieService , 'check ' ) . and . returnValue ( true ) ;
118+ spyOn ( service , 'isValidToken ' ) . and . returnValue ( of ( true ) ) ;
120119 spyOn ( localStorage , 'setItem' ) . withArgs ( 'tenant_id' , '12345' ) ;
121120
122- const isLogin = service . isLogin ( ) ;
121+ service . isLogin ( ) . subscribe ( isLogin => {
122+ expect ( isLogin ) . toEqual ( true ) ;
123+ } ) ;
124+
123125 service . setTenantId ( ) ;
124126
125127 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 ( UserAgentApplication . prototype , 'getAccount ' ) . and . returnValue ( null ) ;
132+ spyOn ( service , 'isValidToken ' ) . and . returnValue ( of ( false ) ) ;
133133 spyOn ( localStorage , 'setItem' ) ;
134- const isLogin = service . isLogin ( ) ;
135- expect ( UserAgentApplication . prototype . getAccount ) . toHaveBeenCalled ( ) ;
136- expect ( isLogin ) . toEqual ( false ) ;
134+
135+ service . isLogin ( ) . subscribe ( isLogin => {
136+ expect ( isLogin ) . toEqual ( false ) ;
137+ } ) ;
137138 expect ( localStorage . setItem ) . not . toHaveBeenCalled ( ) ;
138139 } ) ;
139140
0 commit comments