import { ComponentFixture , TestBed , async } from '@angular/core/testing' ; import { FormBuilder } from '@angular/forms' ; import { Observable , throwError } from 'rxjs' ; import { FrontendTestModule } from '../../../test.module' ; import { AccountService , Account } from 'app/core' ; import { SettingsComponent } from 'app/account/settings/settings.component' ; describe ( 'Component Tests' , ( ) => { describe ( 'SettingsComponent' , ( ) => { let comp ; let fixture ; let mockAuth ; beforeEach ( async ( ( ) => { TestBed . configureTestingModule ( { imports : [ FrontendTestModule ] , declarations : [ SettingsComponent ] , providers : [ FormBuilder ] } ) . overrideTemplate ( SettingsComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( SettingsComponent ) ; comp = fixture . componentInstance ; mockAuth = fixture . debugElement . injector . get ( AccountService ) ; } ) ; it ( 'should send the current identity upon save' , ( ) => { const accountValues = { firstName : 'John' , lastName : 'Doe' , activated : true , email : 'john.doe@mail.com' , langKey : 'en' , login : 'john' } ; mockAuth . setIdentityResponse ( accountValues ) ; comp . updateForm ( accountValues ) ; comp . save ( ) ; expect ( mockAuth . identitySpy ) . toHaveBeenCalled ( ) ; expect ( mockAuth . saveSpy ) . toHaveBeenCalledWith ( accountValues ) ; expect ( comp . settingsForm . value ) . toEqual ( accountValues ) ; } ) ; it ( 'should notify of success upon successful save' , ( ) => { const accountValues = { firstName : 'John' , lastName : 'Doe' } ; comp . settingsForm . patchValue ( { firstName : 'John' , lastName : 'Doe' } ) ; mockAuth . setIdentityResponse ( accountValues ) ; comp . save ( ) ; expect ( comp . error ) . toBeNull ( ) ; expect ( comp . success ) . toBe ( 'OK' ) ; } ) ; it ( 'should notify of error upon failed save' , ( ) => { mockAuth . saveSpy . and . returnValue ( throwError ( 'ERROR' ) ) ; comp . save ( ) ; expect ( comp . error ) . toEqual ( 'ERROR' ) ; expect ( comp . success ) . toBeNull ( ) ; } ) ; } ) ; } ) ;