]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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 { configureTestBed } from '../../../testing/unit-test-helper'; | |
9 | import { FeatureTogglesGuardService } from './feature-toggles-guard.service'; | |
10 | import { FeatureTogglesService } from './feature-toggles.service'; | |
11 | ||
12 | describe('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 | }); |