I think I probably should use a spyObject and I am on the wrong track but is it possible to mock a static function with useClass in Jasmine Angular testing?
The code I am testing is:
this.authenticationContext.onClaimsChange().subscribe((claims) => {
if (!AuthenticationContext.isAzureIdentity(claims))
return;
....
What I have done is:
created a fake class:
class FakeAuthenticationContext {
private fake: { [claim: string]: string | string[] } = {claim:['fake']};
onClaimsChange(): Observable<{ [claim: string]: string | string[] }>
{
console.info('at onClaimsChange');
return of(this.fake)
}
static isAzureIdentity(claims: { [claim: string]: string | string[] }): boolean {
console.info('at isAzureIdentity');
return (true)
}
}
and in the providers for the tests I added:
providers: [
...
{provide: AuthenticationContext, useClass: FakeAuthenticationContext}
]
But it does not work because the code in the component will look for the static function 'AuthenticationContext.isAzureIdentity' not 'FakeAuthenticationContext.isAzureIdentity'
Is there any other way, apart from using the different approach with the spy object that you can make this work?
I think you can do the following:
const isAzureIdentitySpy = spyOn(AuthenticationContext, 'isAzureIdentity');
isAzureIdentitySpy.and.returnValue(true);
The above will spy on the AuthenticationContext.isAzureIdentity
and return true.