Skip to content

Commit

Permalink
Big tests update:
Browse files Browse the repository at this point in the history
1. New test to ensure that src/core/*.js all appear in src/core.json
2. New dev dependency: es6-shim (required for Promises on ancient versions of NodeJS)
3. Document events and dynamic in core.json
4. (global) contextify core/event.js code
5. Move Jasmine helper "Help" to more meaningful name: "interop"
   5.a) Implement NodeJS/browser shims for including code, loading files, and booting core.
6. Port tests (context, event) to use interop
7. Clean up core/event.js tests to deal with global namespace pollution
  • Loading branch information
mdittmer committed Jan 28, 2016
1 parent 219c16d commit 7ab88a5
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 87 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "vjlofvhjfgm",
"version": "0.0.1",
"devDependencies": {
"es6-shim": "^0.34.2",
"istanbul": "^0.4.2",
"jasmine": "^2.4.1",
"jsdom": "^3.1.2"
Expand Down
4 changes: 3 additions & 1 deletion src/core.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[
"core/context.js"
"core/context.js",
"core/event.js",
"core/dynamic.js"
]
41 changes: 20 additions & 21 deletions src/core/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
* limitations under the License.
*/

var GLOBAL = global || this;
var X = GLOBAL.X;

/** Publish and Subscribe Event Notification Service. **/

// MODEL({
// name: 'EventService',
var EventService = {
X.EventService = {

// constants: {
// /** Used as topic suffix to specify broadcast to all sub-topics. **/
Expand All @@ -30,15 +33,15 @@ var EventService = {
// /** Create a "one-time" listener which unsubscribes itself after its first invocation. **/
oneTime: function(listener) {
return function() {
listener.apply(this, EventService.argsToArray(arguments));
listener.apply(this, X.EventService.argsToArray(arguments));
arguments[2](); // the unsubscribe fn
};
},

/** Log all listener invocations to console. **/
consoleLog: function(listener) {
return function() {
var args = EventService.argsToArray(arguments);
var args = X.EventService.argsToArray(arguments);
console.log(args);

listener.apply(this, args);
Expand All @@ -53,7 +56,8 @@ var EventService = {
* the smallest delay that humans aren't able to perceive.
**/
merged: function(listener, opt_delay, opt_X) {
var setTimeoutX = ( opt_X && opt_X.setTimeout ) || setTimeout;
var X = opt_X || GLOBAL.X;
var setTimeoutX = X.setTimeout;
var delay = opt_delay || 16;

return function() {
Expand All @@ -67,7 +71,7 @@ var EventService = {
triggered = true;
setTimeoutX(function() {
triggered = false;
var args = EventService.argsToArray(lastArgs);
var args = X.EventService.argsToArray(lastArgs);
lastArgs = null;
listener.apply(this, args);
}, delay);
Expand Down Expand Up @@ -147,15 +151,15 @@ var EventService = {
},
/** convenience method to turn 'arguments' into a real array */
argsToArray: function(args) {
return EventService.appendArguments([], args, 0);
return GLOBAL.X.EventService.appendArguments([], args, 0);
},

// });
}

// MODEL({
// name: 'EventPublisher',
var EventPublisher = {
X.EventPublisher = {

// properties: [
subs_: null, // inited to {} when first used
Expand All @@ -176,7 +180,7 @@ var EventPublisher = {
if ( ! map ) return false; // if nothing to check, fail
if ( this.hasDirectListeners_(map) ) return true; // if any listeners at this level, we're good
var topic = opt_topic[t];
if ( topic == EventService.WILDCARD ) {
if ( topic == X.EventService.WILDCARD ) {
// if a wildcard is specified, find any listener at all
return this.hasAnyListeners_(map);
}
Expand All @@ -197,13 +201,13 @@ var EventPublisher = {
this.subs_,
0,
topic,
EventService.appendArguments([this, topic, null], arguments, 1)) : // null: to be replaced with the unsub object
X.EventService.appendArguments([this, topic, null], arguments, 1)) : // null: to be replaced with the unsub object
0;
},

/** Publish asynchronously. **/
publishAsync: function(topic) {
var args = EventService.argsToArray(arguments);
var args = X.EventService.argsToArray(arguments);
var self = this;
setTimeout( function() { self.publish.apply(self, args); }, 0);
},
Expand Down Expand Up @@ -269,7 +273,7 @@ var EventPublisher = {
var t = topic[topicIndex];

// wildcard publish, so notify all sub-topics, instead of just one
if ( t == EventService.WILDCARD ) {
if ( t == X.EventService.WILDCARD ) {
return this.notifyListeners_(topic, map, msg, topic.slice(0, topicIndex-1));
}
if ( t ) count += this.pub_(map[t], topicIndex+1, topic, msg);
Expand Down Expand Up @@ -367,15 +371,15 @@ var EventPublisher = {


//});
}
};


// /** Extend EventPublisher with support for dealing with property-change notification. **/
// MODEL({
// name: 'PropertyChangePublisher',
var PropertyChangePublisher = {
X.PropertyChangePublisher = {
// extends: 'EventPublisher',
__proto__: EventPublisher,
__proto__: X.EventPublisher,

// constants: {
// /** Root for property topics. **/
Expand Down Expand Up @@ -412,7 +416,7 @@ var PropertyChangePublisher = {

/** Indicates that one or more unspecified properties have changed. **/
globalChange: function() {
this.publish(this.propertyTopic(EventService.WILDCARD), null, null);
this.publish(this.propertyTopic(X.EventService.WILDCARD), null, null);
},

/** Adds a listener for all property changes. **/
Expand Down Expand Up @@ -450,9 +454,4 @@ var PropertyChangePublisher = {
// // }
// }
// });
}

exports.EventPublisher = EventPublisher;
exports.EventService = EventService;
exports.PropertyChangePublisher = PropertyChangePublisher;

};
2 changes: 0 additions & 2 deletions test/helpers/Help.js

This file was deleted.

63 changes: 63 additions & 0 deletions test/helpers/interop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var GLOBAL = global || this;
var NODEJS = typeof module !== 'undefined' && module.exports;

if (NODEJS) require('es6-shim');

// TODO(markdittmer): Make the non-NodeJS case match with wherever we expect
// Jasmine's SpecRunner.html.
var SRC_DIR = NODEJS ? process.cwd() + '/src' : 'src';

GLOBAL.loadSrcScript = function(path) {
return new Promise(function(resolve, reject) {
if (NODEJS) {
var requirePath = SRC_DIR + '/' + path;
var success = true;
try {
require(requirePath);
} catch (e) {
success = false;
reject(e);
}
if (success) resolve();
} else {
var document = GLOBAL.document;
var script = document.createElement('script');
script.addEventListener('load', function() { resolve(); });
script.addEventListener('error', function() { reject(); });
script.setAttribute('src', SRC_DIR + '/' + path + '.js');
document.head.appendChild(script);
}
});
};

GLOBAL.readSrcFile = function(path, callback) {
return new Promise(function(resolve, reject) {
if (NODEJS) {
var fileContents = require('fs').readFileSync(SRC_DIR + '/' + path);
resolve(fileContents);
} else {
var xhr = new XMLHttpRequest();
xhr.open(SRC_DIR + '/' + path);
xhr.addEventListener('load', function() { resolve(xhr.responseText); });
xhr.addEventListener('error', function(error) { reject(error); });
xhr.send();
}
});
};

var coreFileNamesPromise =
GLOBAL.readSrcFile('core.json').then(function(coreFilesJSON) {
return JSON.parse(coreFilesJSON);
});

GLOBAL.loadCoreTo = function(lastCoreFileName) {
return coreFileNamesPromise.then(function(coreFileNames) {
var cont = true;
return coreFileNames.reduce(function(promise, coreFileName) {
if (!cont) return promise;
promise = promise.then(GLOBAL.loadSrcScript(coreFileName));
if (coreFileName === lastCoreFileName) cont = false;
return promise;
}, Promise.resolve());
});
};
4 changes: 3 additions & 1 deletion test/js/core/context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var GLOBAL = global || this;
require('../../../src/core/context');

var corePromise = GLOBAL.loadCoreTo('core/context.js');

describe('ConteXt object', function() {
beforeEach(function(done) { corePromise.then(done); });

it('Expect global context to exist', function() {
expect(GLOBAL.X).toBeTruthy();
Expand Down
Loading

0 comments on commit 7ab88a5

Please sign in to comment.