How can I test canActivate function of angular that returns a function which in turn returns a boolean value?.
I tried creating objects of ActivatedrouterSnapshot and routerStateSnapshot and passing it to canActivate function but that didn't help.
export class AuthGuard implements CanActivate {
constructor(
private authService: AuthenticationService,
private loginService: LoginService,
private router: Router
) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean>
| boolean {
return this.checkLogin(state.url);
}
checkLogin(url: string): boolean {
if (this.authService.isLoggedIn()) { return true; }
// Store the attempted URL for redirecting
this.loginService.redirectUrl = url;
// Navigate to the login page with extras
this.router.navigate(['/login']);
return false;
}
}
Since the checklogin returns true,I want that to happen. But I don't know where to start?.