-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add server configuration pipe and feature toggle pipe
Co-authored-by: Stefan Hauke <s.hauke@intershop.de> Co-authored-by: Danilo Hoffmann <d.hoffmann@intershop.de>
- Loading branch information
1 parent
55a5755
commit 906a5b4
Showing
10 changed files
with
214 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { FeatureToggleModule } from 'ish-core/feature-toggle.module'; | ||
|
||
import { FeatureTogglePipe } from './feature-toggle.pipe'; | ||
|
||
@Component({ | ||
template: ` | ||
<div>unrelated</div> | ||
<div *ngIf="'feature1' | ishFeature">content1</div> | ||
<div *ngIf="'feature2' | ishFeature">content2</div> | ||
<div *ngIf="'always' | ishFeature">contentAlways</div> | ||
<div *ngIf="'never' | ishFeature">contentNever</div> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
class TestComponent {} | ||
|
||
describe('Feature Toggle Pipe', () => { | ||
let fixture: ComponentFixture<TestComponent>; | ||
let element: HTMLElement; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [FeatureToggleModule.forTesting('feature1')], | ||
declarations: [FeatureTogglePipe, TestComponent], | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestComponent); | ||
element = fixture.nativeElement; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should always render unreleated content', () => { | ||
expect(element.textContent).toContain('unrelated'); | ||
}); | ||
|
||
it('should render content of enabled features', () => { | ||
expect(element.textContent).toContain('content1'); | ||
}); | ||
|
||
it('should not render content of disabled features', () => { | ||
expect(element.textContent).not.toContain('content2'); | ||
}); | ||
|
||
it("should always render content for 'always'", () => { | ||
expect(element.textContent).toContain('contentAlways'); | ||
}); | ||
|
||
it("should never render content for 'never'", () => { | ||
expect(element.textContent).not.toContain('contentNever'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
import { FeatureToggleService } from 'ish-core/utils/feature-toggle/feature-toggle.service'; | ||
|
||
/** | ||
* Pipe | ||
* | ||
* Used on a string, this pipe will only return true if the specified feature *is enabled*. | ||
* For the corresponding directive, see {@link FeatureToggleDirective}. | ||
* | ||
* @example | ||
* <ish-product-add-to-compare *ngIf="'compare' | ishFeature"> ...</ish-product-add-to-compare> | ||
*/ | ||
@Pipe({ name: 'ishFeature', pure: true }) | ||
export class FeatureTogglePipe implements PipeTransform { | ||
constructor(private featureToggleService: FeatureToggleService) {} | ||
|
||
transform(feature: string): boolean { | ||
return this.featureToggleService.enabled(feature); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Component } from '@angular/core'; | ||
import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; | ||
import { concat, of } from 'rxjs'; | ||
import { delay } from 'rxjs/operators'; | ||
import { anything, instance, mock, when } from 'ts-mockito'; | ||
|
||
import { AppFacade } from 'ish-core/facades/app.facade'; | ||
|
||
import { ServerSettingPipe } from './server-setting.pipe'; | ||
|
||
@Component({ template: `<ng-container *ngIf="'service.ABC.runnable' | ishServerSetting">TEST</ng-container>` }) | ||
class TestComponent {} | ||
|
||
describe('Server Setting Pipe', () => { | ||
let fixture: ComponentFixture<TestComponent>; | ||
let element: HTMLElement; | ||
let appFacade: AppFacade; | ||
|
||
beforeEach(async () => { | ||
appFacade = mock(AppFacade); | ||
TestBed.configureTestingModule({ | ||
declarations: [ServerSettingPipe, TestComponent], | ||
providers: [{ provide: AppFacade, useFactory: () => instance(appFacade) }], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestComponent); | ||
element = fixture.nativeElement; | ||
}); | ||
|
||
it('should render TEST when setting is set', () => { | ||
when(appFacade.serverSetting$('service.ABC.runnable')).thenReturn(of(true)); | ||
fixture.detectChanges(); | ||
|
||
expect(element).toMatchInlineSnapshot(`TEST`); | ||
}); | ||
|
||
it('should render TEST when setting is set to anything truthy', () => { | ||
when(appFacade.serverSetting$('service.ABC.runnable')).thenReturn(of('ABC')); | ||
fixture.detectChanges(); | ||
|
||
expect(element).toMatchInlineSnapshot(`TEST`); | ||
}); | ||
|
||
it('should render nothing when setting is not set', () => { | ||
when(appFacade.serverSetting$(anything())).thenReturn(of(undefined)); | ||
fixture.detectChanges(); | ||
|
||
expect(element).toMatchInlineSnapshot(`N/A`); | ||
}); | ||
|
||
it('should render nothing when setting is set to sth falsy', () => { | ||
when(appFacade.serverSetting$(anything())).thenReturn(of('')); | ||
fixture.detectChanges(); | ||
|
||
expect(element).toMatchInlineSnapshot(`N/A`); | ||
}); | ||
|
||
it('should render TEST when setting is set', fakeAsync(() => { | ||
when(appFacade.serverSetting$('service.ABC.runnable')).thenReturn(concat(of(false), of(true).pipe(delay(1000)))); | ||
fixture.detectChanges(); | ||
|
||
expect(element).toMatchInlineSnapshot(`N/A`); | ||
tick(1000); | ||
|
||
fixture.detectChanges(); | ||
expect(element).toMatchInlineSnapshot(`TEST`); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { OnDestroy, Pipe, PipeTransform } from '@angular/core'; | ||
import { Subject, Subscription } from 'rxjs'; | ||
import { takeUntil } from 'rxjs/operators'; | ||
|
||
import { AppFacade } from 'ish-core/facades/app.facade'; | ||
|
||
/** | ||
* Pipe | ||
* | ||
* Used on a string, this pipe will return the corresponding server setting by checking the general/serverConfig store. | ||
* If it is set, the Pipe will return a truthy value. | ||
* | ||
* @example | ||
* <example *ngIf="'services.ABC.runnable' | ishServerSetting"> ...</example> | ||
*/ | ||
@Pipe({ name: 'ishServerSetting', pure: false }) | ||
export class ServerSettingPipe implements PipeTransform, OnDestroy { | ||
private returnValue: unknown; | ||
|
||
private destroy$ = new Subject(); | ||
private sub: Subscription; | ||
|
||
constructor(private appFacade: AppFacade) {} | ||
|
||
transform(path: string) { | ||
if (this.sub) { | ||
return this.returnValue; | ||
} else { | ||
this.sub = this.appFacade | ||
.serverSetting$(path) | ||
.pipe(takeUntil(this.destroy$)) | ||
.subscribe(value => { | ||
this.returnValue = value; | ||
}); | ||
return this.returnValue; | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroy$.next(); | ||
this.destroy$.complete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters