1
1
import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ;
2
2
import { TestBed } from '@angular/core/testing' ;
3
- import { JwtHelperService } from '@auth0/angular-jwt' ;
4
3
import { SocialAuthService } from 'angularx-social-login' ;
5
4
import { CookieService } from 'ngx-cookie-service' ;
6
5
import { of } from 'rxjs' ;
@@ -15,10 +14,6 @@ describe('LoginService', () => {
15
14
let account ;
16
15
const socialAuthServiceStub = jasmine . createSpyObj ( 'SocialAuthService' , [ 'signOut' , 'signIn' ] ) ;
17
16
const cookieStoreStub = { } ;
18
- const helper = new JwtHelperService ( ) ;
19
- const getAccountInfo = ( ) => {
20
- return helper . decodeToken ( account ) ;
21
- } ;
22
17
23
18
beforeEach ( ( ) => {
24
19
TestBed . configureTestingModule ( {
@@ -32,7 +27,12 @@ describe('LoginService', () => {
32
27
cookieService = TestBed . inject ( CookieService ) ;
33
28
httpMock = TestBed . inject ( HttpTestingController ) ;
34
29
socialAuthService = TestBed . inject ( SocialAuthService ) ;
35
- account = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImFiYyIsImdyb3VwcyI6WyJhYmMiXX0.UNxyDT8XzXJhI1F3LySBU7TJlpENPUPHj8my7Obw2ZM' ;
30
+ account = {
31
+ id : 'abc' ,
32
+ name : 'abc' ,
33
+ email : 'abc' ,
34
+ groups : [ 'abc' ] ,
35
+ } ;
36
36
let store = { } ;
37
37
const mockLocalStorage = {
38
38
getItem : ( key : string ) : string => {
@@ -48,7 +48,7 @@ describe('LoginService', () => {
48
48
spyOn ( localStorage , 'getItem' ) . and . callFake ( mockLocalStorage . getItem ) ;
49
49
spyOn ( localStorage , 'setItem' ) . and . callFake ( mockLocalStorage . setItem ) ;
50
50
spyOn ( localStorage , 'clear' ) . and . callFake ( mockLocalStorage . clear ) ;
51
- localStorage . setItem ( 'user ' , account ) ;
51
+ localStorage . setItem ( 'user2 ' , JSON . stringify ( account ) ) ;
52
52
} ) ;
53
53
54
54
it ( 'should be created' , ( ) => {
@@ -58,19 +58,19 @@ describe('LoginService', () => {
58
58
it ( 'should get name from localStorage' , ( ) => {
59
59
const name = service . getName ( ) ;
60
60
61
- expect ( name ) . toEqual ( getAccountInfo ( ) . name ) ;
61
+ expect ( name ) . toEqual ( account . name ) ;
62
62
} ) ;
63
63
64
64
it ( 'should get userId from localStorage' , ( ) => {
65
65
const userId = service . getUserId ( ) ;
66
66
67
- expect ( userId ) . toEqual ( getAccountInfo ( ) . id ) ;
67
+ expect ( userId ) . toEqual ( account . id ) ;
68
68
} ) ;
69
69
70
70
it ( 'should get UserGroup from localStorage' , ( ) => {
71
71
const userGroup = service . getUserGroup ( ) ;
72
72
73
- expect ( userGroup ) . toEqual ( getAccountInfo ( ) . groups ) ;
73
+ expect ( userGroup ) . toEqual ( account . groups ) ;
74
74
} ) ;
75
75
76
76
it ( 'should get BearerToken from localStorage' , ( ) => {
@@ -99,19 +99,18 @@ describe('LoginService', () => {
99
99
100
100
it ( 'should return true when user is Login' , ( ) => {
101
101
spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
102
- spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( true ) ) ;
103
102
104
- service . isLogin ( ) . subscribe ( isLogin => {
105
- expect ( isLogin ) . toEqual ( true ) ;
106
- } ) ;
103
+ const isLogin = service . isLogin ( ) ;
104
+
105
+ expect ( isLogin ) . toBeTruthy ( ) ;
107
106
} ) ;
108
107
109
108
it ( 'should return false when user is not Login' , ( ) => {
110
- spyOn ( service , 'isValidToken ' ) . and . returnValue ( of ( false ) ) ;
109
+ spyOn ( cookieService , 'check ' ) . and . returnValue ( false ) ;
111
110
112
- service . isLogin ( ) . subscribe ( isLogin => {
113
- expect ( isLogin ) . toEqual ( false ) ;
114
- } ) ;
111
+ const isLogin = service . isLogin ( ) ;
112
+
113
+ expect ( isLogin ) . toBeFalsy ( ) ;
115
114
} ) ;
116
115
117
116
it ( 'should login with social angularx-social-login' , ( ) => {
0 commit comments