Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
working on tests, per #113
Browse files Browse the repository at this point in the history
  • Loading branch information
getify committed Apr 4, 2017
1 parent 48a8b01 commit 436380b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 10 additions & 7 deletions scripts/node-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
8 changes: 5 additions & 3 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
<html>
<head>
<meta charset="utf-8" />
<title>Mock-DOM-Resources Test Suite</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.1.1.css">
<title>LABjs Test Suite</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.3.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>

<script src="../src/index.js"></script>
<script src="https://code.jquery.com/qunit/qunit-2.1.1.js"></script>
<script>var get$LAB = $LAB.noConflict().sandbox</script>
<script src="../node_modules/mock-dom-resources/dist/mock-dom.js"></script>
<script src="https://code.jquery.com/qunit/qunit-2.3.0.js"></script>
<script src="tests.js"></script>
</body>
</html>
28 changes: 26 additions & 2 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -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();
} );
} );


Expand Down

0 comments on commit 436380b

Please sign in to comment.