1- import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ;
1+ import { HttpClient } from '@angular/common/http' ;
2+ import { HttpClientTestingModule } from '@angular/common/http/testing' ;
23import { TestBed } from '@angular/core/testing' ;
34import { JwtHelperService } from '@auth0/angular-jwt' ;
45import { SocialAuthService } from 'angularx-social-login' ;
@@ -9,11 +10,11 @@ import { LoginService } from './login.service';
910
1011describe ( 'LoginService' , ( ) => {
1112 let service : LoginService ;
12- let httpMock : HttpTestingController ;
1313 let cookieService : CookieService ;
1414 let socialAuthService : SocialAuthService ;
1515 let account ;
1616 const socialAuthServiceStub = jasmine . createSpyObj ( 'SocialAuthService' , [ 'signOut' , 'signIn' ] ) ;
17+ const httpClientSpy = jasmine . createSpyObj ( 'HttpClient' , [ 'post' , 'get' ] ) ;
1718 const cookieStoreStub = { } ;
1819 const helper = new JwtHelperService ( ) ;
1920 const getAccountInfo = ( ) => {
@@ -26,11 +27,11 @@ describe('LoginService', () => {
2627 providers : [
2728 { providers : CookieService , useValue : cookieStoreStub } ,
2829 { provide : SocialAuthService , useValue : socialAuthServiceStub } ,
30+ { provide : HttpClient , useValue : httpClientSpy }
2931 ] ,
3032 } ) ;
3133 service = TestBed . inject ( LoginService ) ;
3234 cookieService = TestBed . inject ( CookieService ) ;
33- httpMock = TestBed . inject ( HttpTestingController ) ;
3435 socialAuthService = TestBed . inject ( SocialAuthService ) ;
3536 account = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImFiYyIsImdyb3VwcyI6WyJhYmMiXX0.UNxyDT8XzXJhI1F3LySBU7TJlpENPUPHj8my7Obw2ZM' ;
3637 let store = { } ;
@@ -49,6 +50,7 @@ describe('LoginService', () => {
4950 spyOn ( localStorage , 'setItem' ) . and . callFake ( mockLocalStorage . setItem ) ;
5051 spyOn ( localStorage , 'clear' ) . and . callFake ( mockLocalStorage . clear ) ;
5152 localStorage . setItem ( 'user' , account ) ;
53+ localStorage . setItem ( 'user2' , '"test_token_123"' ) ;
5254 } ) ;
5355
5456 it ( 'should be created' , ( ) => {
@@ -90,12 +92,16 @@ describe('LoginService', () => {
9092 } ) ;
9193
9294 it ( 'load a user by sending a token using POST' , ( ) => {
95+ const token = 'test_123' ;
9396 service . baseUrl = '/users' ;
94- service . getUser ( 'token' ) . subscribe ( ) ;
95-
96- const loadUserRequest = httpMock . expectOne ( `${ service . baseUrl } /login` ) ;
97- expect ( loadUserRequest . request . method ) . toBe ( 'POST' ) ;
98- } ) ;
97+ const mockSuccessDataPost = {
98+ SUCCESS : true ,
99+ data : { }
100+ } ;
101+ httpClientSpy . post . and . returnValue ( of ( mockSuccessDataPost ) ) ;
102+ service . getUser ( token ) . subscribe ( ) ;
103+ expect ( httpClientSpy . post ) . toHaveBeenCalled ( ) ;
104+ } ) ;
99105
100106 it ( 'should return true when user is Login' , ( ) => {
101107 spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
@@ -122,4 +128,40 @@ describe('LoginService', () => {
122128 expect ( localStorage . clear ) . toHaveBeenCalled ( ) ;
123129 expect ( cookieService . deleteAll ) . toHaveBeenCalled ( ) ;
124130 } ) ;
131+
132+ it ( 'should call cookieService when app is isLegacyProd' , ( ) => {
133+ service . isLegacyProd = true ;
134+ service . localStorageKey = 'user2' ;
135+ spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
136+ spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( true ) ) ;
137+ service . isLogin ( ) . subscribe ( isLogin => {
138+ expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
139+ } ) ;
140+ } ) ;
141+
142+ it ( 'should call JSON parse when app is isLegacyProd' , ( ) => {
143+ spyOn ( JSON , 'parse' ) . and . returnValue ( 'test_user_123' ) ;
144+ service . isLegacyProd = true ;
145+ service . localStorageKey = 'user2' ;
146+ service . getUserId ( ) ;
147+ service . getName ( ) ;
148+ service . getUserEmail ( ) ;
149+ service . getUserGroup ( ) ;
150+ expect ( JSON . parse ) . toHaveBeenCalled ( ) ;
151+ } ) ;
152+
153+ it ( 'should call setLocalStorage when there is a new_token ' , ( ) => {
154+ spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
155+ spyOn ( service , 'setLocalStorage' ) ;
156+ const token = 'test123' ;
157+ service . baseUrl = '/users' ;
158+ const mockSuccessDataPost = {
159+ SUCCESS : true ,
160+ new_token : 'test_token'
161+ } ;
162+ httpClientSpy . post . and . returnValue ( of ( mockSuccessDataPost ) ) ;
163+ service . isValidToken ( token ) . subscribe ( ) ;
164+ expect ( service . setLocalStorage ) . toHaveBeenCalled ( ) ;
165+ expect ( cookieService . check ) . toHaveBeenCalled ( ) ;
166+ } ) ;
125167} ) ;
0 commit comments