Skip to content

Commit 5d25f86

Browse files
authored
feat(core): implement *isDevMode directive, will render view when isDevMode() will returns true (#6)
1 parent 533f97a commit 5d25f86

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Directive, EmbeddedViewRef, OnDestroy, TemplateRef, ViewContainerRef, isDevMode, Injectable } from '@angular/core';
2+
3+
@Injectable({ providedIn: 'root' })
4+
export class DevModeService {
5+
constructor() {}
6+
7+
isDevMode() {
8+
return isDevMode();
9+
}
10+
}
11+
12+
@Directive({
13+
selector: '[isDevMode]'
14+
})
15+
export class IsDevModeDirective implements OnDestroy {
16+
private viewRef: EmbeddedViewRef<null>;
17+
18+
constructor(
19+
devModeService: DevModeService,
20+
viewContainerRef: ViewContainerRef,
21+
templateRef: TemplateRef<null>
22+
) {
23+
if (devModeService.isDevMode()) {
24+
viewContainerRef.createEmbeddedView(templateRef);
25+
}
26+
}
27+
28+
ngOnDestroy(): void {
29+
if (this.viewRef) {
30+
this.viewRef.destroy();
31+
this.viewRef = null;
32+
}
33+
}
34+
}

projects/platform/src/lib/platform.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ComposeDirective } from './directives/compose.directive';
55
import { CookiesDirective } from './directives/cookies.directive';
66
import { HttpDirective } from './directives/http.directive';
77
import { InitDirective } from './directives/init.directive';
8+
import { IsDevModeDirective } from './directives/is-dev-mode.directive';
89
import { LazyDirective, LAZY_COMPONENT_TOKEN } from './directives/lazy.directive';
910
import { NestDirective } from './directives/nest.directive';
1011
import { RenamePropDirective } from './directives/rename-prop.directive';
@@ -28,6 +29,7 @@ const DIRECTIVES = [
2829
HttpDirective,
2930
LazyDirective,
3031
InitDirective,
32+
IsDevModeDirective,
3133
NestDirective,
3234
RenamePropDirective,
3335
ReturnDirective,
@@ -68,6 +70,7 @@ export { ComposeDirective } from './directives/compose.directive';
6870
export { CookiesDirective } from './directives/cookies.directive';
6971
export { HttpDirective } from './directives/http.directive';
7072
export { InitDirective } from './directives/init.directive';
73+
export { IsDevModeDirective } from './directives/is-dev-mode.directive';
7174
export { LazyDirective, LAZY_COMPONENT_TOKEN } from './directives/lazy.directive';
7275
export { NestDirective } from './directives/nest.directive';
7376
export { RenamePropDirective } from './directives/rename-prop.directive';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createHostComponentFactory, SpectatorWithHost, HostComponent } from '@netbasal/spectator';
2+
import { DevModeService, IsDevModeDirective } from '../../lib/directives/is-dev-mode.directive';
3+
4+
const TEXT = 'NGX Features';
5+
6+
describe('IsDevModeDirective', () => {
7+
let host: SpectatorWithHost<IsDevModeDirective>;
8+
const mock = { isDevMode: () => true };
9+
const create = createHostComponentFactory({
10+
component: IsDevModeDirective,
11+
providers: [
12+
{ provide: DevModeService, useValue: mock }
13+
]
14+
});
15+
16+
it('should create view when dev mode enabled', () => {
17+
mock.isDevMode = () => true;
18+
host = create(`<ng-container *isDevMode>${ TEXT }</ng-container>`);
19+
expect(host.hostElement).toHaveText(TEXT);
20+
});
21+
22+
it(`shouldn't create view when dev mode disabled`, () => {
23+
mock.isDevMode = () => false;
24+
host = create(`<ng-container *isDevMode>${ TEXT }</ng-container>`);
25+
expect(host.hostElement).not.toHaveText(TEXT);
26+
});
27+
28+
});

0 commit comments

Comments
 (0)