11import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ;
22import { TestBed } from '@angular/core/testing' ;
3- import { JwtHelperService } from '@auth0/angular-jwt' ;
43import { SocialAuthService } from 'angularx-social-login' ;
54import { CookieService } from 'ngx-cookie-service' ;
65import { of } from 'rxjs' ;
@@ -15,10 +14,6 @@ describe('LoginService', () => {
1514 let account ;
1615 const socialAuthServiceStub = jasmine . createSpyObj ( 'SocialAuthService' , [ 'signOut' , 'signIn' ] ) ;
1716 const cookieStoreStub = { } ;
18- const helper = new JwtHelperService ( ) ;
19- const getAccountInfo = ( ) => {
20- return helper . decodeToken ( account ) ;
21- } ;
2217
2318 beforeEach ( ( ) => {
2419 TestBed . configureTestingModule ( {
@@ -32,7 +27,12 @@ describe('LoginService', () => {
3227 cookieService = TestBed . inject ( CookieService ) ;
3328 httpMock = TestBed . inject ( HttpTestingController ) ;
3429 socialAuthService = TestBed . inject ( SocialAuthService ) ;
35- account = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImFiYyIsImdyb3VwcyI6WyJhYmMiXX0.UNxyDT8XzXJhI1F3LySBU7TJlpENPUPHj8my7Obw2ZM' ;
30+ account = {
31+ id : 'abc' ,
32+ name : 'abc' ,
33+ email : 'abc' ,
34+ groups : [ 'abc' ] ,
35+ } ;
3636 let store = { } ;
3737 const mockLocalStorage = {
3838 getItem : ( key : string ) : string => {
@@ -48,7 +48,7 @@ describe('LoginService', () => {
4848 spyOn ( localStorage , 'getItem' ) . and . callFake ( mockLocalStorage . getItem ) ;
4949 spyOn ( localStorage , 'setItem' ) . and . callFake ( mockLocalStorage . setItem ) ;
5050 spyOn ( localStorage , 'clear' ) . and . callFake ( mockLocalStorage . clear ) ;
51- localStorage . setItem ( 'user ' , account ) ;
51+ localStorage . setItem ( 'user2 ' , JSON . stringify ( account ) ) ;
5252 } ) ;
5353
5454 it ( 'should be created' , ( ) => {
@@ -58,19 +58,19 @@ describe('LoginService', () => {
5858 it ( 'should get name from localStorage' , ( ) => {
5959 const name = service . getName ( ) ;
6060
61- expect ( name ) . toEqual ( getAccountInfo ( ) . name ) ;
61+ expect ( name ) . toEqual ( account . name ) ;
6262 } ) ;
6363
6464 it ( 'should get userId from localStorage' , ( ) => {
6565 const userId = service . getUserId ( ) ;
6666
67- expect ( userId ) . toEqual ( getAccountInfo ( ) . id ) ;
67+ expect ( userId ) . toEqual ( account . id ) ;
6868 } ) ;
6969
7070 it ( 'should get UserGroup from localStorage' , ( ) => {
7171 const userGroup = service . getUserGroup ( ) ;
7272
73- expect ( userGroup ) . toEqual ( getAccountInfo ( ) . groups ) ;
73+ expect ( userGroup ) . toEqual ( account . groups ) ;
7474 } ) ;
7575
7676 it ( 'should get BearerToken from localStorage' , ( ) => {
@@ -99,19 +99,18 @@ describe('LoginService', () => {
9999
100100 it ( 'should return true when user is Login' , ( ) => {
101101 spyOn ( cookieService , 'check' ) . and . returnValue ( true ) ;
102- spyOn ( service , 'isValidToken' ) . and . returnValue ( of ( true ) ) ;
103102
104- service . isLogin ( ) . subscribe ( isLogin => {
105- expect ( isLogin ) . toEqual ( true ) ;
106- } ) ;
103+ const isLogin = service . isLogin ( ) ;
104+
105+ expect ( isLogin ) . toBeTruthy ( ) ;
107106 } ) ;
108107
109108 it ( 'should return false when user is not Login' , ( ) => {
110- spyOn ( service , 'isValidToken ' ) . and . returnValue ( of ( false ) ) ;
109+ spyOn ( cookieService , 'check ' ) . and . returnValue ( false ) ;
111110
112- service . isLogin ( ) . subscribe ( isLogin => {
113- expect ( isLogin ) . toEqual ( false ) ;
114- } ) ;
111+ const isLogin = service . isLogin ( ) ;
112+
113+ expect ( isLogin ) . toBeFalsy ( ) ;
115114 } ) ;
116115
117116 it ( 'should login with social angularx-social-login' , ( ) => {
0 commit comments