import { ComponentFixture , TestBed , async , inject , fakeAsync , tick } from '@angular/core/testing' ; import { HttpResponse } from '@angular/common/http' ; import { FormBuilder } from '@angular/forms' ; import { ActivatedRoute } from '@angular/router' ; import { Observable , of } from 'rxjs' ; import { FrontendTestModule } from '../../../test.module' ; import { UserMgmtUpdateComponent } from 'app/admin/user-management/user-management-update.component' ; import { UserService , User , JhiLanguageHelper } from 'app/core' ; describe ( 'Component Tests' , ( ) => { describe ( 'User Management Update Component' , ( ) => { let comp ; let fixture ; let service ; let mockLanguageHelper ; const route = ( { data : of ( { user : new User ( 1 , 'user' , 'first' , 'last' , 'first@last.com' , true , 'en' , [ 'ROLE_USER' ] , 'admin' , null , null , null ) } ) } as any ) as ActivatedRoute ; beforeEach ( async ( ( ) => { TestBed . configureTestingModule ( { imports : [ FrontendTestModule ] , declarations : [ UserMgmtUpdateComponent ] , providers : [ FormBuilder , { provide : ActivatedRoute , useValue : route } ] } ) . overrideTemplate ( UserMgmtUpdateComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( UserMgmtUpdateComponent ) ; comp = fixture . componentInstance ; service = fixture . debugElement . injector . get ( UserService ) ; mockLanguageHelper = fixture . debugElement . injector . get ( JhiLanguageHelper ) ; } ) ; describe ( 'OnInit' , ( ) => { it ( 'Should load authorities and language on init' , inject ( [ ] , fakeAsync ( ( ) => { spyOn ( service , 'authorities' ) . and . returnValue ( of ( [ 'USER' ] ) ) ; comp . ngOnInit ( ) ; expect ( service . authorities ) . toHaveBeenCalled ( ) ; expect ( comp . authorities ) . toEqual ( [ 'USER' ] ) ; expect ( mockLanguageHelper . getAllSpy ) . toHaveBeenCalled ( ) ; } ) ) ) ; } ) ; describe ( 'save' , ( ) => { it ( 'Should call update service on save for existing user' , inject ( [ ] , fakeAsync ( ( ) => { const entity = new User ( 123 ) ; spyOn ( service , 'update' ) . and . returnValue ( of ( new HttpResponse ( { body : entity } ) ) ) ; comp . user = entity ; comp . editForm . patchValue ( { id : entity . id } ) ; comp . save ( ) ; tick ( ) ; expect ( service . update ) . toHaveBeenCalledWith ( entity ) ; expect ( comp . isSaving ) . toEqual ( false ) ; } ) ) ) ; it ( 'Should call create service on save for new user' , inject ( [ ] , fakeAsync ( ( ) => { const entity = new User ( ) ; spyOn ( service , 'create' ) . and . returnValue ( of ( new HttpResponse ( { body : entity } ) ) ) ; comp . user = entity ; comp . save ( ) ; tick ( ) ; expect ( service . create ) . toHaveBeenCalledWith ( entity ) ; expect ( comp . isSaving ) . toEqual ( false ) ; } ) ) ) ; } ) ; } ) ; } ) ;