1 import { Component, NgZone } from '@angular/core';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, 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;
14 let route: ActivatedRouteSnapshot;
15 let state: RouterStateSnapshot;
17 @Component({ selector: 'cd-login', template: '' })
18 class LoginComponent {}
20 const routes: Routes = [{ path: 'login', component: LoginComponent }];
23 imports: [RouterTestingModule.withRoutes(routes)],
24 providers: [AuthGuardService, AuthStorageService],
25 declarations: [LoginComponent]
29 service = TestBed.get(AuthGuardService);
30 authStorageService = TestBed.get(AuthStorageService);
31 ngZone = TestBed.get(NgZone);
34 it('should be created', () => {
35 expect(service).toBeTruthy();
38 it('should allow the user if loggedIn', () => {
40 state = { url: '/', root: null };
41 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
42 expect(service.canActivate(route, state)).toBe(true);
45 it('should prevent user if not loggedIn and redirect to login page', fakeAsync(() => {
46 const router = TestBed.get(Router);
47 state = { url: '/pool', root: null };
49 expect(service.canActivate(route, state)).toBe(false);
52 expect(router.url).toBe('/login?returnUrl=%2Fpool');