Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 77cdb2c

Browse files
committed
loader api tests
1 parent f968a27 commit 77cdb2c

9 files changed

+141
-26
lines changed
File renamed without changes.

core/fetch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export var envFetch;
4242
if (typeof XMLHttpRequest != 'undefined')
4343
envFetch = xhrFetch;
4444
else if (typeof module !== 'undefined' && module.require && typeof process !== 'undefined')
45-
envFetch = fsFetch;
45+
envFetch = nodeFetch;
4646
else if (typeof self !== 'undefined' && self.fetch)
4747
envFetch = fetchFetch;
4848

core/loader-polyfill.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Loader.prototype.load = function(key, parent) {
8484
* Registry has been adjusted to use Namespace objects over ModuleStatus objects
8585
* as part of simplifying loader API implementation
8686
*/
87+
var iteratorSupport = typeof Symbol !== 'undefined' && Symbol.iterator;
8788
function Registry() {
8889
this._registry = {};
8990
}
@@ -92,31 +93,35 @@ Registry.prototype.constructor = function() {
9293
throw new TypeError('Custom registries cannot be created.');
9394
};
9495

95-
if (typeof Symbol !== 'undefined' && Symbol.iterator) {
96+
if (iteratorSupport) {
9697
// 4.4.2
9798
Registry.prototype[Symbol.iterator] = function() {
98-
var self = this;
99-
return arrayValues(Object.keys(this).map(function(key) {
100-
return [key, self[key]]
101-
}));
99+
return this.entries()[Symbol.iterator]();
102100
};
101+
103102
// 4.4.3
104-
Registry.prototype.entries = Registry.prototype[Symbol.iterator];
103+
Registry.prototype.entries = function() {
104+
var registry = this._registry;
105+
return arrayValues(Object.keys(registry).map(function(key) {
106+
return [key, registry[key]];
107+
}));
108+
};
105109
}
110+
106111
// 4.4.4
107112
Registry.prototype.keys = function() {
108-
return arrayValues(Object.keys(this));
113+
return arrayValues(Object.keys(this._registry));
109114
};
110115
// 4.4.5
111116
Registry.prototype.values = function() {
112-
var self = this;
113-
return arrayValues(Object.keys(this).map(function(key) {
114-
return self[key];
117+
var registry = this._registry;
118+
return arrayValues(Object.keys(registry).map(function(key) {
119+
return registry[key];
115120
}));
116121
};
117122
// 4.4.6
118123
Registry.prototype.get = function(key) {
119-
return this._registry[key] || undefined;
124+
return this._registry[key];
120125
};
121126
// 4.4.7
122127
Registry.prototype.set = function(key, namespace) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
],
3434
"scripts": {
3535
"test": "node-esml test/runner.js",
36+
"test-inspect": "node --inspect node_modules/.bin/node-esml test/runner.js",
3637
"bench": "node-esml bench/runner.js"
3738
}
3839
}

test-old/browser-script-type-module.js

-13
This file was deleted.
File renamed without changes.

test/2-loader-tests.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import assert from 'assert';
2+
import { Loader, Module, InternalModuleNamespace } from '../core/loader-polyfill.js';
3+
import { pathToFileUrl } from '../core/common.js';
4+
5+
describe('Loader Polyfill API', function() {
6+
var loader = new Loader(pathToFileUrl(process.cwd()));
7+
8+
it('Should be an instance of itself', function() {
9+
assert(loader instanceof Loader);
10+
});
11+
12+
it('Should support the full registry API', function() {
13+
assert(loader.registry);
14+
15+
loader.registry.set('asdf', new InternalModuleNamespace({ asdf: 'asdf' }));
16+
assert(loader.registry.has('asdf'));
17+
var m = loader.registry.get('asdf');
18+
assert(m);
19+
assert(m instanceof InternalModuleNamespace);
20+
assert.equal(m.asdf, 'asdf');
21+
22+
for (var k of loader.registry.keys())
23+
assert.equal(k, 'asdf');
24+
25+
for (var v of loader.registry.values())
26+
assert.equal(v.asdf, 'asdf');
27+
28+
for (var v of loader.registry.entries()) {
29+
assert.equal(v[0], 'asdf');
30+
assert.equal(v[1].asdf, 'asdf');
31+
}
32+
33+
for (var v of loader.registry) {
34+
assert.equal(v[0], 'asdf');
35+
assert.equal(v[1].asdf, 'asdf');
36+
}
37+
38+
assert.equal(loader.registry.delete('asdf'), true);
39+
assert.equal(loader.registry.has('asdf'), false);
40+
});
41+
42+
it('Should support Module construction and execution', function() {
43+
var evaluated = false;
44+
var mutator;
45+
var module = new Module({
46+
a: { value: 'asdf' }
47+
}, function(_mutator) {
48+
mutator = _mutator;
49+
}, function() {
50+
evaluated = true;
51+
mutator.a = 'b';
52+
});
53+
54+
assert.equal(module.a, 'asdf');
55+
56+
Module.evaluate(module);
57+
assert(evaluated);
58+
59+
assert.equal(module.a, 'b');
60+
});
61+
});
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import RegisterLoader from '../../core/register-loader.js';
2+
import { isBrowser, isNode, global, baseURI } from '../../core/common.js';
3+
import { resolveUrlToParentIfNotPlain } from '../../core/resolve.js';
4+
import { scriptLoad, nodeFetch } from '../../core/fetch.js';
5+
6+
/*
7+
* Example System Register loader
8+
*
9+
* Loads modules in the browser and Node as System.register modules
10+
* Uses <script> injection in the browser, and fs in Node
11+
* If the module does not call System.register, an error will be thrown
12+
*/
13+
function SystemRegisterLoader(baseKey) {
14+
baseKey = resolveUrlToParentIfNotPlain(baseKey || (isNode ? process.cwd() : '.'), baseURI) || baseKey;
15+
RegisterLoader.call(this, baseKey);
16+
17+
var loader = this;
18+
19+
// ensure System.register is available
20+
global.System = global.System || {};
21+
if (typeof global.System.register == 'function')
22+
var prevRegister = global.System.register;
23+
global.System.register = function() {
24+
loader.register.apply(loader, arguments);
25+
if (prevRegister)
26+
prevRegister.apply(this, arguments);
27+
};
28+
}
29+
SystemRegisterLoader.prototype = Object.create(RegisterLoader.prototype);
30+
31+
// normalize is never given a relative name like "./x", that part is already handled
32+
// so we just need to do plain name detect to throw as in the WhatWG spec
33+
SystemRegisterLoader.prototype.normalize = function(key, parent, metadata) {
34+
if (key.indexOf(':') === -1)
35+
throw new RangeError('System.register loader does not resolve plain module names, resolving "' + key + '" to ' + parent);
36+
return key;
37+
};
38+
39+
// instantiate just needs to run System.register
40+
// so we load the module name as a URL, and expect that to run System.register
41+
SystemRegisterLoader.prototype.instantiate = function(key, metadata) {
42+
var loader = this;
43+
44+
return new Promise(function(resolve, reject) {
45+
if (isNode)
46+
nodeFetch(key, undefined, function(source) {
47+
eval(source);
48+
loader.processRegisterContext(key);
49+
resolve();
50+
}, reject);
51+
else if (isBrowser)
52+
scriptLoad(key, function() {
53+
loader.processRegisterContext(key);
54+
resolve();
55+
}, reject);
56+
else
57+
throw new Error('No fetch system defined for this environment.');
58+
});
59+
};
60+
61+
export default SystemRegisterLoader;

test/runner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ Promise.all(tests.map((test) => loader.import(path.resolve('test/' + test))))
1717
setTimeout(function() {
1818
throw err;
1919
});
20-
});
20+
});

0 commit comments

Comments
 (0)