Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame^] | 1 | import {TestBed, ComponentFixture, tick, inject} from '@angular/core/testing'; |
| 2 | import {Component, DebugElement, Renderer2, Type} from "@angular/core"; |
| 3 | import {By} from "@angular/platform-browser"; |
| 4 | import {BasicFeatureFlagDirective} from "./basic.featureFlag.directive"; |
| 5 | import {FeatureFlagService} from "../../service/featureFlag.service"; |
| 6 | import {ConfigurationService} from "../../../shared/services/configuration.service"; |
| 7 | import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing"; |
| 8 | import {NgRedux} from "@angular-redux/store"; |
| 9 | import {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 | }) |
| 24 | class TestFeatureFlagComponent { |
| 25 | } |
| 26 | |
| 27 | class MockRenderer<T> { |
| 28 | setStyle() { |
| 29 | |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | class MockAppStore<T> { |
| 34 | getState() { |
| 35 | return { |
| 36 | global: { |
| 37 | flags : { |
| 38 | |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | describe('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 | }); |