Skip to content

Commit

Permalink
added tests for jsdom.env.processArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpvar committed Feb 16, 2011
1 parent d25d232 commit 0e72da6
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 11 deletions.
94 changes: 83 additions & 11 deletions lib/jsdom.js
Expand Up @@ -158,13 +158,11 @@ exports.jQueryify = exports.jsdom.jQueryify = function (window /* path [optional
};


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

if(!config || !config.done) {
throw new Error('JSDOM: a config and config.done callback must be supplied.');
}
exports.env = exports.jsdom.env = function() {

var
args = Array.prototype.slice.call(arguments),
config = exports.env.processArguments(args),
callback = config.done,
processHTML = function(err, html) {
html += '';
Expand All @@ -179,12 +177,13 @@ exports.env = exports.jsdom.env = function(config) {
config.code = [config.code];
}

var window = exports.html(html).createWindow(),
features = window.document.implementation._features,
docsLoaded = 0,
totalDocs = config.code.length;
readyState = null,
errors = null;
var
window = exports.html(html).createWindow(),
features = window.document.implementation._features,
docsLoaded = 0,
totalDocs = config.code.length;
readyState = null,
errors = null;

if (!window || !window.document) {
return callback(new Error('JSDOM: a window object could not be created.'));
Expand Down Expand Up @@ -240,3 +239,76 @@ exports.env = exports.jsdom.env = function(config) {
}
}
};

/*
Since jsdom.env() is a helper for quickly and easily setting up a
window with scripts and such already loaded into it, the arguments
should be fairly flexible. Here are the requirements
1) collect `html` (url, string, or file on disk) (STRING)
2) load `code` into the window (array of scripts) (ARRAY)
3) callback when resources are `done` (FUNCTION)
4) configuration (OBJECT)
Rules:
+ if there is one argument it had better be an object with atleast
a `html` and `done` property (other properties are gravy)
+
*/
exports.env.processArguments = function(args) {

if (!args || !args.length || args.length < 1) {
throw new Error('No arguments passed to jsdom.env().');
}

var
required = ['html', 'done'],
config = {
code : []
},
l = args.length
;
if (l === 1) {
config = args[0];
} else {
args.forEach(function(v) {
var type = typeof v;
if (!v) {
return
}
if (type === 'string' || v + '' === v) {
config.html = v;
} else if (type === 'object') {
// Array
if (v.length && v[0]) {
config.code = v;
} else {
// apply missing required properties if appropriate
required.forEach(function(req) {
if (v &&
typeof v[req] !== 'undefined' &&
typeof config[req === 'undefined'])
{
config[req] = v[req];
delete v[req];
}
})
config.config = v;
}
} else if (type === 'function') {
config.done = v;
}
});
}

required.forEach(function(req) {
if (typeof config[req] === 'undefined') {
throw new Error("jsdom.env requires a '" + req + "' argument");
}
})

return config;
};
81 changes: 81 additions & 0 deletions test/jsdom/index.js
Expand Up @@ -125,6 +125,87 @@ exports.tests = {
});
},

env_processArguments_invalid_args : function() {
var caught = 0;

try {
jsdom.env.processArguments();
} catch (e) {
caught++;
}

try {
jsdom.env.processArguments({});
} catch (e) {
caught++;
}

try {
jsdom.env.processArguments([{
html : 'abc123'
}]);
} catch (e) {
caught++;
}

try {
jsdom.env.processArguments([{
done : function() {}
}]);
} catch (e) {
caught++;
}

assertEquals("the previous ops should be caught", caught, 4);
},

env_processArguments_config_object : function() {
var config = jsdom.env.processArguments([{
html : "",
done : function() {}
}]);

assertNotNull("has done", config.done);
assertNotNull("has html", config.html);
},

env_processArguments_object_and_callback : function() {
var config = jsdom.env.processArguments([{
html : ""
},
function() {}
]);

assertNotNull("has done", config.done);
assertNotNull("has html", config.html);
},

env_processArguments_all_args_no_config : function() {
var config = jsdom.env.processArguments([
"<html></html>",
['script.js'],
function() {}
]);

assertNotNull("has done", config.done);
assertNotNull("has html", config.html);
assertEquals('script length should be 1', 1, config.code.length);
},

env_processArguments_all_args_with_config : function() {
var config = jsdom.env.processArguments([
"<html></html>",
['script.js'],
{ features : [] },
function() {},
]);

assertNotNull("has done", config.done);
assertNotNull("has html", config.html);
assertEquals('script length should be 1', 1, config.code.length);
assertNotNull("has config.features", config.config.features);
},

plain_window_document : function() {
var window = (jsdom.createWindow());
assertTrue("jsdom.createWindow() should create a documentless window",
Expand Down

0 comments on commit 0e72da6

Please sign in to comment.