All files / src/app/core/directives not-feature-toggle.directive.ts

95.23% Statements 20/21
68.42% Branches 13/19
100% Functions 4/4
95.23% Lines 20/21

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57195x 195x 195x 195x   195x                               195x   20x   20x     20x 20x 20x 20x   20x 20x 10x   10x   20x           20x         20x     20x      
import { ChangeDetectorRef, DestroyRef, Directive, Input, TemplateRef, ViewContainerRef, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ReplaySubject, Subscription } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
 
import { FeatureToggleService, FeatureToggleType } from 'ish-core/feature-toggle.module';
 
/**
 * Structural directive.
 * Used on an element, this element will only be rendered if the specified feature *is not enabled*.
 *
 * For the negated case see {@link FeatureToggleDirective}.
 *
 * @example
 * <div *ishNotFeature="'quoting'">
 *   Only visible when quoting is NOT enabled by configuration.
 * </div>
 */
@Directive({
  selector: '[ishNotFeature]',
})
export class NotFeatureToggleDirective {
  private subscription: Subscription;
  private disabled$ = new ReplaySubject<boolean>(1);
 
  private destroyRef = inject(DestroyRef);
 
  constructor(
    private templateRef: TemplateRef<unknown>,
    private viewContainer: ViewContainerRef,
    private featureToggle: FeatureToggleService,
    private cdRef: ChangeDetectorRef
  ) {
    this.disabled$.pipe(distinctUntilChanged(), takeUntilDestroyed()).subscribe(disabled => {
      if (disabled) {
        this.viewContainer.createEmbeddedView(this.templateRef);
      } else {
        this.viewContainer.clear();
      }
      this.cdRef.markForCheck();
    });
  }
 
  @Input() set ishNotFeature(val: 'always' | 'never' | FeatureToggleType) {
    // end previous subscription and newly subscribe
    Iif (this.subscription) {
      // eslint-disable-next-line ban/ban
      this.subscription.unsubscribe();
    }
 
    this.subscription = this.featureToggle
      .enabled$(val)
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe({ next: value => this.disabled$.next(!value) });
  }
}