@@ -5,11 +5,15 @@ import { of } from 'rxjs';
55import { LoginComponent } from './login.component' ;
66import { Router } from '@angular/router' ;
77import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-toggle-cookies/feature-toggle-cookies.service' ;
8+ import { LoginService } from './services/login.service' ;
9+ import { HttpClientTestingModule } from '@angular/common/http/testing' ;
10+ import { SocialAuthService } from 'angularx-social-login' ;
811
912describe ( 'LoginComponent' , ( ) => {
1013 let component : LoginComponent ;
1114 let fixture : ComponentFixture < LoginComponent > ;
1215 let azureAdB2CService : AzureAdB2CService ;
16+ let loginService : LoginService ;
1317 let featureToggleCookiesService : FeatureToggleCookiesService ;
1418
1519 const azureAdB2CServiceStub = {
@@ -23,29 +27,45 @@ describe('LoginComponent', () => {
2327 }
2428 } ;
2529
30+ const loginServiceStub = {
31+ isLogin ( ) {
32+ return true ;
33+ } ,
34+ signIn ( ) {
35+ return of ( ) ;
36+ } ,
37+ setCookies ( ) {
38+ }
39+ } ;
40+
2641 const featureToggleCookiesServiceStub = {
2742 setCookies ( ) {
2843 return null ;
2944 }
3045 } ;
3146
47+ const socialAuthServiceStub = jasmine . createSpyObj ( 'SocialAuthService' , [ 'signIn' , 'authState' ] ) ;
3248 beforeEach ( waitForAsync ( ( ) => {
3349 TestBed . configureTestingModule ( {
34- imports : [ RouterTestingModule ] ,
50+ imports : [ RouterTestingModule , HttpClientTestingModule ] ,
3551 declarations : [ LoginComponent ] ,
3652 providers : [
3753 { providers : AzureAdB2CService , useValue : azureAdB2CServiceStub } ,
38- { providers : FeatureToggleCookiesService , useValue : featureToggleCookiesServiceStub }
54+ { providers : FeatureToggleCookiesService , useValue : featureToggleCookiesServiceStub } ,
55+ { providers : LoginService , useValue : loginServiceStub } ,
56+ { provide : SocialAuthService , useValue : socialAuthServiceStub }
3957 ]
4058 } )
4159 . compileComponents ( ) ;
4260 } ) ) ;
4361
4462 beforeEach ( ( ) => {
63+ socialAuthServiceStub . authState = of ( 'some value' ) ;
4564 fixture = TestBed . createComponent ( LoginComponent ) ;
4665 component = fixture . componentInstance ;
4766 fixture . detectChanges ( ) ;
4867 azureAdB2CService = TestBed . inject ( AzureAdB2CService ) ;
68+ loginService = TestBed . inject ( LoginService ) ;
4969 featureToggleCookiesService = TestBed . inject ( FeatureToggleCookiesService ) ;
5070 } ) ;
5171
@@ -65,7 +85,7 @@ describe('LoginComponent', () => {
6585 expect ( component ) . toBeTruthy ( ) ;
6686 } ) ;
6787
68- it ( 'should sign up or login with google if is not logged-in into the app' , inject ( [ Router ] , ( router : Router ) => {
88+ it ( 'should sign up or login with google if is not logged-in into the app on Production ' , inject ( [ Router ] , ( router : Router ) => {
6989 spyOn ( azureAdB2CService , 'isLogin' ) . and . returnValue ( false ) ;
7090 spyOn ( azureAdB2CService , 'setCookies' ) . and . returnValue ( ) ;
7191 spyOn ( azureAdB2CService , 'signIn' ) . and . returnValue ( of ( ( ) => { } ) ) ;
@@ -78,11 +98,35 @@ describe('LoginComponent', () => {
7898 expect ( featureToggleCookiesService . setCookies ) . toHaveBeenCalled ( ) ;
7999 } ) ) ;
80100
81- it ( 'should not sign-up or login with google if is already logged-in into the app' , inject ( [ Router ] , ( router : Router ) => {
101+ it ( 'should sign up or login with google if is not logged-in into the app Locally' , inject ( [ Router ] , ( router : Router ) => {
102+ spyOn ( loginService , 'isLogin' ) . and . returnValue ( false ) ;
103+ spyOn ( loginService , 'setLocalStorage' ) . and . returnValue ( ) ;
104+ spyOn ( loginService , 'getUser' ) . and . returnValue ( of ( ( ) => { } ) ) ;
105+ spyOn ( loginService , 'setCookies' ) . and . returnValue ( ) ;
106+ spyOn ( loginService , 'signIn' ) . and . returnValue ( ) ;
107+ spyOn ( featureToggleCookiesService , 'setCookies' ) . and . returnValue ( featureToggleCookiesService . setCookies ( ) ) ;
108+
109+ component . ngOnInit ( ) ;
110+ component . loginWithGoogle ( ) ;
111+
112+ expect ( loginService . signIn ) . toHaveBeenCalled ( ) ;
113+ expect ( loginService . setCookies ) . toHaveBeenCalled ( ) ;
114+ expect ( featureToggleCookiesService . setCookies ) . toHaveBeenCalled ( ) ;
115+ } ) ) ;
116+
117+ it ( 'should not sign-up or login with google if is already logged-in into the app on Production' , inject ( [ Router ] , ( router : Router ) => {
82118 spyOn ( azureAdB2CService , 'isLogin' ) . and . returnValue ( true ) ;
83119 spyOn ( router , 'navigate' ) . and . stub ( ) ;
84120 component . login ( ) ;
85121 expect ( azureAdB2CService . isLogin ) . toHaveBeenCalled ( ) ;
86122 expect ( router . navigate ) . toHaveBeenCalledWith ( [ '' ] ) ;
87123 } ) ) ;
124+
125+ it ( 'should not sign-up or login with google if is already logged-in into the app Locally' , inject ( [ Router ] , ( router : Router ) => {
126+ spyOn ( loginService , 'isLogin' ) . and . returnValue ( true ) ;
127+ spyOn ( router , 'navigate' ) . and . stub ( ) ;
128+ component . loginWithGoogle ( ) ;
129+ expect ( loginService . isLogin ) . toHaveBeenCalled ( ) ;
130+ expect ( router . navigate ) . toHaveBeenCalledWith ( [ '' ] ) ;
131+ } ) ) ;
88132} ) ;
0 commit comments