I need help in order to write unit tests for router.events.subscribe. I used ActivationEnd to use snapshot object which has properties which I need for my application.
Below is my code snippet.
In constructor,
this.router.events.subscribe(e => this.onRouterEvent(e))
onRouterEvent(e: Event): void {
if (e instanceof ActivationEnd) {
this.section = e.snapshot.data.title;
this.dynamicSubNavLabel = e.snapshot.routeConfig?.data?.label;
}
}
Unit test File
class ActivationEnd {
snapshot: any = {
data: {
title: 'a title'
},
routeConfig: {
data: {
label: 'a label'
}
},
params: {
orderID: "3525",
projectID: "24",
usecaseID: "3"
}
};
}
class MockRouter {
events: Observable<Event> = new Observable(observer => {
observer.next(new ActivationEnd()); // This does NOT make the condition true (e instance of ActivationEnd)
observer.complete();
});
get url(): string { return 'asd'; }
navigateByUrl(url: string) { return url; }
navigate(path: string) { return path; }
}
and using MockRouter class in providers
TestBed.configureTestingModule({
declarations: [AppComponent],
imports: [
RouterTestingModule
],
providers: [
{ provide: Router, useClass: MockRouter },
],
schemas: [NO_ERRORS_SCHEMA]
})
Can you please help me how can I mock ActivationEnd event and pass it to onRouterEvent() so that if conditions evaluates to be true ?
Regards, Alpesh