here's the big deal how can I do a Unit test for this Guard, The purpose of this is if I am authenticated, then return false and redirect to login and if we return to login and if we have authenticaded before then go to profile page if it's our first time that we're logging then go forward , why? cause I do not want go to that route (callback page) in this case that route its a callback from an external service (okta) this is the code of guard:
import { Injectable } from '@angular/core';
import { LoginMineService } from '../login/login-mine.service';
import { Router } from '@angular/router';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class LoginCheckGuard implements CanActivate {
constructor(private loginMineService: LoginMineService,
private router: Router,
) {
}
canActivate(): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (!this.loginMineService.isAuthenticated) {
return true;
} else {
this.router.navigate(['/login']);
return false;
}
}
}
and this is the Unit test code:
import { TestBed } from '@angular/core/testing';
import { LoginCheckGuard } from './login-check.guard';
describe('LoginCheckGuard', () => {
let guard: LoginCheckGuard;
beforeEach(() => {
TestBed.configureTestingModule({});
guard = TestBed.inject(LoginCheckGuard);
});
it('should be created', () => {
expect(guard).toBeTruthy();
});
});
PS: I am working on Angular 9, Thank y'all