Currently working on beefing up the code coverage on some Angular code that I didn't write.
In the component we have something like
public onClick(value: any) {
...
this.someService.SomeServiceFunction(..., () => {
this.someService.SomeOtherServiceFunction();
});
}
with SomeServiceFunction declared in the service along the lines of
SomeServiceFunction(... callback: ()=> void = () => {})
{ ... }
Now in the test we have the usual
providers: [
{ provide: OurService, useValue: fakeServiceSpy }, ...
and then in the test
fakeServiceSpy.SomeServiceFunction.and.returnValue(of(true));
which is working fine in terms of getting the test to run to completion, but is there a way for the fake to actually invoke the line in the lambda above so that it can get covered?
I tried playing around with callFake
but could not get it to work in this context.
I'm aware that I can isolate the code from the lambda in its own function and call that explicitly from the test, but that would require changes to the component, and I'd like to avoid that if possible.
Would something like this suit your needs ?
let capturedCallback;
fakeServiceSpy.SomeServiceFunction.and.callFake( (... callback) =>
capturedCallback = callback;
return of(true);
});
// Now that you setup the fake call you can call the function which in turn
// is gonna call the spy's function.
// ex:
testedService.onClick();
// After that your callback is in capturedCallback and you can test it however you'd like
Wrap in fakeAsync()
if needed.