I have a component, inside ngOninit, I am dispatching an action which is being handled by ngrx/effect to get some data from service API. I am writing unit tests and want to mock the service call in ngOninit so that the original API is not called. But still the original API is being called, maybe because it was handled from effects. How should I handle this so that I stop actual API call and call mocked service API instead?
Component Code:
ngOnInit() {
this.sectionId$ = this.store.select(reducers.getSectionIDFromAppState).subscribe((sectionID) => {
if (sectionID) {
this.store.dispatch(new layoutActions.GetLogFormatListAction(sectionID));
}
}
Action Code:
export class GetLogFormatListAction implements Action {
type = ActionTypes.GET_LOG_FORMAT_LIST;
constructor(public payload?: any) {
this.payload = payload;
}
}
Effects Code:
@Effect()
getLogFormatList$: Observable<Action> = this.actions$
.ofType(layoutActions.ActionTypes.GET_LOG_FORMAT_LIST)
.mergeMap((action: any) => {
return this.layoutService.getLogFormatList(action.payload)
.map(logFormatList => new layoutActions.GetLogFormatListSuccessAction(logFormatList))
.catch(error => Observable.of(new layoutActions.GetLogFormatListFailureAction(error)));
});
Layout.service.ts
getLogFormatList(sectionId): Observable<any> {
const requestData = {
endpoint: environment.endpoints.GET_LOG_FORMAT_LIST.endpoint + sectionId,
params: {
}
};
return this.httpRequestWrapperService.fetchData(requestData); }
My Spec code:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
LogviewerWidgetConfigComponent,
MockComponent({ selector: 'app-logviewer-config-template', inputs: ['logformatList', 'widgetConfig' , 'index'] }),
MockComponent({ selector: 'app-widget-config-header', inputs: ['model'], outputs: ['closeWidgetConfig'] }),
],
imports: [FormsModule, StoreModule.forRoot(reducers)],
providers: [{ provide: LayoutService, useClass: MockLayoutService }]
}).compileComponents(); }));
beforeEach(() => {
store = TestBed.get(Store);
spyOn(store, 'dispatch').and.callThrough();
fixture = TestBed.createComponent(LogviewerWidgetConfigComponent);
component = fixture.componentInstance;
fixture.detectChanges(); });
Mocked service:
class MockLayoutService {
getLogFormatList(param: any): Observable<any> {
return Observable.of(null);
}
}