In my Angular 8 application, I am utilizing the OidcSecurityService for identity server.
Currently, I am working on writing some unit tests for it. However, I am facing a challenge with the following code section:
ngOnInit() {
this.oidcSecurityService
.checkAuth()
.subscribe(isAuthenticated => {
if (!isAuthenticated) {
Eif ('/autologin' !== window.location.pathname) {
this.write('redirect', window.location.pathname);
this.router.navigate(['/autologin']);
}
}
if (isAuthenticated) {
this.navigateToStoredEndpoint();
}
});
//console.log('windowPath', window.location.pathname);
}
The complete TypeScript file is as follows:
export class AppComponent implements OnInit {
title = 'cityflows-client';
constructor(public oidcSecurityService: OidcSecurityService, public router: Router) {}
ngOnInit() {
this.oidcSecurityService
.checkAuth()
.subscribe(isAuthenticated => {
if (!isAuthenticated) {
if ('/autologin' !== window.location.pathname) {
this.write('redirect', window.location.pathname);
this.router.navigate(['/autologin']);
}
}
if (isAuthenticated) {
this.navigateToStoredEndpoint();
}
});
//console.log('windowPath', window.location.pathname);
}
// Other methods in the AppComponent class omitted for brevity...
}
My unit test is structured like this:
import { CommonModule } from '@angular/common';
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { OidcSecurityService } from 'angular-auth-oidc-client';
import { of } from 'rxjs';
import { AppComponent } from './app.component';
import { OidcSecurityServiceStub } from './shared/mocks/oidcSecurityServiceStub';
// Remaining lines of unit test code omitted for brevity...
However, when looking at the coverage report, I noticed an issue marked by an "E" on this line:
if ('/autologin' !== window.location.pathname) {
It says "Else path not taken." How can I address this?
Thank you!
I found a solution by defining a property in the component:
public href: string = '/';
Then I updated the code as follows:
ngOnInit() {
this.oidcSecurityService
.checkAuth()
.subscribe(isAuthenticated => {
if (!isAuthenticated) {
if ('/autologin' !== this.href) {
this.write('redirect', this.href);
this.router.navigate(['/autologin']);
}
}
if (isAuthenticated) {
this.navigateToStoredEndpoint();
}
});
}