import { ComponentFixture , TestBed , async , inject , fakeAsync , tick } from '@angular/core/testing' ; import { FormBuilder } from '@angular/forms' ; import { Router } from '@angular/router' ; import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap' ; import { JhiEventManager } from 'ng-jhipster' ; import { LoginService } from 'app/core/login/login.service' ; import { JhiLoginModalComponent } from 'app/shared/login/login.component' ; import { StateStorageService } from 'app/core/auth/state-storage.service' ; import { FrontendTestModule } from '../../../test.module' ; import { MockLoginService } from '../../../helpers/mock-login.service' ; import { MockStateStorageService } from '../../../helpers/mock-state-storage.service' ; describe ( 'Component Tests' , ( ) => { describe ( 'LoginComponent' , ( ) => { let comp ; let fixture ; let mockLoginService ; let mockStateStorageService ; let mockRouter ; let mockEventManager ; let mockActiveModal ; beforeEach ( async ( ( ) => { TestBed . configureTestingModule ( { imports : [ FrontendTestModule ] , declarations : [ JhiLoginModalComponent ] , providers : [ FormBuilder , { provide : LoginService , useClass : MockLoginService } , { provide : StateStorageService , useClass : MockStateStorageService } ] } ) . overrideTemplate ( JhiLoginModalComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( JhiLoginModalComponent ) ; comp = fixture . componentInstance ; mockLoginService = fixture . debugElement . injector . get ( LoginService ) ; mockStateStorageService = fixture . debugElement . injector . get ( StateStorageService ) ; mockRouter = fixture . debugElement . injector . get ( Router ) ; mockEventManager = fixture . debugElement . injector . get ( JhiEventManager ) ; mockActiveModal = fixture . debugElement . injector . get ( NgbActiveModal ) ; } ) ; it ( 'should authenticate the user upon login when previous state was set' , inject ( [ ] , fakeAsync ( ( ) => { const credentials = { username : 'admin' , password : 'admin' , rememberMe : true } ; comp . loginForm . patchValue ( { username : 'admin' , password : 'admin' , rememberMe : true } ) ; mockLoginService . setResponse ( { } ) ; mockStateStorageService . setResponse ( 'admin/users?page=0' ) ; comp . login ( ) ; tick ( ) ; expect ( comp . authenticationError ) . toEqual ( false ) ; expect ( mockActiveModal . dismissSpy ) . toHaveBeenCalledWith ( 'login success' ) ; expect ( mockEventManager . broadcastSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( mockLoginService . loginSpy ) . toHaveBeenCalledWith ( credentials ) ; expect ( mockStateStorageService . getUrlSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( mockStateStorageService . storeUrlSpy ) . toHaveBeenCalledWith ( null ) ; expect ( mockRouter . navigateByUrlSpy ) . toHaveBeenCalledWith ( 'admin/users?page=0' ) ; } ) ) ) ; it ( 'should authenticate the user upon login when previous state was not set' , inject ( [ ] , fakeAsync ( ( ) => { const credentials = { username : 'admin' , password : 'admin' , rememberMe : true } ; comp . loginForm . patchValue ( { username : 'admin' , password : 'admin' , rememberMe : true } ) ; mockLoginService . setResponse ( { } ) ; mockStateStorageService . setResponse ( null ) ; comp . login ( ) ; tick ( ) ; expect ( comp . authenticationError ) . toEqual ( false ) ; expect ( mockActiveModal . dismissSpy ) . toHaveBeenCalledWith ( 'login success' ) ; expect ( mockEventManager . broadcastSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( mockLoginService . loginSpy ) . toHaveBeenCalledWith ( credentials ) ; expect ( mockStateStorageService . getUrlSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( mockStateStorageService . storeUrlSpy ) . not . toHaveBeenCalled ( ) ; expect ( mockRouter . navigateSpy ) . not . toHaveBeenCalled ( ) ; } ) ) ) ; it ( 'should empty the credentials upon cancel' , ( ) => { comp . loginForm . patchValue ( { username : 'admin' , password : 'admin' } ) ; const expected = { username : '' , password : '' , rememberMe : true } ; comp . cancel ( ) ; expect ( comp . authenticationError ) . toEqual ( false ) ; expect ( comp . loginForm . get ( 'username' ) . value ) . toEqual ( expected . username ) ; expect ( comp . loginForm . get ( 'password' ) . value ) . toEqual ( expected . password ) ; expect ( comp . loginForm . get ( 'rememberMe' ) . value ) . toEqual ( expected . rememberMe ) ; expect ( mockActiveModal . dismissSpy ) . toHaveBeenCalledWith ( 'cancel' ) ; } ) ; it ( 'should redirect user when register' , ( ) => { comp . register ( ) ; expect ( mockActiveModal . dismissSpy ) . toHaveBeenCalledWith ( 'to state register' ) ; expect ( mockRouter . navigateSpy ) . toHaveBeenCalledWith ( [ '/register' ] ) ; } ) ; it ( 'should redirect user when request password' , ( ) => { comp . requestResetPassword ( ) ; expect ( mockActiveModal . dismissSpy ) . toHaveBeenCalledWith ( 'to state requestReset' ) ; expect ( mockRouter . navigateSpy ) . toHaveBeenCalledWith ( [ '/reset' , 'request' ] ) ; } ) ; } ) ; } ) ;