aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/featureFlag/directive/basic/basic.featureFlag.directive.spec.ts
blob: 5722aa795a20fcd19f862df0b0627109964a7729 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {TestBed, ComponentFixture, tick, inject} from '@angular/core/testing';
import {Component, DebugElement, Renderer2, Type} from "@angular/core";
import {By} from "@angular/platform-browser";
import {BasicFeatureFlagDirective} from "./basic.featureFlag.directive";
import {FeatureFlagService} from "../../service/featureFlag.service";
import {ConfigurationService} from "../../../shared/services/configuration.service";
import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing";
import {NgRedux} from "@angular-redux/store";
import {of} from "rxjs";

@Component({
  template: `
    <div
      id="featureFlagOff"
      featureFlag
      [flagName]='"featureFlagOff"'>
    </div>
    <div
      id="featureFlagOn"
      featureFlag
      [flagName]='"featureFlagOn"'>
    </div>`
})
class TestFeatureFlagComponent {
}

class MockRenderer<T> {
  setStyle() {

  }
}

class MockAppStore<T> {
  getState() {
    return {
      global: {
        flags : {

        }
      }
    }
  }
}


describe('Basic Feature Flag Directive', () => {
  let component: TestFeatureFlagComponent;
  let fixture: ComponentFixture<TestFeatureFlagComponent>;
  let directiveInstance: BasicFeatureFlagDirective;
  let elementOff: DebugElement;
  let elementOn: DebugElement;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule
      ],
      declarations: [
        TestFeatureFlagComponent,
        BasicFeatureFlagDirective],
      providers: [
        FeatureFlagService,
        ConfigurationService,
        {provide: NgRedux, useClass: MockAppStore},
        {provide: Renderer2, useClass: MockRenderer}]
    }).compileComponents();

    fixture = TestBed.createComponent(TestFeatureFlagComponent);
    component = fixture.componentInstance;
    elementOff = fixture.debugElement.query(By.css('#featureFlagOff'));
    elementOn = fixture.debugElement.query(By.css('#featureFlagOn'));
    directiveInstance = elementOff.injector.get(BasicFeatureFlagDirective);
  });


  test('directive should be defined', () => {
    expect(directiveInstance).toBeDefined();
  });

  test('should hide element if feature flag is off', () => {
    directiveInstance.flagName  = 'featureFlagOff';

    directiveInstance.ngAfterContentChecked();
    expect(elementOff.nativeElement.style.display).toEqual('none');
  });

  test('should show element if feature flag is on', () => {
    directiveInstance.flagName  = 'featureFlagOn';

    directiveInstance.ngAfterContentChecked();
    expect(elementOn.nativeElement.style.display).toEqual('');
  });
});