0

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

  • Does this answer your question? [How to unit-test canActivate guard method of angular2 using Jasmine?](https://stackoverflow.com/questions/41218323/how-to-unit-test-canactivate-guard-method-of-angular2-using-jasmine) – OSH Apr 06 '20 at 21:56

1 Answers1

-1

Something like this should work:

import { TestBed } from '@angular/core/testing';

import { LoginCheckGuard } from './login-check.guard';
.......

describe('LoginCheckGuard', () => {
  let guard: LoginCheckGuard;
  let mockLoginMineService = { isAuthenticated: undefined };
  let loginMineservice: LoginMineService;
  let router: Router;

  beforeEach(() => {    
    TestBed.configureTestingModule({ 
      // RouterTestingModule to get a handle on the router in a testing environment
      imports: [RouterTestingModule],
      providers: [
                  LoginCheckGuard,
        { provide: LoginMineService, useValue: mockLoginMineService },
      ],
    });
    // get handle on items that are needed
    guard = TestBed.inject(LoginCheckGuard);
    loginMineService = TestBed.inject(LoginMineService);
    router = TestBed.inject(router);
    // spy on the router navigate
    spyOn(router, 'navigate');
  });

  it('should be created', () => {
    expect(guard).toBeTruthy();
  });

  it('should return true if the user isAuthenticated', () => {
    loginMineService.isAuthenticated = true;
    expect(guard.canActivate()).toBe(true);
  });

  it('should return false if the user is not authenticated and navigate to login', () => {
     loginMineService.isAuthenticated = false;
     expect(guard.canActivate()).toBe(true);
     expect(router.navigate).toHaveBeenCalledWith(['/login']);
  });
});

Keep in mind I did this without a code editor so there might be some typos.

AliF50
  • 16,947
  • 1
  • 21
  • 37