I have a pretty simple component that gets an OktaAuthStateService, for observing the authState, and a OktaAuth, for signing in & out, and I want to write a test spec for them that gives me full code coverage. Only problem is with the 2 objects that are injected into the constructor.
import { Component, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { OktaAuthStateService, OKTA_AUTH } from '@okta/okta-angular';
import OktaAuth, { AuthState } from '@okta/okta-auth-js';
import { filter, map, Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
public isAuthenticated$!: Observable<boolean>;
public name$!: Observable<string>;
constructor(private _router: Router,
private _oktaStateService: OktaAuthStateService,
@Inject(OKTA_AUTH) private _oktaAuth: OktaAuth)
{
}
public ngOnInit(): void {
this.isAuthenticated$ = this._oktaStateService.authState$.pipe(
filter((s: AuthState) => !!s),
map((s: AuthState) => s.isAuthenticated ?? false)
);
this.name$ = this._oktaStateService.authState$.pipe(
filter((authState: AuthState) => !!authState && !!authState.isAuthenticated),
map((authState: AuthState) => authState.idToken?.claims.name ?? '')
);
}
public async signIn() : Promise<void> {
await this._oktaAuth.signInWithRedirect().then(
_ => this._router.navigate([''])
);
}
public async signOut(): Promise<void> {
await this._oktaAuth.signOut();
}
}
What should my test spec look like?