Skip to content

Commit

Permalink
feat(lib): add define global injections function
Browse files Browse the repository at this point in the history
  • Loading branch information
Netanel Basal committed May 21, 2019
1 parent 6097376 commit 5456230
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 10 deletions.
2 changes: 1 addition & 1 deletion projects/spectator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@netbasal/spectator",
"description": "Angular tests made easy",
"author": "Netanel Basal <netanel7799s@gmail.com>",
"version": "3.8.2",
"version": "3.9.0",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
15 changes: 9 additions & 6 deletions projects/spectator/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Component, NO_ERRORS_SCHEMA, Type } from '@angular/core';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { mockProvider } from './mock';
import { isType } from './is-type';
import { getGlobalsInjections } from './globals-injections';

@Component({
template: ''
Expand Down Expand Up @@ -38,6 +39,8 @@ const defaultOptions: SpectatorOptions<any, any> = {
declareComponent: true
};

const { declarations: globalDec, providers: globalProviders, imports: globalImports } = getGlobalsInjections();

export function initialModule<T, C = HostComponent>(
options: SpectatorOptions<T, C> | Type<T>,
withHost = false
Expand All @@ -55,21 +58,21 @@ export function initialModule<T, C = HostComponent>(
component = options;
host = HostComponent;
moduleMetadata = {
declarations: [component, withHost ? host : []],
imports: [NoopAnimationsModule],
declarations: [...globalDec, component, withHost ? host : []],
imports: [...globalImports, NoopAnimationsModule],
schemas: [],
providers: [],
providers: [...globalProviders],
entryComponents: []
};
} else {
component = merged.component;
host = merged.host;

moduleMetadata = {
declarations: [merged.declareComponent ? component : [], withHost ? host : [], ...(merged.declarations || [])],
imports: [merged.disableAnimations ? NoopAnimationsModule : [], ...(merged.imports || [])],
declarations: [...globalDec, merged.declareComponent ? component : [], withHost ? host : [], ...(merged.declarations || [])],
imports: [...globalImports, merged.disableAnimations ? NoopAnimationsModule : [], ...(merged.imports || [])],
schemas: [merged.shallow ? NO_ERRORS_SCHEMA : merged.schemas || []],
providers: [...(merged.providers || [])],
providers: [...globalProviders, ...(merged.providers || [])],
componentProviders: merged.componentProviders ? [merged.componentProviders] : undefined,
entryComponents: [merged.entryComponents]
};
Expand Down
15 changes: 15 additions & 0 deletions projects/spectator/src/lib/globals-injections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { TestModuleMetadata } from '@angular/core/testing';

let globals: TestModuleMetadata = {
providers: [],
declarations: [],
imports: []
};

export function defineGlobalsInjections(config: TestModuleMetadata) {
globals = { ...globals, ...config };
}

export function getGlobalsInjections() {
return globals;
}
1 change: 1 addition & 0 deletions projects/spectator/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from './type-in-element';
export * from './globals';
export * from './mock-component';
export * from './mock-directive';
export * from './globals-injections';
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import { WidgetComponent } from './widget/widget.component';
import { ZippyComponent } from './zippy/zippy.component';
import { FormInputComponent } from './form-input/form-input.component';
import { IntegrationModule } from './integration/integration.module';
import { TranslatePipe } from './translate.pipe';

@NgModule({
declarations: [AppComponent, ZippyComponent, ButtonComponent, HighlightDirective, CalcComponent, DynamicComponent, ConsumeDynamicComponent, ViewChildrenComponent, ChildComponent, WidgetComponent, AppUnlessDirective, WidgetComponent, ClickComponent, AutoFocusDirective, FgComponent, AsyncComponent, DomSelectorsComponent, HelloComponent, AsyncInputComponent, ComponentWithoutOverwrittenProvidersComponent, FormInputComponent],
declarations: [AppComponent, ZippyComponent, ButtonComponent, HighlightDirective, CalcComponent, DynamicComponent, ConsumeDynamicComponent, ViewChildrenComponent, ChildComponent, WidgetComponent, AppUnlessDirective, WidgetComponent, ClickComponent, AutoFocusDirective, FgComponent, AsyncComponent, DomSelectorsComponent, HelloComponent, AsyncInputComponent, ComponentWithoutOverwrittenProvidersComponent, FormInputComponent, TranslatePipe],
entryComponents: [DynamicComponent],
imports: [BrowserModule, HttpClientModule, ReactiveFormsModule, IntegrationModule],
providers: [ChildServiceService, WidgetService, WidgetDataService],
Expand Down
5 changes: 4 additions & 1 deletion src/app/hello/hello.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { TranslateService } from '../translate.service';

@Component({
selector: 'hello',
template: `
<div [style.width]="width">
<h1>{{ title }}</h1>
<h1>{{ title | translate }}</h1>
</div>
<div *ngIf="!widthRaw" style="color:red">widthRaw is not set</div>
Expand All @@ -24,4 +25,6 @@ export class HelloComponent {
get width() {
return typeof this.widthRaw === 'number' ? `${this.widthRaw}px` : this.widthRaw;
}

constructor(private translate: TranslateService) {}
}
13 changes: 13 additions & 0 deletions src/app/translate.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Pipe, PipeTransform } from '@angular/core';
import { TranslateService } from './translate.service';

@Pipe({
name: 'translate'
})
export class TranslatePipe implements PipeTransform {
constructor(private translateService: TranslateService) {}

transform(value: any, args?: any): any {
return this.translateService.transform(value);
}
}
10 changes: 10 additions & 0 deletions src/app/translate.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Injectable } from '@angular/core';

@Injectable()
export class TranslateService {
constructor() {}

transform(value: any) {
return value;
}
}
8 changes: 8 additions & 0 deletions src/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
import 'jest-preset-angular';
import { defineGlobalsInjections } from '../projects/spectator/src/lib/globals-injections';
import { TranslateService } from './app/translate.service';
import { TranslatePipe } from './app/translate.pipe';

defineGlobalsInjections({
providers: [TranslateService],
declarations: [TranslatePipe]
});
9 changes: 8 additions & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { defineGlobalsInjections } from '../projects/spectator/src/lib/globals-injections';
import { TranslateService } from './app/translate.service';
import { TranslatePipe } from './app/translate.pipe';

declare const require: any;

defineGlobalsInjections({
providers: [TranslateService],
declarations: [TranslatePipe]
});

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
// Then we find all the tests.
Expand Down

0 comments on commit 5456230

Please sign in to comment.