blob: 5722aa795a20fcd19f862df0b0627109964a7729 [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {TestBed, ComponentFixture, tick, inject} from '@angular/core/testing';
2import {Component, DebugElement, Renderer2, Type} from "@angular/core";
3import {By} from "@angular/platform-browser";
4import {BasicFeatureFlagDirective} from "./basic.featureFlag.directive";
5import {FeatureFlagService} from "../../service/featureFlag.service";
6import {ConfigurationService} from "../../../shared/services/configuration.service";
7import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing";
8import {NgRedux} from "@angular-redux/store";
9import {of} from "rxjs";
10
11@Component({
12 template: `
13 <div
14 id="featureFlagOff"
15 featureFlag
16 [flagName]='"featureFlagOff"'>
17 </div>
18 <div
19 id="featureFlagOn"
20 featureFlag
21 [flagName]='"featureFlagOn"'>
22 </div>`
23})
24class TestFeatureFlagComponent {
25}
26
27class MockRenderer<T> {
28 setStyle() {
29
30 }
31}
32
33class MockAppStore<T> {
34 getState() {
35 return {
36 global: {
37 flags : {
38
39 }
40 }
41 }
42 }
43}
44
45
46describe('Basic Feature Flag Directive', () => {
47 let component: TestFeatureFlagComponent;
48 let fixture: ComponentFixture<TestFeatureFlagComponent>;
49 let directiveInstance: BasicFeatureFlagDirective;
50 let elementOff: DebugElement;
51 let elementOn: DebugElement;
52
53 beforeEach(() => {
54 TestBed.configureTestingModule({
55 imports: [
56 HttpClientTestingModule
57 ],
58 declarations: [
59 TestFeatureFlagComponent,
60 BasicFeatureFlagDirective],
61 providers: [
62 FeatureFlagService,
63 ConfigurationService,
64 {provide: NgRedux, useClass: MockAppStore},
65 {provide: Renderer2, useClass: MockRenderer}]
66 }).compileComponents();
67
68 fixture = TestBed.createComponent(TestFeatureFlagComponent);
69 component = fixture.componentInstance;
70 elementOff = fixture.debugElement.query(By.css('#featureFlagOff'));
71 elementOn = fixture.debugElement.query(By.css('#featureFlagOn'));
72 directiveInstance = elementOff.injector.get(BasicFeatureFlagDirective);
73 });
74
75
76 test('directive should be defined', () => {
77 expect(directiveInstance).toBeDefined();
78 });
79
80 test('should hide element if feature flag is off', () => {
81 directiveInstance.flagName = 'featureFlagOff';
82
83 directiveInstance.ngAfterContentChecked();
84 expect(elementOff.nativeElement.style.display).toEqual('none');
85 });
86
87 test('should show element if feature flag is on', () => {
88 directiveInstance.flagName = 'featureFlagOn';
89
90 directiveInstance.ngAfterContentChecked();
91 expect(elementOn.nativeElement.style.display).toEqual('');
92 });
93});