Skip to content

Commit e489731

Browse files
committed
TT-52 feat: include route in the feature toggle
1 parent 20f8679 commit e489731

File tree

5 files changed

+92
-19
lines changed

5 files changed

+92
-19
lines changed

src/app/app-routing.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { LoginComponent } from './modules/login/login.component';
1212
import { CustomerComponent } from './modules/customer-management/pages/customer.component';
1313
import { UsersComponent } from './modules/users/pages/users.component';
1414
import { TechnologyReportComponent } from './modules/technology-report/pages/technology-report.component';
15+
import { TechnologiesReportGuard } from './guards/technologies-report-guard/technologies-report.guard';
1516

1617
const routes: Routes = [
1718
{
@@ -25,7 +26,7 @@ const routes: Routes = [
2526
{ path: 'activities-management', component: ActivitiesManagementComponent },
2627
{ path: 'customers-management', canActivate: [AdminGuard], component: CustomerComponent },
2728
{ path: 'users', canActivate: [AdminGuard], component: UsersComponent },
28-
{ path: 'technology-report', canActivate: [AdminGuard], component: TechnologyReportComponent},
29+
{ path: 'technology-report', canActivate: [AdminGuard, TechnologiesReportGuard], component: TechnologyReportComponent},
2930
{ path: '', pathMatch: 'full', redirectTo: 'time-clock' },
3031
],
3132
},
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { inject, TestBed } from '@angular/core/testing';
2+
import { Router } from '@angular/router';
3+
import { RouterTestingModule } from '@angular/router/testing';
4+
import { Observable, of } from 'rxjs';
5+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
6+
import { TechnologiesReportGuard } from './technologies-report.guard';
7+
8+
describe('TechnologiesReportGuard', () => {
9+
10+
let technologiesReportGuard: TechnologiesReportGuard;
11+
let featureManagerService: FeatureManagerService;
12+
13+
beforeEach(() => {
14+
TestBed.configureTestingModule({
15+
imports: [ RouterTestingModule ]
16+
});
17+
technologiesReportGuard = TestBed.inject(TechnologiesReportGuard);
18+
featureManagerService = TestBed.inject(FeatureManagerService);
19+
});
20+
21+
it('should be created', () => {
22+
expect(technologiesReportGuard).toBeTruthy();
23+
});
24+
25+
it('can activate the route when feature is enabled for user', () => {
26+
spyOn(featureManagerService, 'isToggleEnabledForUser').and.returnValue(of(true));
27+
28+
const canActivate: Observable<boolean> = technologiesReportGuard.canActivate();
29+
30+
expect(featureManagerService.isToggleEnabledForUser).toHaveBeenCalled();
31+
canActivate.subscribe(value => expect(value).toEqual(true));
32+
});
33+
34+
it('can not active the route and is redirected to home if feature is not enabled for user', inject([Router], (router: Router) => {
35+
spyOn(featureManagerService, 'isToggleEnabledForUser').and.returnValue(of(false));
36+
spyOn(router, 'navigate').and.stub();
37+
38+
const canActivate: Observable<boolean> = technologiesReportGuard.canActivate();
39+
40+
expect(featureManagerService.isToggleEnabledForUser).toHaveBeenCalled();
41+
canActivate.subscribe(value => expect(value).toEqual(false));
42+
expect(router.navigate).toHaveBeenCalledWith(['']);
43+
}));
44+
45+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Injectable } from '@angular/core';
2+
import { Router, CanActivate } from '@angular/router';
3+
import { map } from 'rxjs/operators';
4+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
5+
6+
@Injectable({
7+
providedIn: 'root'
8+
})
9+
export class TechnologiesReportGuard implements CanActivate {
10+
11+
constructor(
12+
private featureManagerService: FeatureManagerService,
13+
private router: Router
14+
) { }
15+
16+
canActivate() {
17+
return this.featureManagerService
18+
.isToggleEnabledForUser('ui-list-technologies')
19+
.pipe(map((enabled) => {
20+
if (enabled === true) {
21+
return true;
22+
} else {
23+
this.router.navigate(['']);
24+
return false;
25+
}
26+
}));
27+
}
28+
}

src/app/modules/shared/components/sidebar/sidebar.component.spec.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import {RouterTestingModule} from '@angular/router/testing';
66
import {Router, Routes} from '@angular/router';
77
import {TimeClockComponent} from '../../../time-clock/pages/time-clock.component';
88
import {provideMockStore} from '@ngrx/store/testing';
9-
import {Observable, of} from 'rxjs';
9+
import {of} from 'rxjs';
10+
import {FeatureManagerService} from '../../feature-toggles/feature-toggle-manager.service';
1011

1112
describe('SidebarComponent', () => {
1213
let component: SidebarComponent;
1314
let fixture: ComponentFixture<SidebarComponent>;
1415
let azureAdB2CServiceStubInjected;
16+
let featureManagerServiceStubInjected: FeatureManagerService;
1517
let router;
1618
const routes: Routes = [
1719
{path: 'time-clock', component: TimeClockComponent}
@@ -42,6 +44,7 @@ describe('SidebarComponent', () => {
4244
beforeEach(() => {
4345
fixture = TestBed.createComponent(SidebarComponent);
4446
azureAdB2CServiceStubInjected = TestBed.inject(AzureAdB2CService);
47+
featureManagerServiceStubInjected = TestBed.inject(FeatureManagerService);
4548
component = fixture.componentInstance;
4649
fixture.detectChanges();
4750
});
@@ -81,25 +84,21 @@ describe('SidebarComponent', () => {
8184
});
8285
});
8386

84-
it('List Technologies item is added when feature flag "ui-list-technologies" is enabled', () => {
85-
const featureManagerServiceStub = {
86-
isToggleEnabledForUser(toggleName: string, toggleLabel?: string): Observable<boolean> {
87-
return of(true);
88-
},
89-
};
87+
it('List Technologies item is added when feature flag "ui-list-technologies" is enabled for user', () => {
88+
spyOn(featureManagerServiceStubInjected, 'isToggleEnabledForUser').and.returnValue(of(true));
9089
const itemsSidebar = [];
91-
component.toggleListTechnologies(featureManagerServiceStub, itemsSidebar);
90+
91+
component.toggleListTechnologies(itemsSidebar);
92+
9293
expect(itemsSidebar.length).toBe(1);
9394
});
9495

95-
it('List Technologies item is not added when feature flag "ui-list-technologies" is disabled', () => {
96-
const featureManagerServiceStub = {
97-
isToggleEnabledForUser(toggleName: string, toggleLabel?: string): Observable<boolean> {
98-
return of(false);
99-
},
100-
};
96+
it('List Technologies item is not added when feature flag "ui-list-technologies" is disabled for user', () => {
97+
spyOn(featureManagerServiceStubInjected, 'isToggleEnabledForUser').and.returnValue(of(false));
10198
const itemsSidebar = [];
102-
component.toggleListTechnologies(featureManagerServiceStub, itemsSidebar);
99+
100+
component.toggleListTechnologies(itemsSidebar);
101+
103102
expect(itemsSidebar.length).toBe(0);
104103
});
105104

src/app/modules/shared/components/sidebar/sidebar.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class SidebarComponent implements OnInit {
2929
ngOnInit(): void {
3030
this.toggleSideBar();
3131
this.getSidebarItems();
32-
this.toggleListTechnologies(this.featureManagerService, this.itemsSidebar);
32+
this.toggleListTechnologies(this.itemsSidebar);
3333
this.highlightMenuOption(this.router.routerState.snapshot.url);
3434
this.navStart.subscribe(evt => {
3535
this.highlightMenuOption(evt.url);
@@ -61,8 +61,8 @@ export class SidebarComponent implements OnInit {
6161
}
6262
}
6363

64-
toggleListTechnologies(featureManagerService, itemsSidebar: ItemSidebar[]){
65-
featureManagerService
64+
toggleListTechnologies(itemsSidebar: ItemSidebar[]){
65+
this.featureManagerService
6666
.isToggleEnabledForUser('ui-list-technologies')
6767
.subscribe((enabled) => {
6868
if (enabled === true){

0 commit comments

Comments
 (0)