]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts
import ceph 16.2.6
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / module-status-guard.service.spec.ts
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';
6
7 import { of as observableOf } from 'rxjs';
8
9 import { configureTestBed } from '~/testing/unit-test-helper';
10 import { ModuleStatusGuardService } from './module-status-guard.service';
11
12 describe('ModuleStatusGuardService', () => {
13 let service: ModuleStatusGuardService;
14 let httpClient: HttpClient;
15 let router: Router;
16 let route: ActivatedRouteSnapshot;
17 let ngZone: NgZone;
18
19 @Component({ selector: 'cd-foo', template: '' })
20 class FooComponent {}
21
22 const fakeService = {
23 get: () => true
24 };
25
26 const routes: Routes = [{ path: '**', component: FooComponent }];
27
28 const testCanActivate = (getResult: {}, activateResult: boolean, urlResult: string) => {
29 let result: boolean;
30 spyOn(httpClient, 'get').and.returnValue(observableOf(getResult));
31 ngZone.run(() => {
32 service.canActivateChild(route).subscribe((resp) => {
33 result = resp;
34 });
35 });
36
37 tick();
38 expect(result).toBe(activateResult);
39 expect(router.url).toBe(urlResult);
40 };
41
42 configureTestBed({
43 imports: [RouterTestingModule.withRoutes(routes)],
44 providers: [ModuleStatusGuardService, { provide: HttpClient, useValue: fakeService }],
45 declarations: [FooComponent]
46 });
47
48 beforeEach(() => {
49 service = TestBed.inject(ModuleStatusGuardService);
50 httpClient = TestBed.inject(HttpClient);
51 router = TestBed.inject(Router);
52 route = new ActivatedRouteSnapshot();
53 route.url = [];
54 route.data = {
55 moduleStatusGuardConfig: {
56 apiPath: 'bar',
57 redirectTo: '/foo'
58 }
59 };
60 ngZone = TestBed.inject(NgZone);
61 });
62
63 it('should be created', () => {
64 expect(service).toBeTruthy();
65 });
66
67 it('should test canActivate with status available', fakeAsync(() => {
68 route.data.moduleStatusGuardConfig.redirectTo = 'foo';
69 testCanActivate({ available: true, message: 'foo' }, true, '/');
70 }));
71
72 it('should test canActivateChild with status unavailable', fakeAsync(() => {
73 testCanActivate({ available: false, message: null }, false, '/foo');
74 }));
75
76 it('should test canActivateChild with status unavailable', fakeAsync(() => {
77 testCanActivate(null, false, '/foo');
78 }));
79 });