I am trying to write a simple test for ActivatedRoute
. Here's what my test looks like.
it("should check if subscribes are called in init", () => {
const subRouteSpy = spyOn(activatedRouteStub.paramMap, "subscribe");
component.ngOnInit();
expect(subRouteSpy).toHaveBeenCalled();
});
My TestBed config
:
const activatedRouteStub = {
paramMap: {
subscribe() {
return of();
}
}
};
TestBed.configureTestingModule({
declarations: [HomeFilterDrawerComponent],
providers: [
{ provide: ActivatedRoute, useValue: activatedRouteStub }
],
imports: [
FormsModule,
StoreModule.forRoot(appReducers),
HttpClientTestingModule,
RouterTestingModule
]
}).compileComponents();
The test keeps failing giving me Expected spy subscribe to have been called.
Not sure what exactly I am doing wrong here.
The code inside ngOnInit
of the component.
this.route.paramMap.subscribe(params => {
if (params["params"].slug !== undefined) {
}
});