]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/feature-toggles-guard.service.spec.ts
update ceph source to reef 18.2.0
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / feature-toggles-guard.service.spec.ts
1 import { Component, NgZone } from '@angular/core';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { ActivatedRouteSnapshot, Router, Routes } from '@angular/router';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { of as observableOf } from 'rxjs';
7
8 import { DashboardNotFoundError } from '~/app/core/error/error';
9 import { configureTestBed } from '~/testing/unit-test-helper';
10 import { FeatureTogglesGuardService } from './feature-toggles-guard.service';
11 import { FeatureTogglesService } from './feature-toggles.service';
12
13 describe('FeatureTogglesGuardService', () => {
14 let service: FeatureTogglesGuardService;
15 let fakeFeatureTogglesService: FeatureTogglesService;
16 let router: Router;
17 let ngZone: NgZone;
18
19 @Component({ selector: 'cd-cephfs', template: '' })
20 class CephfsComponent {}
21
22 @Component({ selector: 'cd-404', template: '' })
23 class NotFoundComponent {}
24
25 const routes: Routes = [
26 { path: 'cephfs', component: CephfsComponent },
27 { path: '404', component: NotFoundComponent }
28 ];
29
30 configureTestBed({
31 imports: [RouterTestingModule.withRoutes(routes)],
32 providers: [
33 { provide: FeatureTogglesService, useValue: { get: null } },
34 FeatureTogglesGuardService
35 ],
36 declarations: [CephfsComponent, NotFoundComponent]
37 });
38
39 beforeEach(() => {
40 service = TestBed.inject(FeatureTogglesGuardService);
41 fakeFeatureTogglesService = TestBed.inject(FeatureTogglesService);
42 ngZone = TestBed.inject(NgZone);
43 router = TestBed.inject(Router);
44 });
45
46 it('should be created', () => {
47 expect(service).toBeTruthy();
48 });
49
50 function testCanActivate(path: string, feature_toggles_map: object) {
51 let result: boolean;
52 spyOn(fakeFeatureTogglesService, 'get').and.returnValue(observableOf(feature_toggles_map));
53
54 ngZone.run(() => {
55 service
56 .canActivate(<ActivatedRouteSnapshot>{ routeConfig: { path: path } })
57 .subscribe((val) => (result = val));
58 });
59 tick();
60
61 return result;
62 }
63
64 it('should allow the feature if enabled', fakeAsync(() => {
65 expect(testCanActivate('cephfs', { cephfs: true })).toBe(true);
66 expect(router.url).toBe('/');
67 }));
68
69 it('should throw error if disable', fakeAsync(() => {
70 expect(() => testCanActivate('cephfs', { cephfs: false })).toThrowError(DashboardNotFoundError);
71 }));
72 });