1 import { HttpClient } from '@angular/common/http';
2 import { Component, NgZone } from '@angular/core';
3 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
4 import { ActivatedRouteSnapshot, Router, Routes } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { of as observableOf } from 'rxjs';
9 import { configureTestBed } from '../../../testing/unit-test-helper';
10 import { ModuleStatusGuardService } from './module-status-guard.service';
12 describe('ModuleStatusGuardService', () => {
13 let service: ModuleStatusGuardService;
14 let httpClient: HttpClient;
16 let route: ActivatedRouteSnapshot;
19 @Component({ selector: 'cd-foo', template: '' })
26 const routes: Routes = [{ path: '**', component: FooComponent }];
28 const testCanActivate = (getResult: {}, activateResult: boolean, urlResult: string) => {
30 spyOn(httpClient, 'get').and.returnValue(observableOf(getResult));
32 service.canActivateChild(route).subscribe((resp) => {
38 expect(result).toBe(activateResult);
39 expect(router.url).toBe(urlResult);
44 imports: [RouterTestingModule.withRoutes(routes)],
45 providers: [ModuleStatusGuardService, { provide: HttpClient, useValue: fakeService }],
46 declarations: [FooComponent]
52 service = TestBed.get(ModuleStatusGuardService);
53 httpClient = TestBed.get(HttpClient);
54 router = TestBed.get(Router);
55 route = new ActivatedRouteSnapshot();
58 moduleStatusGuardConfig: {
63 ngZone = TestBed.get(NgZone);
66 it('should be created', () => {
67 expect(service).toBeTruthy();
70 it('should test canActivate with status available', fakeAsync(() => {
71 route.data.moduleStatusGuardConfig.redirectTo = 'foo';
72 testCanActivate({ available: true, message: 'foo' }, true, '/');
75 it('should test canActivateChild with status unavailable', fakeAsync(() => {
76 testCanActivate({ available: false, message: null }, false, '/foo/');
79 it('should test canActivateChild with status unavailable', fakeAsync(() => {
80 testCanActivate(null, false, '/foo');