From 436380b9a329484e3a68b60f7d40b1b6d11c2a9e Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Mon, 3 Apr 2017 20:31:08 -0500 Subject: [PATCH] working on tests, per #113 --- package.json | 3 ++- scripts/node-tests.js | 17 ++++++++++------- src/index.js | 10 +++++----- tests/index.html | 8 +++++--- tests/tests.js | 28 ++++++++++++++++++++++++++-- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index eb869e1..f3d50e5 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "prepublish": "npm run build && npm test" }, "devDependencies": { - "mock-dom-resources": "~4.0.1", + "freshy": "~1.0.2", + "mock-dom-resources": "~5.0.0", "qunitjs": "~2.3.0", "stable-timers": "~1.0.0", "uglify-js": "~2.8.21" diff --git a/scripts/node-tests.js b/scripts/node-tests.js index 0c58f3d..f40b181 100755 --- a/scripts/node-tests.js +++ b/scripts/node-tests.js @@ -2,23 +2,26 @@ "use strict"; +global.QUnit = require("qunitjs"); var path = require("path"); +var freshy = require("freshy"); // make timers behave more stably require("stable-timers").replaceGlobals(); global.$DOM = require("mock-dom-resources"); -global.$DOM.replaceGlobals = true; -var win = global.$DOM({ log(){}, error(){} }); - -// TEMP HACK -win.location = global.location = { href: "https://some.tld/", protocol: "https:", search: "" }; -require(path.join("..","src","index.js")); -global.QUnit = require("qunitjs"); +global.get$LAB = get$LAB; require("../tests/qunit.config.js"); require("../tests/tests.js"); QUnit.start(); + + +// ********************************** + +function get$LAB() { + return freshy.reload(path.join("..","src","index.js")).$LAB; +} diff --git a/src/index.js b/src/index.js index 9362534..f2c44c2 100644 --- a/src/index.js +++ b/src/index.js @@ -174,13 +174,13 @@ // resource already (pre)loaded? if (perfEntries.length > 0) { registryEntry.preloaded = true; - console.log( "markup link already preloaded", href ); + // logMsg( "markup link already preloaded", href ); } else { - console.log( "listening for markup link preload", href ); + // logMsg( "listening for markup link preload", href ); // listen for preload to complete elem.addEventListener( "load", function resourcePreloaded(){ - console.log("markup link preloaded!",href); + // logMsg("markup link preloaded!",href); elem.removeEventListener( "load", resourcePreloaded ); registryEntry.preloaded = true; notifyRegistryListeners( registryEntry ); @@ -366,7 +366,7 @@ // listen for preload to complete elem.addEventListener( "load", function resourcePreloaded(){ - console.log("resource preloaded!",resourceRecord.src); + // logMsg("resource preloaded!",resourceRecord.src); elem.removeEventListener( "load", resourcePreloaded ); elem.parentNode.removeChild( elem ); registryEntry.preloaded = true; @@ -457,7 +457,7 @@ // listen for load to complete elem.addEventListener( "load", function resourceLoaded(){ - console.log("resource loaded!",resourceRecord.src); + // logMsg("resource loaded!",resourceRecord.src); elem.removeEventListener( "load", resourceLoaded ); registryEntry.complete = true; notifyRegistryListeners( registryEntry ); diff --git a/tests/index.html b/tests/index.html index 9f30f46..614a8b9 100644 --- a/tests/index.html +++ b/tests/index.html @@ -2,15 +2,17 @@ -Mock-DOM-Resources Test Suite - +LABjs Test Suite +
- + + + diff --git a/tests/tests.js b/tests/tests.js index b5544a0..7fa24ac 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,8 +1,32 @@ "use strict"; -QUnit.test( "placeholder", function test(assert){ +QUnit.test( "load a script", function test(assert){ + var done = assert.async(); assert.expect( 1 ); - assert.ok( true, "placeholder" ); + + var { logs, log, error } = collectLogs(); + + // only replace globals in node (fails in browser) + $DOM.replaceGlobals = (typeof window == "undefined"); + $DOM( { + sequentialIds: true, + log, + error, + location: "http://some.tld/", + resources: [ + { url: "http://some.tld/a.js", preloadDelay: 10, preload: true, loadDelay: 5, load: true } + ] + } ); + + var $LAB = get$LAB(); + + $LAB + .script( "a.js" ) + .wait( function(){ + assert.ok( true, "a.js" ); + // console.log(logs); + done(); + } ); } );