import { ComponentFixture , TestBed } from '@angular/core/testing' ; import { HttpHeaders , HttpResponse } from '@angular/common/http' ; import { HttpClientTestingModule } from '@angular/common/http/testing' ; import { of } from 'rxjs' ; import { OperationService } from '../service/operation.service' ; import { OperationComponent } from './operation.component' ; describe ( 'Operation Management Component' , ( ) => { let comp ; let fixture ; let service ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ HttpClientTestingModule ] , declarations : [ OperationComponent ] , } ) . overrideTemplate ( OperationComponent , '' ) . compileComponents ( ) ; fixture = TestBed . createComponent ( OperationComponent ) ; comp = fixture . componentInstance ; service = TestBed . inject ( OperationService ) ; const headers = new HttpHeaders ( ) ; jest . spyOn ( service , 'query' ) . mockReturnValue ( of ( new HttpResponse ( { body : [ { id : 123 } ] , headers , } ) ) ) ; } ) ; it ( 'Should call load all on init' , ( ) => { comp . ngOnInit ( ) ; expect ( service . query ) . toHaveBeenCalled ( ) ; expect ( comp . operations [ 0 ] ) . toEqual ( expect . objectContaining ( { id : 123 } ) ) ; } ) ; it ( 'should load a page' , ( ) => { comp . loadPage ( 1 ) ; expect ( service . query ) . toHaveBeenCalled ( ) ; expect ( comp . operations [ 0 ] ) . toEqual ( expect . objectContaining ( { id : 123 } ) ) ; } ) ; it ( 'should calculate the sort attribute for an id' , ( ) => { comp . ngOnInit ( ) ; expect ( service . query ) . toHaveBeenCalledWith ( expect . objectContaining ( { sort : [ 'id,asc' ] } ) ) ; } ) ; it ( 'should calculate the sort attribute for a non-id attribute' , ( ) => { comp . ngOnInit ( ) ; comp . predicate = 'name' ; comp . loadPage ( 1 ) ; expect ( service . query ) . toHaveBeenLastCalledWith ( expect . objectContaining ( { sort : [ 'name,asc' , 'id' ] } ) ) ; } ) ; it ( 'should re-initialize the page' , ( ) => { comp . loadPage ( 1 ) ; comp . reset ( ) ; expect ( comp . page ) . toEqual ( 0 ) ; expect ( service . query ) . toHaveBeenCalledTimes ( 2 ) ; expect ( comp . operations [ 0 ] ) . toEqual ( expect . objectContaining ( { id : 123 } ) ) ; } ) ; } ) ;