jest . mock ( 'app/core/auth/account.service' ) ; import { ComponentFixture , TestBed , waitForAsync , inject , fakeAsync , tick } from '@angular/core/testing' ; import { HttpHeaders , HttpResponse } from '@angular/common/http' ; import { HttpClientTestingModule } from '@angular/common/http/testing' ; import { ActivatedRoute } from '@angular/router' ; import { RouterTestingModule } from '@angular/router/testing' ; import { of } from 'rxjs' ; import { UserManagementService } from '../service/user-management.service' ; import { User } from '../user-management.model' ; import { AccountService } from 'app/core/auth/account.service' ; import { UserManagementComponent } from './user-management.component' ; describe ( 'User Management Component' , ( ) => { let comp ; let fixture ; let service ; let mockAccountService ; const data = of ( { defaultSort : 'id,asc' , } ) ; const queryParamMap = of ( jest . requireActual ( '@angular/router' ) . convertToParamMap ( { page : '1' , size : '1' , sort : 'id,desc' , } ) ) ; beforeEach ( waitForAsync ( ( ) => { TestBed . configureTestingModule ( { imports : [ HttpClientTestingModule , RouterTestingModule . withRoutes ( [ ] ) ] , declarations : [ UserManagementComponent ] , providers : [ { provide : ActivatedRoute , useValue : { data , queryParamMap } } , AccountService ] , } ) . overrideTemplate ( UserManagementComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( UserManagementComponent ) ; comp = fixture . componentInstance ; service = TestBed . inject ( UserManagementService ) ; mockAccountService = TestBed . inject ( AccountService ) ; mockAccountService . identity = jest . fn ( ( ) => of ( null ) ) ; } ) ; describe ( 'OnInit' , ( ) => { it ( 'Should call load all on init' , inject ( [ ] , fakeAsync ( ( ) => { const headers = new HttpHeaders ( ) . append ( 'link' , 'link;link' ) ; jest . spyOn ( service , 'query' ) . mockReturnValue ( of ( new HttpResponse ( { body : [ new User ( 123 ) ] , headers , } ) ) ) ; comp . ngOnInit ( ) ; tick ( ) ; expect ( service . query ) . toHaveBeenCalled ( ) ; expect ( comp . users ?. [ 0 ] ) . toEqual ( expect . objectContaining ( { id : 123 } ) ) ; } ) ) ) ; } ) ; describe ( 'setActive' , ( ) => { it ( 'Should update user and call load all' , inject ( [ ] , fakeAsync ( ( ) => { const headers = new HttpHeaders ( ) . append ( 'link' , 'link;link' ) ; const user = new User ( 123 ) ; jest . spyOn ( service , 'query' ) . mockReturnValue ( of ( new HttpResponse ( { body : [ user ] , headers , } ) ) ) ; jest . spyOn ( service , 'update' ) . mockReturnValue ( of ( user ) ) ; comp . setActive ( user , true ) ; tick ( ) ; expect ( service . update ) . toHaveBeenCalledWith ( { ... user , activated : true } ) ; expect ( service . query ) . toHaveBeenCalled ( ) ; expect ( comp . users ?. [ 0 ] ) . toEqual ( expect . objectContaining ( { id : 123 } ) ) ; } ) ) ) ; } ) ; } ) ;