Skip to content

Commit

Permalink
feat: added the routing guard for beta pages (#2627)
Browse files Browse the repository at this point in the history
* added the routing guard for beta pages

* added service method
  • Loading branch information
arjunaj5 authored Dec 1, 2023
1 parent 9472848 commit 1651e9d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/app/core/guards/beta-page-feature-flag.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { ActivatedRoute, Router } from '@angular/router';
import { OrgSettingsService } from '../services/org-settings.service';
import { BetaPageFeatureFlagGuard } from './beta-page-feature-flag.guard';
import { TestBed } from '@angular/core/testing';
import { Observable, of } from 'rxjs';
import { routerStateSnapshotData } from '../mock-data/router-state-snapshot.data';

describe('BetaPageFeatureFlagGuard', () => {
let guard: BetaPageFeatureFlagGuard;
let orgSettingsService: jasmine.SpyObj<OrgSettingsService>;
let router: jasmine.SpyObj<Router>;
let activatedRoute: jasmine.SpyObj<ActivatedRoute>;

beforeEach(() => {
const orgSettingsServiceSpy = jasmine.createSpyObj('OrgSettingsService', ['isBetaPageEnabledForPath']);
const routerSpy = jasmine.createSpyObj('Router', ['navigate']);

TestBed.configureTestingModule({
providers: [
{
provide: OrgSettingsService,
useValue: orgSettingsServiceSpy,
},
{
provide: Router,
useValue: routerSpy,
},
{
provide: ActivatedRoute,
useValue: {
snapshot: {
routeConfig: {
path: 'my_view_report',
},
data: {
url: '/enterprise/my_view_report',
root: null,
},
},
},
},
],
});
guard = TestBed.inject(BetaPageFeatureFlagGuard);
orgSettingsService = TestBed.inject(OrgSettingsService) as jasmine.SpyObj<OrgSettingsService>;
activatedRoute = TestBed.inject(ActivatedRoute) as jasmine.SpyObj<ActivatedRoute>;
router = TestBed.inject(Router) as jasmine.SpyObj<Router>;
});

it('should be created', () => {
expect(guard).toBeTruthy();
});

describe('canActivate(): ', () => {
it('Should navigate to the beta page if the feature flag is enabled', (done) => {
orgSettingsService.isBetaPageEnabledForPath.and.returnValue(of(true));
const canActivate = guard.canActivate(activatedRoute.snapshot, routerStateSnapshotData) as Observable<boolean>;
canActivate.subscribe((res) => {
expect(orgSettingsService.isBetaPageEnabledForPath).toHaveBeenCalledTimes(1);
expect(res).toBeFalse();
expect(router.navigate).toHaveBeenCalledWith(['/', 'enterprise', 'my_view_report_beta', {}]);
done();
});
});

it('Should navigate to the correct page if the feature flag is disabled', (done) => {
orgSettingsService.isBetaPageEnabledForPath.and.returnValue(of(false));
const canActivate = guard.canActivate(activatedRoute.snapshot, routerStateSnapshotData) as Observable<boolean>;
canActivate.subscribe((res) => {
expect(orgSettingsService.isBetaPageEnabledForPath).toHaveBeenCalledTimes(1);
expect(res).toBeTrue();
expect(router.navigate).not.toHaveBeenCalled();
done();
});
});
});
});
32 changes: 32 additions & 0 deletions src/app/core/guards/beta-page-feature-flag.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable, map, of } from 'rxjs';
import { OrgSettingsService } from '../services/org-settings.service';

@Injectable({
providedIn: 'root',
})
export class BetaPageFeatureFlagGuard implements CanActivate {
constructor(private orgSettingsService: OrgSettingsService, private router: Router) {}

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const currentPath = route.routeConfig && route.routeConfig.path;

if (currentPath) {
return this.orgSettingsService.isBetaPageEnabledForPath(currentPath).pipe(
map((isBetaPageEnabled) => {
if (isBetaPageEnabled) {
this.router.navigate(['/', 'enterprise', `${currentPath}_beta`, { ...route.params }]);
return false;
}
return true;
})
);
} else {
return of(true);
}
}
}
9 changes: 9 additions & 0 deletions src/app/core/services/org-settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export class OrgSettingsService {
return this.apiService.post('/org/settings', data);
}

isBetaPageEnabledForPath(currentPath: string): Observable<boolean> {
const pathSettingsFlagMap = {
my_view_report: 'mobile_app_view_report_beta_enabled',
view_team_report: 'mobile_app_view_report_beta_enabled',
};
const featureFlag = pathSettingsFlagMap[currentPath];
return this.get().pipe(map((orgSettings: OrgSettings) => orgSettings[featureFlag]));
}

getIncomingAccountingObject(incomingAccountExport: AccountingExportSettings): IncomingAccountObject {
const accounting: IncomingAccountObject = {
enabled: false,
Expand Down

0 comments on commit 1651e9d

Please sign in to comment.