jest . mock ( 'app/core/auth/account.service' ) ; import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ; import { HttpClientTestingModule } from '@angular/common/http/testing' ; import { FormBuilder } from '@angular/forms' ; import { throwError , of } from 'rxjs' ; import { TranslateModule } from '@ngx-translate/core' ; import { AccountService } from 'app/core/auth/account.service' ; import { Account } from 'app/core/auth/account.model' ; import { SettingsComponent } from './settings.component' ; describe ( 'SettingsComponent' , ( ) => { let comp ; let fixture ; let mockAccountService ; const account = { firstName : 'John' , lastName : 'Doe' , activated : true , email : 'john.doe@mail.com' , langKey : 'en' , login : 'john' , authorities : [ ] , imageUrl : '' , } ; beforeEach ( waitForAsync ( ( ) => { TestBed . configureTestingModule ( { imports : [ TranslateModule . forRoot ( ) , HttpClientTestingModule ] , declarations : [ SettingsComponent ] , providers : [ FormBuilder , AccountService ] , } ) . overrideTemplate ( SettingsComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( SettingsComponent ) ; comp = fixture . componentInstance ; mockAccountService = TestBed . inject ( AccountService ) ; mockAccountService . identity = jest . fn ( ( ) => of ( account ) ) ; mockAccountService . getAuthenticationState = jest . fn ( ( ) => of ( account ) ) ; } ) ; it ( 'should send the current identity upon save' , ( ) => { mockAccountService . save = jest . fn ( ( ) => of ( { } ) ) ; const settingsFormValues = { firstName : 'John' , lastName : 'Doe' , email : 'john.doe@mail.com' , langKey : 'en' , } ; comp . ngOnInit ( ) ; comp . save ( ) ; expect ( mockAccountService . identity ) . toHaveBeenCalled ( ) ; expect ( mockAccountService . save ) . toHaveBeenCalledWith ( account ) ; expect ( mockAccountService . authenticate ) . toHaveBeenCalledWith ( account ) ; expect ( comp . settingsForm . value ) . toEqual ( settingsFormValues ) ; } ) ; it ( 'should notify of success upon successful save' , ( ) => { mockAccountService . save = jest . fn ( ( ) => of ( { } ) ) ; comp . ngOnInit ( ) ; comp . save ( ) ; expect ( comp . success ) . toBe ( true ) ; } ) ; it ( 'should notify of error upon failed save' , ( ) => { mockAccountService . save = jest . fn ( ( ) => throwError ( 'ERROR' ) ) ; comp . ngOnInit ( ) ; comp . save ( ) ; expect ( comp . success ) . toBe ( false ) ; } ) ; } ) ;