I am currently working on testing my routerOnActivate method to ensure it is being called correctly with parameters. However, I am having trouble finding documentation on how to achieve this.
For the sake of simplicity, let's consider the method as follows:
routerOnActivate(curr: RouteSegment): void {
this.uniqueId = curr.getParam('uniqueId');
}
and I am looking to implement something like the following test:
it('should work', inject([TestComponentBuilder, MockBackend], (tcb: TestComponentBuilder, mockBackend: MockBackend) => {
tcb.createAsync(TestComponent)
.then((rootTC:any) => {
let componentInstance = rootTC.debugElement.children[0].componentInstance;
spyOn(componentInstance, 'routerOnActivate');
mockBackend.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(new ResponseOptions({
status: 200,
body: JSON.stringify({message:'found'})
})));
});
rootTC.detectChanges();
expect(componentInstance.routerOnActivate).toHaveBeenCalled();
expect(componentInstance.uniqueId).toBe(123);
});
}));