import { ComponentFixture , TestBed , async } from '@angular/core/testing' ; import { HttpResponse } from '@angular/common/http' ; import { FormBuilder } from '@angular/forms' ; import { Observable , of , throwError } from 'rxjs' ; import { FrontendTestModule } from '../../../test.module' ; import { PasswordComponent } from 'app/account/password/password.component' ; import { PasswordService } from 'app/account/password/password.service' ; describe ( 'Component Tests' , ( ) => { describe ( 'PasswordComponent' , ( ) => { let comp ; let fixture ; let service ; beforeEach ( async ( ( ) => { TestBed . configureTestingModule ( { imports : [ FrontendTestModule ] , declarations : [ PasswordComponent ] , providers : [ FormBuilder ] } ) . overrideTemplate ( PasswordComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( PasswordComponent ) ; comp = fixture . componentInstance ; service = fixture . debugElement . injector . get ( PasswordService ) ; } ) ; it ( 'should show error if passwords do not match' , ( ) => { comp . passwordForm . patchValue ( { newPassword : 'password1' , confirmPassword : 'password2' } ) ; comp . changePassword ( ) ; expect ( comp . doNotMatch ) . toBe ( 'ERROR' ) ; expect ( comp . error ) . toBeNull ( ) ; expect ( comp . success ) . toBeNull ( ) ; } ) ; it ( 'should call Auth.changePassword when passwords match' , ( ) => { const passwordValues = { currentPassword : 'oldPassword' , newPassword : 'myPassword' } ; spyOn ( service , 'save' ) . and . returnValue ( of ( new HttpResponse ( { body : true } ) ) ) ; comp . passwordForm . patchValue ( { currentPassword : passwordValues . currentPassword , newPassword : passwordValues . newPassword , confirmPassword : passwordValues . newPassword } ) ; comp . changePassword ( ) ; expect ( service . save ) . toHaveBeenCalledWith ( passwordValues . newPassword , passwordValues . currentPassword ) ; } ) ; it ( 'should set success to OK upon success' , function ( ) { spyOn ( service , 'save' ) . and . returnValue ( of ( new HttpResponse ( { body : true } ) ) ) ; comp . passwordForm . patchValue ( { newPassword : 'myPassword' , confirmPassword : 'myPassword' } ) ; comp . changePassword ( ) ; expect ( comp . doNotMatch ) . toBeNull ( ) ; expect ( comp . error ) . toBeNull ( ) ; expect ( comp . success ) . toBe ( 'OK' ) ; } ) ; it ( 'should notify of error if change password fails' , function ( ) { spyOn ( service , 'save' ) . and . returnValue ( throwError ( 'ERROR' ) ) ; comp . passwordForm . patchValue ( { newPassword : 'myPassword' , confirmPassword : 'myPassword' } ) ; comp . changePassword ( ) ; expect ( comp . doNotMatch ) . toBeNull ( ) ; expect ( comp . success ) . toBeNull ( ) ; expect ( comp . error ) . toBe ( 'ERROR' ) ; } ) ; } ) ; } ) ;