title: How do I unit test canActivate of Angular for authguard?
body: <p>How do I perform unit testing for auth guard?</p>
  
  <p>I don't know how to start, can anyone please help me?</p>
  
  <p>This is my authguard.component:</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import {
    Router,
    CanActivate,
    ActivatedRouteSnapshot,
    RouterStateSnapshot
  } from '@angular/router';
  
  import { LoginService } from '../services/login/login.service';
  
  @Injectable({
    providedIn: 'root'
  })
  export class AuthGuard implements CanActivate {
    constructor(private router: Router, private loginService: LoginService) {}
  
    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
      const currentUser = this.loginService.currentUserValue;
      if (currentUser) {
        // Authorized so return true
        return !0;
      }
  
      // not authorized so redirect to signin page with the return url
      this.router.navigate(['/login'], {
        queryParams: { returnUrl: state.url }
      });
      return !1;
    }
  }
  </code></pre>