Skip to content

Commit

Permalink
refactor(data): improvements
Browse files Browse the repository at this point in the history
- validata data type ObjectData;
- delete tests need integration with http;
- fix test.
  • Loading branch information
ramonornela committed Nov 26, 2016
1 parent b4b109a commit a4edb99
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/providers/data/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export class ObjectData extends ConfigBase {

constructor(data: Object, env?: string) {
super(env);
if (data === null) {
throw new Error(`Data is required`);
if (data === null || Array.isArray(data)) {
throw new Error('Data type config invalid');
}

if (Object.keys(data).length === 0) {
throw new Error(`Data is required`);
throw new Error('Config is required');
}

this.data = data;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function jsonFactory(config: string, xhr: BrowserXhr, env?: string): Json
export function loaderFactory(config: any, xhr?: BrowserXhr, env?: string) {

switch (true) {
case typeof config === 'object' && (config !== null && !Array.isArray(config)):
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);
Expand Down
18 changes: 3 additions & 15 deletions test/loaders-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('loaders', () => {

it('invalid type null', () => {
expect(() => loaderFactory(null))
.toThrow(new Error('Invalid configuration'));
.toThrow(new Error('Data type config invalid'));
});

it('invalid type number', () => {
Expand All @@ -22,24 +22,12 @@ describe('loaders', () => {

it('invalid type array', () => {
expect(() => loaderFactory([]))
.toThrow(new Error('Invalid configuration'));
.toThrow(new Error('Data type config invalid'));
});

it('object empty', () => {
expect(() => loaderFactory({}))
.toThrow(new Error('Data is required'));
});
});

describe('factories json', () => {
it('test file external http', () => {
var jsonLoader = loaderFactory('http://example.com/data', new MockBrowserXhr());
expect(jsonLoader instanceof JsonData).toBeTruthy();
});

it('test file local', () => {
var jsonLoader = loaderFactory('assets/data.json', new MockBrowserXhr());
expect(jsonLoader instanceof JsonData).toBeTruthy();
.toThrow(new Error('Config is required'));
});
});

Expand Down

0 comments on commit a4edb99

Please sign in to comment.