import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ; import { HttpClientTestingModule } from '@angular/common/http/testing' ; import { of } from 'rxjs' ; import { LogsComponent } from './logs.component' ; import { LogsService } from './logs.service' ; import { Log , LoggersResponse } from './log.model' ; describe ( 'LogsComponent' , ( ) => { let comp ; let fixture ; let service ; beforeEach ( waitForAsync ( ( ) => { TestBed . configureTestingModule ( { imports : [ HttpClientTestingModule ] , declarations : [ LogsComponent ] , providers : [ LogsService ] , } ) . overrideTemplate ( LogsComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( LogsComponent ) ; comp = fixture . componentInstance ; service = TestBed . inject ( LogsService ) ; } ) ; describe ( 'OnInit' , ( ) => { it ( 'should set all default values correctly' , ( ) => { expect ( comp . filter ) . toBe ( '' ) ; expect ( comp . orderProp ) . toBe ( 'name' ) ; expect ( comp . ascending ) . toBe ( true ) ; } ) ; it ( 'Should call load all on init' , ( ) => { const log = new Log ( 'main' , 'WARN' ) ; jest . spyOn ( service , 'findAll' ) . mockReturnValue ( of ( { loggers : { main : { effectiveLevel : 'WARN' , } , } , } as unknown as LoggersResponse ) ) ; comp . ngOnInit ( ) ; expect ( service . findAll ) . toHaveBeenCalled ( ) ; expect ( comp . loggers ?. [ 0 ] ) . toEqual ( expect . objectContaining ( log ) ) ; } ) ; } ) ; describe ( 'change log level' , ( ) => { it ( 'should change log level correctly' , ( ) => { const log = new Log ( 'main' , 'ERROR' ) ; jest . spyOn ( service , 'changeLevel' ) . mockReturnValue ( of ( { } ) ) ; jest . spyOn ( service , 'findAll' ) . mockReturnValue ( of ( { loggers : { main : { effectiveLevel : 'ERROR' , } , } , } as unknown as LoggersResponse ) ) ; comp . changeLevel ( 'main' , 'ERROR' ) ; expect ( service . changeLevel ) . toHaveBeenCalled ( ) ; expect ( service . findAll ) . toHaveBeenCalled ( ) ; expect ( comp . loggers ?. [ 0 ] ) . toEqual ( expect . objectContaining ( log ) ) ; } ) ; } ) ; } ) ;