Skip to content

Commit

Permalink
[test/api] added env export to easily set up a dom+resource environme…
Browse files Browse the repository at this point in the history
…nt/closure, detatched tests added not complete
  • Loading branch information
hij1nx committed Feb 10, 2011
1 parent adea6d6 commit 46f149c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,46 @@ exports.jQueryify = exports.jsdom.jQueryify = function (window /* path [optional

return window;
};


exports.env = exports.jsdom.env = function(config, callback) {

var self = this;

if(!config || !callback) {
throw new Error("JSDOM: a config and callback must be supplied.");
}

fs.readFile(__dirname + config.document, function(err, document) {

if(err) {
throw new Error("JSDOM: the document for the new environment could not be loaded.");
}

var window = self.html(document.toString()).createWindow(),
features = window.document.implementation._features,
docsLoaded = 0, readyState = null;

if (!window || !window.document) {
throw new Error("JSDOM: a window object could not be created.");
}

window.document.implementation.addFeature('FetchExternalResources', ['script']);
window.document.implementation.addFeature('ProcessExternalResources', ['script']);
window.document.implementation.addFeature('MutationEvents', ["1.0"]);

config.scripts.forEach(function(path) {
var script = window.document.createElement("script");
script.onload = function() { docsLoaded++; };
script.src = __dirname + path;
window.document.head.appendChild(script);
});

docsLoader = setInterval(function() {
if(docsLoaded == config.scripts.length) {
clearInterval(docsLoader);
callback(window);
}
}, 1);
});
};
8 changes: 8 additions & 0 deletions test/env.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>hello, Node.js!</title>
</head>
<body>
</body>
</html>
21 changes: 21 additions & 0 deletions test/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

var jsdom = require("../lib/jsdom");

jsdom.env({

scripts: ['/../example/jquery/jquery.js'],
document: '/../test/env.html'

},

function(window) {

var $ = window.jQuery;

$("body").html("Let's Rock!")

console.log($('html').html());

}

);

0 comments on commit 46f149c

Please sign in to comment.