Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Commit

Permalink
Add unit tests and improve implementation based on the results
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanforbes committed Jun 16, 2015
1 parent 1dd74d0 commit b6b4648
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/load.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import has from './has';
import Promise from './Promise';

declare var define: {
(...args: any[]): any;
amd: any;
};

export interface Load {
(require: Require, ...moduleIds: string[]): Promise<any[]>;
}

export interface Require {
export interface AMDRequire {
(moduleIds: string[], callback: (...modules:any[]) => void): void;
}
export interface NodeRequire {
(moduleId: string): any;
}
export type Require = AMDRequire | NodeRequire;

export interface Load {
(require: Require, ...moduleIds: string[]): Promise<any[]>;
}

const load: Load = (function (): Load {
if (typeof define === 'function' && define.amd) {
return function (require: Require, ...moduleIds: string[]): Promise<any[]> {
return new Promise(function (resolve) {
// TODO: Error path
require(moduleIds, function (...modules: any[]) {
resolve(modules);
});
});
};
}
else if (has('host-node')) {
return function (require: Require, ...moduleIds: string[]): Promise<any[]> {
if (typeof module === 'object' && typeof module.exports === 'object') {
return function (require: NodeRequire, ...moduleIds: string[]): Promise<any[]> {
return new Promise(function (resolve, reject) {
try {
resolve(moduleIds.map(function (moduleId): any {
Expand All @@ -40,8 +32,18 @@ const load: Load = (function (): Load {
});
};
}
else if (typeof define === 'function' && define.amd) {
return function (require: AMDRequire, ...moduleIds: string[]): Promise<any[]> {
return new Promise(function (resolve) {
// TODO: Error path
require(moduleIds, function (...modules: any[]) {
resolve(modules);
});
});
};
}
else {
return <any> function () {
return function () {
return Promise.reject(new Error('Unknown loader'));
};
}
Expand Down
1 change: 1 addition & 0 deletions tests/unit/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import './encoding';
import './global';
import './has';
import './lang';
import './load';
import './math';
import './Map';
import './number';
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/load.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import assert = require('intern/chai!assert');
import registerSuite = require('intern!object');
import has from 'src/has';
import load from 'src/load';
import Promise from 'src/Promise';

const suite: any = {
name: 'load',

load() {
var def = this.async(5000);

load(require, './load/a', './load/b').then(def.callback(function ([ a, b ]: [ any, any ]) {
assert.deepEqual(a, { one: 1, two: 2 });
assert.deepEqual(b, { three: 3, four: 4 });
}));
}
};

if (has('host-node')) {
const nodeRequire: any = global.require.nodeRequire;
const path: any = nodeRequire('path');
const buildDir: string = path.join(process.cwd(), '_build');

suite.node = {
'different than AMD load'() {
const nodeLoad: typeof load = nodeRequire(path.join(buildDir, 'src', 'load')).default;
assert.notStrictEqual(nodeLoad, load);
},

'load succeeds'() {
var def = this.async(5000);

var result: Promise<any[]> = nodeRequire(path.join(buildDir, 'tests', 'unit', 'load', 'node')).succeed;
result.then(def.callback(function ([ a, b ]: [ any, any ]) {
assert.deepEqual(a, { one: 1, two: 2 });
assert.deepEqual(b, { three: 3, four: 4 });
}));
},

'load fails'() {
var def = this.async(5000);

var result: Promise<any[]> = nodeRequire(path.join(buildDir, 'tests', 'unit', 'load', 'node')).fail;
result.then(function () {
def.reject(new Error('load should not have succeeded'));
}, def.callback(function (error: Error) {
assert.instanceOf(error, Error);
}));
}
};
}

registerSuite(suite);
2 changes: 2 additions & 0 deletions tests/unit/load/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const one = 1;
export const two = 2;
2 changes: 2 additions & 0 deletions tests/unit/load/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const three = 3;
export const four = 4;
4 changes: 4 additions & 0 deletions tests/unit/load/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import load from '../../../src/load';

export const succeed = load(require, './a', './b');
export const fail = load(require, './a', './c');
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
"./tests/unit/global.ts",
"./tests/unit/has.ts",
"./tests/unit/lang.ts",
"./tests/unit/load.ts",
"./tests/unit/load/a.ts",
"./tests/unit/load/b.ts",
"./tests/unit/load/node.ts",
"./tests/unit/math.ts",
"./tests/unit/module.d.ts",
"./tests/unit/number.ts",
Expand Down

0 comments on commit b6b4648

Please sign in to comment.