Here is the code from component that i have written it has simple code to fetch the api data and console here.
Here is the test case i have tried using tick and fakeasync as well still no use.but when i remove filter from the component code,code coverage is happening.
sample project test case
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs';
import { ApiService, API_DATA } from './api.service';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
let apiService: jasmine.SpyObj<ApiService>;
beforeEach(async () => {
apiService = jasmine.createSpyObj('ApiService', ['getData','filter']);
apiService.getData.and.returnValue(of());
await TestBed.configureTestingModule({
imports: [
RouterTestingModule, HttpClientTestingModule
],
declarations: [
AppComponent
],
providers: [{ provide: ApiService, useValue: apiService }]
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have as title 'free-case'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('free-case');
});
it('should render title', fakeAsync(() => {
const fixture = TestBed.createComponent(AppComponent);
const response: API_DATA[] = [{
login: '',
id: 1,
node_id: '',
url: '',
repos_url: '',
events_url: '',
hooks_url: '',
issues_url: '',
members_url: '',
public_members_url: '',
avatar_url: '',
description: '',
}];
apiService.getData.and.returnValue(of(response));
fixture.detectChanges();
expect(apiService.getData).toHaveBeenCalled()
}));
});
Realtime poject test case :
it('should call the getDealState THRU_DATES', fakeAsync(() => {
dealService = jasmine.createSpyObj('DealService', ['getGridTogglesState', 'getDealRequest', 'getDealState', 'setDealRequest','filter']);
const data:ThruDates ={
digital: {
firstParty: '',
thirdParty: '',
},
linearRatings: {
commercial: '',
program: ''
}
}
const fixture = TestBed.createComponent(GridViewComponent);
dealService.getDealState.withArgs(STATE.THRU_DATES).and.returnValue(of(data));
dealService.getDealState(STATE.THRU_DATES).pipe(filter(item => Boolean(item))).subscribe((item:ThruDates) => {
expect(item).toEqual(data);
})
tick(2000)
fixture.detectChanges();
expect(dealService.getDealState).toHaveBeenCalledTimes(1)
})) ```