-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): implement *isDevMode directive, will render view when isD…
…evMode() will returns true (#6)
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
projects/platform/src/lib/directives/is-dev-mode.directive.ts
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,34 @@ | ||
import { Directive, EmbeddedViewRef, OnDestroy, TemplateRef, ViewContainerRef, isDevMode, Injectable } from '@angular/core'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class DevModeService { | ||
constructor() {} | ||
|
||
isDevMode() { | ||
return isDevMode(); | ||
} | ||
} | ||
|
||
@Directive({ | ||
selector: '[isDevMode]' | ||
}) | ||
export class IsDevModeDirective implements OnDestroy { | ||
private viewRef: EmbeddedViewRef<null>; | ||
|
||
constructor( | ||
devModeService: DevModeService, | ||
viewContainerRef: ViewContainerRef, | ||
templateRef: TemplateRef<null> | ||
) { | ||
if (devModeService.isDevMode()) { | ||
viewContainerRef.createEmbeddedView(templateRef); | ||
} | ||
} | ||
|
||
ngOnDestroy(): void { | ||
if (this.viewRef) { | ||
this.viewRef.destroy(); | ||
this.viewRef = null; | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
projects/platform/src/test/directives/is-dev-mode.directive.spec.ts
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,28 @@ | ||
import { createHostComponentFactory, SpectatorWithHost, HostComponent } from '@netbasal/spectator'; | ||
import { DevModeService, IsDevModeDirective } from '../../lib/directives/is-dev-mode.directive'; | ||
|
||
const TEXT = 'NGX Features'; | ||
|
||
describe('IsDevModeDirective', () => { | ||
let host: SpectatorWithHost<IsDevModeDirective>; | ||
const mock = { isDevMode: () => true }; | ||
const create = createHostComponentFactory({ | ||
component: IsDevModeDirective, | ||
providers: [ | ||
{ provide: DevModeService, useValue: mock } | ||
] | ||
}); | ||
|
||
it('should create view when dev mode enabled', () => { | ||
mock.isDevMode = () => true; | ||
host = create(`<ng-container *isDevMode>${ TEXT }</ng-container>`); | ||
expect(host.hostElement).toHaveText(TEXT); | ||
}); | ||
|
||
it(`shouldn't create view when dev mode disabled`, () => { | ||
mock.isDevMode = () => false; | ||
host = create(`<ng-container *isDevMode>${ TEXT }</ng-container>`); | ||
expect(host.hostElement).not.toHaveText(TEXT); | ||
}); | ||
|
||
}); |