1
- import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ;
1
+ import { HttpClient } from '@angular/common/http' ;
2
+ import { HttpClientTestingModule } from '@angular/common/http/testing' ;
2
3
import { TestBed } from '@angular/core/testing' ;
3
4
import { JwtHelperService } from '@auth0/angular-jwt' ;
4
5
import { SocialAuthService } from 'angularx-social-login' ;
@@ -9,11 +10,11 @@ import { LoginService } from './login.service';
9
10
10
11
describe ( 'LoginService' , ( ) => {
11
12
let service : LoginService ;
12
- let httpMock : HttpTestingController ;
13
13
let cookieService : CookieService ;
14
14
let socialAuthService : SocialAuthService ;
15
15
let account ;
16
16
const socialAuthServiceStub = jasmine . createSpyObj ( 'SocialAuthService' , [ 'signOut' , 'signIn' ] ) ;
17
+ const httpClientSpy = jasmine . createSpyObj ( 'HttpClient' , [ 'post' , 'get' ] ) ;
17
18
const cookieStoreStub = { } ;
18
19
const helper = new JwtHelperService ( ) ;
19
20
const getAccountInfo = ( ) => {
@@ -26,11 +27,11 @@ describe('LoginService', () => {
26
27
providers : [
27
28
{ providers : CookieService , useValue : cookieStoreStub } ,
28
29
{ provide : SocialAuthService , useValue : socialAuthServiceStub } ,
30
+ { provide : HttpClient , useValue : httpClientSpy }
29
31
] ,
30
32
} ) ;
31
33
service = TestBed . inject ( LoginService ) ;
32
34
cookieService = TestBed . inject ( CookieService ) ;
33
- httpMock = TestBed . inject ( HttpTestingController ) ;
34
35
socialAuthService = TestBed . inject ( SocialAuthService ) ;
35
36
account = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImFiYyIsImdyb3VwcyI6WyJhYmMiXX0.UNxyDT8XzXJhI1F3LySBU7TJlpENPUPHj8my7Obw2ZM' ;
36
37
let store = { } ;
@@ -49,6 +50,7 @@ describe('LoginService', () => {
49
50
spyOn ( localStorage , 'setItem' ) . and . callFake ( mockLocalStorage . setItem ) ;
50
51
spyOn ( localStorage , 'clear' ) . and . callFake ( mockLocalStorage . clear ) ;
51
52
localStorage . setItem ( 'user' , account ) ;
53
+ localStorage . setItem ( 'user2' , '"test_token_123"' ) ;
52
54
} ) ;
53
55
54
56
it ( 'should be created' , ( ) => {
@@ -90,12 +92,16 @@ describe('LoginService', () => {
90
92
} ) ;
91
93
92
94
it ( 'load a user by sending a token using POST' , ( ) => {
95
+ const token = 'test_123' ;
93
96
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
+ } ) ;
99
105
100
106
it ( 'should return true when user is Login' , ( ) => {
101
107
spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
@@ -122,4 +128,40 @@ describe('LoginService', () => {
122
128
expect ( localStorage . clear ) . toHaveBeenCalled ( ) ;
123
129
expect ( cookieService . deleteAll ) . toHaveBeenCalled ( ) ;
124
130
} ) ;
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
+ } ) ;
125
167
} ) ;
0 commit comments