1+ import { HttpClient } from '@angular/common/http' ;
12import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ;
23import { TestBed } from '@angular/core/testing' ;
34import { JwtHelperService } from '@auth0/angular-jwt' ;
@@ -14,6 +15,7 @@ describe('LoginService', () => {
1415 let socialAuthService : SocialAuthService ;
1516 let account ;
1617 const socialAuthServiceStub = jasmine . createSpyObj ( 'SocialAuthService' , [ 'signOut' , 'signIn' ] ) ;
18+ const httpClientSpy = jasmine . createSpyObj ( 'HttpClient' , [ 'post' , 'get' ] ) ;
1719 const cookieStoreStub = { } ;
1820 const helper = new JwtHelperService ( ) ;
1921 const getAccountInfo = ( ) => {
@@ -26,6 +28,7 @@ describe('LoginService', () => {
2628 providers : [
2729 { providers : CookieService , useValue : cookieStoreStub } ,
2830 { provide : SocialAuthService , useValue : socialAuthServiceStub } ,
31+ { provide : HttpClient , useValue : httpClientSpy }
2932 ] ,
3033 } ) ;
3134 service = TestBed . inject ( LoginService ) ;
@@ -49,6 +52,7 @@ describe('LoginService', () => {
4952 spyOn ( localStorage , 'setItem' ) . and . callFake ( mockLocalStorage . setItem ) ;
5053 spyOn ( localStorage , 'clear' ) . and . callFake ( mockLocalStorage . clear ) ;
5154 localStorage . setItem ( 'user' , account ) ;
55+ localStorage . setItem ( 'user2' , '"test_token_123"' ) ;
5256 } ) ;
5357
5458 it ( 'should be created' , ( ) => {
@@ -90,12 +94,16 @@ describe('LoginService', () => {
9094 } ) ;
9195
9296 it ( 'load a user by sending a token using POST' , ( ) => {
97+ const token = 'test_123'
9398 service . baseUrl = '/users' ;
99+ const mockSuccessDataPost = {
100+ SUCCESS : true ,
101+ data : { }
102+ }
103+ httpClientSpy . post . and . returnValue ( of ( mockSuccessDataPost ) ) ;
94104 service . getUser ( 'token' ) . subscribe ( ) ;
95-
96- const loadUserRequest = httpMock . expectOne ( `${ service . baseUrl } /login` ) ;
97- expect ( loadUserRequest . request . method ) . toBe ( 'POST' ) ;
98- } ) ;
105+ expect ( httpClientSpy . post ) . toHaveBeenCalled ( ) ;
106+ } ) ;
99107
100108 it ( 'should return true when user is Login' , ( ) => {
101109 spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
@@ -128,4 +136,40 @@ describe('LoginService', () => {
128136 expect ( localStorage . clear ) . toHaveBeenCalled ( ) ;
129137 expect ( cookieService . deleteAll ) . toHaveBeenCalled ( ) ;
130138 } ) ;
139+
140+ it ( 'should call cookieService when app is isLegacyProd' , ( ) => {
141+ service . isLegacyProd = true ;
142+ service . localStorageKey = 'user2' ;
143+ spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
144+ spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( true ) ) ;
145+ service . isLogin ( ) . subscribe ( isLogin => {
146+ expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
147+ } ) ;
148+ } ) ;
149+
150+ it ( 'should call JSON parse when app is isLegacyProd' , ( ) => {
151+ spyOn ( JSON , 'parse' ) . and . returnValue ( 'test_user_123' ) ;
152+ service . isLegacyProd = true ;
153+ service . localStorageKey = 'user2' ;
154+ service . getUserId ( ) ;
155+ service . getName ( ) ;
156+ service . getUserEmail ( ) ;
157+ service . getUserGroup ( ) ;
158+ expect ( JSON . parse ) . toHaveBeenCalled ( ) ;
159+ } ) ;
160+
161+ it ( 'should call setLocalStorage when there is a new_token ' , ( ) => {
162+ spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
163+ spyOn ( service , 'setLocalStorage' ) ;
164+ const token = 'test123'
165+ service . baseUrl = '/users' ;
166+ const mockSuccessDataPost = {
167+ SUCCESS : true ,
168+ new_token : 'test_token'
169+ }
170+ httpClientSpy . post . and . returnValue ( of ( mockSuccessDataPost ) ) ;
171+ service . isValidToken ( token ) . subscribe ( ) ;
172+ expect ( service . setLocalStorage ) . toHaveBeenCalled ( ) ;
173+ expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
174+ } ) ;
131175} ) ;
0 commit comments