import { ComponentFixture , TestBed } from '@angular/core/testing' ; import { HttpResponse } from '@angular/common/http' ; import { HttpClientTestingModule } from '@angular/common/http/testing' ; import { FormBuilder } from '@angular/forms' ; import { ActivatedRoute } from '@angular/router' ; import { RouterTestingModule } from '@angular/router/testing' ; import { of , Subject , from } from 'rxjs' ; import { BankAccountService } from '../service/bank-account.service' ; import { IBankAccount , BankAccount } from '../bank-account.model' ; import { IUser } from 'app/entities/user/user.model' ; import { UserService } from 'app/entities/user/user.service' ; import { BankAccountUpdateComponent } from './bank-account-update.component' ; describe ( 'BankAccount Management Update Component' , ( ) => { let comp ; let fixture ; let activatedRoute ; let bankAccountService ; let userService ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ HttpClientTestingModule , RouterTestingModule . withRoutes ( [ ] ) ] , declarations : [ BankAccountUpdateComponent ] , providers : [ FormBuilder , { provide : ActivatedRoute , useValue : { params : from ( [ { } ] ) , } , } , ] , } ) . overrideTemplate ( BankAccountUpdateComponent , '' ) . compileComponents ( ) ; fixture = TestBed . createComponent ( BankAccountUpdateComponent ) ; activatedRoute = TestBed . inject ( ActivatedRoute ) ; bankAccountService = TestBed . inject ( BankAccountService ) ; userService = TestBed . inject ( UserService ) ; comp = fixture . componentInstance ; } ) ; describe ( 'ngOnInit' , ( ) => { it ( 'Should call User query and add missing value' , ( ) => { const bankAccount = { id : 456 } ; const user = { id : 39533 } ; bankAccount . user = user ; const userCollection = [ { id : 39416 } ] ; jest . spyOn ( userService , 'query' ) . mockReturnValue ( of ( new HttpResponse ( { body : userCollection } ) ) ) ; const additionalUsers = [ user ] ; const expectedCollection = [ ... additionalUsers , ... userCollection ] ; jest . spyOn ( userService , 'addUserToCollectionIfMissing' ) . mockReturnValue ( expectedCollection ) ; activatedRoute . data = of ( { bankAccount } ) ; comp . ngOnInit ( ) ; expect ( userService . query ) . toHaveBeenCalled ( ) ; expect ( userService . addUserToCollectionIfMissing ) . toHaveBeenCalledWith ( userCollection , ... additionalUsers ) ; expect ( comp . usersSharedCollection ) . toEqual ( expectedCollection ) ; } ) ; it ( 'Should update editForm' , ( ) => { const bankAccount = { id : 456 } ; const user = { id : 90365 } ; bankAccount . user = user ; activatedRoute . data = of ( { bankAccount } ) ; comp . ngOnInit ( ) ; expect ( comp . editForm . value ) . toEqual ( expect . objectContaining ( bankAccount ) ) ; expect ( comp . usersSharedCollection ) . toContain ( user ) ; } ) ; } ) ; describe ( 'save' , ( ) => { it ( 'Should call update service on save for existing entity' , ( ) => { const saveSubject = new Subject < HttpResponse < BankAccount > > ( ) ; const bankAccount = { id : 123 } ; jest . spyOn ( bankAccountService , 'update' ) . mockReturnValue ( saveSubject ) ; jest . spyOn ( comp , 'previousState' ) ; activatedRoute . data = of ( { bankAccount } ) ; comp . ngOnInit ( ) ; comp . save ( ) ; expect ( comp . isSaving ) . toEqual ( true ) ; saveSubject . next ( new HttpResponse ( { body : bankAccount } ) ) ; saveSubject . complete ( ) ; expect ( comp . previousState ) . toHaveBeenCalled ( ) ; expect ( bankAccountService . update ) . toHaveBeenCalledWith ( bankAccount ) ; expect ( comp . isSaving ) . toEqual ( false ) ; } ) ; it ( 'Should call create service on save for new entity' , ( ) => { const saveSubject = new Subject < HttpResponse < BankAccount > > ( ) ; const bankAccount = new BankAccount ( ) ; jest . spyOn ( bankAccountService , 'create' ) . mockReturnValue ( saveSubject ) ; jest . spyOn ( comp , 'previousState' ) ; activatedRoute . data = of ( { bankAccount } ) ; comp . ngOnInit ( ) ; comp . save ( ) ; expect ( comp . isSaving ) . toEqual ( true ) ; saveSubject . next ( new HttpResponse ( { body : bankAccount } ) ) ; saveSubject . complete ( ) ; expect ( bankAccountService . create ) . toHaveBeenCalledWith ( bankAccount ) ; expect ( comp . isSaving ) . toEqual ( false ) ; expect ( comp . previousState ) . toHaveBeenCalled ( ) ; } ) ; it ( 'Should set isSaving to false on error' , ( ) => { const saveSubject = new Subject < HttpResponse < BankAccount > > ( ) ; const bankAccount = { id : 123 } ; jest . spyOn ( bankAccountService , 'update' ) . mockReturnValue ( saveSubject ) ; jest . spyOn ( comp , 'previousState' ) ; activatedRoute . data = of ( { bankAccount } ) ; comp . ngOnInit ( ) ; comp . save ( ) ; expect ( comp . isSaving ) . toEqual ( true ) ; saveSubject . error ( 'This is an error!' ) ; expect ( bankAccountService . update ) . toHaveBeenCalledWith ( bankAccount ) ; expect ( comp . isSaving ) . toEqual ( false ) ; expect ( comp . previousState ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'Tracking relationships identifiers' , ( ) => { describe ( 'trackUserById' , ( ) => { it ( 'Should return tracked User primary key' , ( ) => { const entity = { id : 123 } ; const trackResult = comp . trackUserById ( 0 , entity ) ; expect ( trackResult ) . toEqual ( entity . id ) ; } ) ; } ) ; } ) ; } ) ;