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. No other events from router provide these so I have to stick to this ActivationEnd. But I did not get anything so far as to how to mock this snapshot object.
Below is my code snippet.
ngOnInit() {
this.router.events.subscribe(e => {
if (e instanceof ActivationEnd) {
this.section = e.snapshot.data.title;
this.dynamicSubNavLabel = e.snapshot.routeConfig?.data?.label;
}
});
}
Can you please help me what could I do to achieve unit tests for this or any alternatives ?
Regards, Alpesh
Let say, you have a component like this:
Because
Router.eventsis aSubject<Event>already. We can do this:Subject<Event>then callnextto emit our custom/mock ActivationEnd Event.Source code from Angular: https://github.com/angular/angular/blob/75a3c778b1f7be913f0287423d40fade68ee9adc/packages/router/src/router.ts#L461