1 import { Component, NgZone } from '@angular/core';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { Router, Routes } from '@angular/router';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { configureTestBed } from '../../../testing/unit-test-helper';
7 import { AuthGuardService } from './auth-guard.service';
8 import { AuthStorageService } from './auth-storage.service';
10 describe('AuthGuardService', () => {
11 let service: AuthGuardService;
12 let authStorageService: AuthStorageService;
15 @Component({ selector: 'cd-login', template: '' })
16 class LoginComponent {}
18 const routes: Routes = [{ path: 'login', component: LoginComponent }];
21 imports: [RouterTestingModule.withRoutes(routes)],
22 providers: [AuthGuardService, AuthStorageService],
23 declarations: [LoginComponent]
27 service = TestBed.get(AuthGuardService);
28 authStorageService = TestBed.get(AuthStorageService);
29 ngZone = TestBed.get(NgZone);
32 it('should be created', () => {
33 expect(service).toBeTruthy();
36 it('should allow the user if loggedIn', () => {
37 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
38 expect(service.canActivate()).toBe(true);
41 it('should prevent user if not loggedIn and redirect to login page', fakeAsync(() => {
42 const router = TestBed.get(Router);
44 expect(service.canActivate()).toBe(false);
47 expect(router.url).toBe('/login');