All files / src/app/shared/services feature-toggles-guard.service.ts

94.74% Statements 18/19
80% Branches 8/10
80% Functions 4/5
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 308x 8x   8x   8x         8x 3x   8x 2x   2x 1x 1x   1x         8x     8x  
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router } from '@angular/router';
 
import { map } from 'rxjs/operators';
 
import { FeatureTogglesMap, FeatureTogglesService } from './feature-toggles.service';
 
@Injectable({
  providedIn: 'root'
})
export class FeatureTogglesGuardService implements CanActivate, CanActivateChild {
  constructor(private router: Router, private featureToggles: FeatureTogglesService) {}
 
  canActivate(route: ActivatedRouteSnapshot) {
    return this.featureToggles.get().pipe(
      map((enabledFeatures: FeatureTogglesMap) => {
        if (enabledFeatures[route.routeConfig.path] === false) {
          this.router.navigate(['404']);
          return false;
        }
        return true;
      })
    );
  }
 
  canActivateChild(route: ActivatedRouteSnapshot) {
    return this.canActivate(route.parent);
  }
}