jest . mock ( 'app/core/auth/account.service' ) ; jest . mock ( 'app/login/login.service' ) ; import { ElementRef } from '@angular/core' ; import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ; import { FormBuilder } from '@angular/forms' ; import { Router , Navigation } from '@angular/router' ; import { RouterTestingModule } from '@angular/router/testing' ; import { of , throwError } from 'rxjs' ; import { AccountService } from 'app/core/auth/account.service' ; import { LoginService } from './login.service' ; import { LoginComponent } from './login.component' ; describe ( 'LoginComponent' , ( ) => { let comp ; let fixture ; let mockRouter ; let mockAccountService ; let mockLoginService ; beforeEach ( waitForAsync ( ( ) => { TestBed . configureTestingModule ( { imports : [ RouterTestingModule . withRoutes ( [ ] ) ] , declarations : [ LoginComponent ] , providers : [ FormBuilder , AccountService , { provide : LoginService , useValue : { login : jest . fn ( ( ) => of ( { } ) ) , } , } , ] , } ) . overrideTemplate ( LoginComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( LoginComponent ) ; comp = fixture . componentInstance ; mockRouter = TestBed . inject ( Router ) ; jest . spyOn ( mockRouter , 'navigate' ) . mockImplementation ( ( ) => Promise . resolve ( true ) ) ; mockLoginService = TestBed . inject ( LoginService ) ; mockAccountService = TestBed . inject ( AccountService ) ; } ) ; describe ( 'ngOnInit' , ( ) => { it ( 'Should call accountService.identity on Init' , ( ) => { mockAccountService . identity = jest . fn ( ( ) => of ( null ) ) ; mockAccountService . getAuthenticationState = jest . fn ( ( ) => of ( null ) ) ; comp . ngOnInit ( ) ; expect ( mockAccountService . identity ) . toHaveBeenCalled ( ) ; } ) ; it ( 'Should call accountService.isAuthenticated on Init' , ( ) => { mockAccountService . identity = jest . fn ( ( ) => of ( null ) ) ; comp . ngOnInit ( ) ; expect ( mockAccountService . isAuthenticated ) . toHaveBeenCalled ( ) ; } ) ; it ( 'should navigate to home page on Init if authenticated=true' , ( ) => { mockAccountService . identity = jest . fn ( ( ) => of ( null ) ) ; mockAccountService . getAuthenticationState = jest . fn ( ( ) => of ( null ) ) ; mockAccountService . isAuthenticated = ( ) => true ; comp . ngOnInit ( ) ; expect ( mockRouter . navigate ) . toHaveBeenCalledWith ( [ '' ] ) ; } ) ; } ) ; describe ( 'ngAfterViewInit' , ( ) => { it ( 'shoult set focus to username input after the view has been initialized' , ( ) => { const node = { focus : jest . fn ( ) , } ; comp . username = new ElementRef ( node ) ; comp . ngAfterViewInit ( ) ; expect ( node . focus ) . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'login' , ( ) => { it ( 'should authenticate the user and navigate to home page' , ( ) => { const credentials = { username : 'admin' , password : 'admin' , rememberMe : true , } ; comp . loginForm . patchValue ( { username : 'admin' , password : 'admin' , rememberMe : true , } ) ; comp . login ( ) ; expect ( comp . authenticationError ) . toEqual ( false ) ; expect ( mockLoginService . login ) . toHaveBeenCalledWith ( credentials ) ; expect ( mockRouter . navigate ) . toHaveBeenCalledWith ( [ '' ] ) ; } ) ; it ( 'should authenticate the user but not navigate to home page if authentication process is already routing to cached url from localstorage' , ( ) => { jest . spyOn ( mockRouter , 'getCurrentNavigation' ) . mockReturnValue ( { } as Navigation ) ; comp . login ( ) ; expect ( comp . authenticationError ) . toEqual ( false ) ; expect ( mockRouter . navigate ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'should stay on login form and show error message on login error' , ( ) => { mockLoginService . login = jest . fn ( ( ) => throwError ( { } ) ) ; comp . login ( ) ; expect ( comp . authenticationError ) . toEqual ( true ) ; expect ( mockRouter . navigate ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; } ) ;