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