]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts
update sources to ceph Nautilus 14.2.1
[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 {
44 imports: [RouterTestingModule.withRoutes(routes)],
45 providers: [ModuleStatusGuardService, { provide: HttpClient, useValue: fakeService }],
46 declarations: [FooComponent]
47 },
48 true
49 );
50
51 beforeEach(() => {
52 service = TestBed.get(ModuleStatusGuardService);
53 httpClient = TestBed.get(HttpClient);
54 router = TestBed.get(Router);
55 route = new ActivatedRouteSnapshot();
56 route.url = [];
57 route.data = {
58 moduleStatusGuardConfig: {
59 apiPath: 'bar',
60 redirectTo: '/foo'
61 }
62 };
63 ngZone = TestBed.get(NgZone);
64 });
65
66 it('should be created', () => {
67 expect(service).toBeTruthy();
68 });
69
70 it('should test canActivate with status available', fakeAsync(() => {
71 route.data.moduleStatusGuardConfig.redirectTo = 'foo';
72 testCanActivate({ available: true, message: 'foo' }, true, '/');
73 }));
74
75 it('should test canActivateChild with status unavailable', fakeAsync(() => {
76 testCanActivate({ available: false, message: null }, false, '/foo/');
77 }));
78
79 it('should test canActivateChild with status unavailable', fakeAsync(() => {
80 testCanActivate(null, false, '/foo');
81 }));
82 });