summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/anotherScript.js2
-rw-r--r--tests/test_app.js32
-rw-r--r--tests/test_globals.js30
-rw-r--r--tests/test_require_js.js25
-rw-r--r--tests/test_script_jsdom.js9
-rw-r--r--tests/test_sub_process.js9
-rw-r--r--tests/test_util.js44
7 files changed, 151 insertions, 0 deletions
diff --git a/tests/anotherScript.js b/tests/anotherScript.js
new file mode 100644
index 00000000..93da9fbf
--- /dev/null
+++ b/tests/anotherScript.js
@@ -0,0 +1,2 @@
+debugger
+console.log(window.__myObject);
diff --git a/tests/test_app.js b/tests/test_app.js
new file mode 100644
index 00000000..91f6b6cd
--- /dev/null
+++ b/tests/test_app.js
@@ -0,0 +1,32 @@
+// mocks just to all run_tests to succeed
+if (typeof define == 'undefined') {
+ function define(mod, cb) {
+ }
+}
+if (typeof document == 'undefined') {
+ var document = {};
+}
+
+(function() {
+var config = {
+ //urlArgs: "bust=" + (new Date()).getTime(), // NOTE: useful for debugging
+ paths: {
+ jquery: 'external/jquery',
+ 'jquery-ui': 'external/jquery-ui',
+ caret: 'external/caret',
+ 'd3': 'external/d3/d3',
+ autocomplete: 'external/autocomplete',
+ FileSaver: 'external/FileSaver',
+ }
+}
+
+define('test', function() {
+ console.log('hello from test factory');
+ return {f:function(){console.log(42);}};
+});
+
+console.log('test_app: running under node');
+config.baseUrl = '../scripts/';
+
+document.config = config;
+}());
diff --git a/tests/test_globals.js b/tests/test_globals.js
new file mode 100644
index 00000000..afd79ecf
--- /dev/null
+++ b/tests/test_globals.js
@@ -0,0 +1,30 @@
+var base = require('./base');
+var util = require('../scripts/util');
+
+var count;
+
+function object_length(obj) {
+ var count = 0;
+ for (var k in obj) {
+ count += 1;
+ }
+ return count;
+}
+
+var win_before;
+var win_after;
+
+base.run_tests({
+ created: function(errors, window) {
+ count = object_length(window);
+ console.log('before fields count: ' + count);
+ win_before = util.set_from_object(window);
+ },
+ done:function (errors, window) {
+ var new_count = object_length(window);
+ console.log('after fields count: ' + new_count);
+ console.log('new fields count: ' + (new_count - count));
+ win_after = util.set_from_object(window);
+ console.log(util.set_diff(win_after, win_before).a_b.join(','));
+ }
+});
diff --git a/tests/test_require_js.js b/tests/test_require_js.js
new file mode 100644
index 00000000..9cecc019
--- /dev/null
+++ b/tests/test_require_js.js
@@ -0,0 +1,25 @@
+var jsdom = require("jsdom").jsdom;
+var document = jsdom();
+var window = document.parentWindow;
+var addScript = require('./test_util').addScript;
+
+window.console.log = console.log;
+window.is_node = true;
+
+addScript(window, '../scripts/external/require.js')
+ .load_next('test_app.js')
+ .done(function () {
+ var config = document.config
+ var requirejs = window.requirejs;
+ var require = window.require;
+ console.log('callback after test_app.js loading');
+ console.log(config);
+ require.config(config);
+ requirejs(['require', 'test', './test2.js', 'main'], function(require, test, test2, main) {
+ console.log('here we are after test_app prerequisites');
+ debugger;
+ test.f();
+ main.main();
+ process.exit();
+ });
+ });
diff --git a/tests/test_script_jsdom.js b/tests/test_script_jsdom.js
new file mode 100644
index 00000000..1ae5835f
--- /dev/null
+++ b/tests/test_script_jsdom.js
@@ -0,0 +1,9 @@
+var jsdom = require("jsdom").jsdom;
+var window = jsdom().parentWindow;
+
+window.__myObject = { foo: "bar" };
+
+var scriptEl = window.document.createElement("script");
+scriptEl.src = "../tests/anotherScript.js";
+window.console.log = console.log;
+window.document.body.appendChild(scriptEl);
diff --git a/tests/test_sub_process.js b/tests/test_sub_process.js
new file mode 100644
index 00000000..95f5126e
--- /dev/null
+++ b/tests/test_sub_process.js
@@ -0,0 +1,9 @@
+var spawn = require('child_process').spawn;
+
+var ls = spawn('ls');
+ls.stdout.on('data', function (data) {
+ console.log('stdout ' + data.length);
+});
+ls.on('close', function(code, signal) {
+ console.log('closed, ' + code + ', ' + signal);
+});
diff --git a/tests/test_util.js b/tests/test_util.js
new file mode 100644
index 00000000..7780b0ad
--- /dev/null
+++ b/tests/test_util.js
@@ -0,0 +1,44 @@
+function new_deffer() {
+ var deffer = {
+ cb: null,
+ call_cb: false,
+ };
+ deffer.done = function(cb) {
+ deffer.cb = cb;
+ if (deffer.call_cb) {
+ // warning: going down callstack - should probably use setInterval
+ deffer.call_cb = false; // first do this to avoid endless recursion
+ return deffer.cb();
+ }
+ };
+ deffer.on_done = function() {
+ if (this.cb) {
+ this.call_cb = false;
+ this.cb();
+ } else {
+ this.call_cb = true;
+ }
+ };
+ return deffer;
+}
+
+function addScript(window, name) {
+ var scriptEl = window.document.createElement("script");
+ scriptEl.src = name;
+ var deffer = new_deffer();
+ deffer.load_next = function(script) {
+ var sec_deffer = new_deffer();
+ addScript(window, script).done(
+ function() { sec_deffer.on_done(); })
+ return sec_deffer;
+ }
+ function onload_cb() {
+ console.log('addScript: loaded ' + name);
+ deffer.on_done();
+ }
+ scriptEl.onload = onload_cb;
+ window.document.body.appendChild(scriptEl);
+ return deffer;
+}
+
+exports.addScript = addScript;