diff options
| author | Alon Levy <alon@pobox.com> | 2014-10-26 12:24:11 +0200 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2014-10-26 12:27:50 +0200 |
| commit | 07373747c7e709cadbe73dd024a11b8336034f6d (patch) | |
| tree | 4a28da0f5bb938f355047713d2547022afacd1ab | |
| parent | 9e7c7ddc7de091d404e83efbab113f35ebde2b77 (diff) | |
tests: forgot some more files
| -rw-r--r-- | tests/anotherScript.js | 2 | ||||
| -rw-r--r-- | tests/test_app.js | 32 | ||||
| -rw-r--r-- | tests/test_globals.js | 30 | ||||
| -rw-r--r-- | tests/test_require_js.js | 25 | ||||
| -rw-r--r-- | tests/test_script_jsdom.js | 9 | ||||
| -rw-r--r-- | tests/test_sub_process.js | 9 | ||||
| -rw-r--r-- | tests/test_util.js | 44 |
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; |
