Skip to content

Commit

Permalink
feat(factory): add custom loader, allow build loaders yml, plugin sql…
Browse files Browse the repository at this point in the history
… ...
  • Loading branch information
ramonornela committed Nov 27, 2016
1 parent 6a18198 commit c62fa56
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NgModule } from '@angular/core';
import { HttpModule, BrowserXhr } from '@angular/http';
import { Config, loaderFactory, ConfigToken, EnvToken } from './providers';
import { Config, loaderFactory, ConfigToken, EnvToken, CustomLoaderToken } from './providers';

@NgModule({
imports: [ HttpModule ],
providers: [
{provide: Config, useFactory: loaderFactory, deps: [ConfigToken, BrowserXhr, EnvToken]}
{provide: Config, useFactory: loaderFactory, deps: [ConfigToken, BrowserXhr, EnvToken, CustomLoaderToken]}
]
})
export class ConfigModule {
Expand Down
1 change: 1 addition & 0 deletions src/providers/config-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Config } from './config';

export const ConfigToken = new OpaqueToken('CONFIGTOKEN');
export const EnvToken = new OpaqueToken('ENVTOKEN');
export const CustomLoaderToken = new OpaqueToken('CUSTOMLOADERTOKEN');

export abstract class ConfigBase implements Config {

Expand Down
4 changes: 3 additions & 1 deletion src/providers/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export function jsonFactory(config: string, xhr: BrowserXhr, env?: string): Json
return new JsonData(config, xhr, env);
}

export function loaderFactory(config: any, xhr?: BrowserXhr, env?: string) {
export function loaderFactory(config: any, xhr?: BrowserXhr, env?: string, customLoader?: Function) {

switch (true) {
case typeof config === 'object':
return dataFactory(config, env);
case typeof config === 'string' && (config.indexOf('.json') !== -1 || config.indexOf('http') !== -1):
return jsonFactory(config, xhr, env);
case typeof customLoader === 'function':
return customLoader.apply(null, [config, env]);
default:
throw new Error('Invalid configuration');
}
Expand Down
70 changes: 43 additions & 27 deletions test/config-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,29 @@ import 'zone.js/dist//sync-test';
import 'zone.js/dist/jasmine-patch';
import { TestBed, inject } from '@angular/core/testing';
import { platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { ConfigModule, Config, ConfigToken, EnvToken } from '../src';
import { ConfigModule, Config, ConfigToken, EnvToken, CustomLoaderToken } from '../src';
import { customLoader } from './mocks';

function configModule(data?: Object, loader?: Function) {
data = data || {
'my-config': 'value',
'my-config-env': {
'dev': 'Config Dev'
},
'my-config-initialize-scalar': 'valueInit',
'my-config-initialize-object': {
'value': 'one'
}
};

TestBed.configureTestingModule({
providers: [
{ provide: ConfigToken, useValue: data },
{ provide: CustomLoaderToken, useValue: loader },
{ provide: EnvToken, useValue: 'dev' }
]
});
}
describe('Module Config', () => {
beforeAll(() => {
TestBed.initTestEnvironment(
Expand All @@ -20,25 +41,10 @@ describe('Module Config', () => {
});

beforeEach(() => {
let data = {
'my-config': 'value',
'my-config-env': {
'dev': 'Config Dev'
},
'my-config-initialize-scalar': 'valueInit',
'my-config-initialize-object': {
'value': 'one'
}
};
TestBed.configureTestingModule({
providers: [
{ provide: ConfigToken, useValue: data },
{ provide: EnvToken, useValue: 'dev' }
]
});
configModule();
});

it('Env', inject([Config], (config: Config) => {
it('Env', inject([Config], (config: any) => {
expect(config.getEnv()).toEqual('dev');
config.setEnv('prod');
expect(config.getEnv()).toEqual('prod');
Expand Down Expand Up @@ -66,24 +72,34 @@ describe('Module Config', () => {
}));

it('Fluent interface', inject([Config], (config: Config) => {
config.setEnv('dev')
.set('config-fluent', 'Config');
config.setEnv('dev').set('config-fluent', 'Config');
}));

it('Set env env', inject([Config], (config: Config) => {
config.set('my-config-setenv', 'valueDev');
config.set('my-config-setenv', 'valueProd', 'prod');
expect(config.get('my-config-setenv')).toBe('valueDev');
expect(config.get('my-config-setenv', 'dev')).toBe('valueDev');
expect(config.get('my-config-setenv', 'prod')).toBe('valueProd');
config.set('my-config-setenv', 'valueDev');
config.set('my-config-setenv', 'valueProd', 'prod');
expect(config.get('my-config-setenv')).toBe('valueDev');
expect(config.get('my-config-setenv', 'dev')).toBe('valueDev');
expect(config.get('my-config-setenv', 'prod')).toBe('valueProd');
}));


it('Set env param initialize', inject([Config], (config: Config) => {
expect(() => config.set('my-config-initialize-scalar', 'valueProd', 'prod'))
expect(() => config.set('my-config-initialize-scalar', 'valueProd', 'prod'))
.toThrow(new Error('Not allow assign to value initialized how scalar'));

expect(() => config.set('my-config-initialize-object', 'valueProd', 'prod'))
.toThrow(new Error('Not allow assign to value initialized how object'))
}));

describe('Custom loader', () => {

beforeEach(() => {
configModule('test.yml', customLoader);
});

it('custom loader', inject([Config], (config: any) => {
expect(config.file).toBe('test.yml');
}));

});
});
11 changes: 9 additions & 2 deletions test/loaders-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'jasmine';
import { MockBrowserXhr } from './mocks';
import { MockBrowserXhr, customLoader, MyCustomLoader } from './mocks';
import { loaderFactory, JsonData, ObjectData } from '../src';

describe('loaders', () => {
Expand Down Expand Up @@ -33,8 +33,15 @@ describe('loaders', () => {

describe('factory object', () => {
it('test object', () => {
var jsonLoader = loaderFactory({'key': 'value' });
let jsonLoader = loaderFactory({'key': 'value' });
expect(jsonLoader instanceof ObjectData).toBeTruthy();
});
});

describe('factory custom', () => {
it('test object', () => {
let myLoader = loaderFactory('test.yml', null, null, customLoader);
expect(myLoader instanceof MyCustomLoader).toBeTruthy();
});
});
});
24 changes: 16 additions & 8 deletions test/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { Injectable } from '@angular/core';
import { BrowserXhr } from '@angular/http';
import { ConfigBase } from '../src';

@Injectable()
export class MockBrowserXhr extends BrowserXhr {
Expand All @@ -17,16 +18,23 @@ export class MockBrowserXhr extends BrowserXhr {
}
}

export var XHRProxy = function() {
this.uri = null;
}
class XHRProxy {

responseType: any;

open(method, uri, sync) {}

XHRProxy.prototype.responseType = null;
addEventListener(event, callback) {}

XHRProxy.prototype.open = function(method, uri, sync) {
this.file = uri;
send(data) {}
}

XHRProxy.prototype.addEventListener = function(event, callback) {}
export function customLoader(config: any, env: string) {
return new MyCustomLoader(config, env);
}

XHRProxy.prototype.send = function(data) {}
export class MyCustomLoader extends ConfigBase {
constructor(public file, env: string) {
super(env);
}
}

0 comments on commit c62fa56

Please sign in to comment.