Permalink
Browse files

Move `load` test helper into util.js.

  • Loading branch information...
lddubeau authored and domenic committed Jan 27, 2014
1 parent 5fdcbbc commit fd21229879316a974b7172252c09d61f9ea18b7a
Showing with 38 additions and 29 deletions.
  1. +1 −23 test/level2/html.js
  2. +37 −6 test/util.js
View
@@ -3,30 +3,8 @@ var path = require('path');
var jsdom = require("../../lib/jsdom");
var toPathname = require("../util").toPathname(__dirname);
var toFileUrl = require("../util").toFileUrl(__dirname);
+var load = require("../util").load(__dirname + "/html/");
-var fileCache = {};
-var load = function(name, options) {
- if (!options) {
- options = {};
- }
-
- var file = path.resolve(__dirname, "html/files/" + name + ".html");
-
- if(!options.url) {
- options.url = toFileUrl(file);
- }
-
- var contents = fileCache[file] || fs.readFileSync(file, 'utf8'),
- doc = jsdom.jsdom(null, null, options),
- window = doc.createWindow();
-
- doc.parent = window;
- window.loadComplete = function() {};
-
- doc.innerHTML = contents;
- fileCache[file] = contents;
- return doc;
-};
var level2 = require("../../lib/jsdom/level2/html").dom.level2.html;
var getImplementation = function() {
var doc = new level2.HTMLDocument();
View
@@ -1,4 +1,6 @@
var path = require('path');
+var jsdom = require('../lib/jsdom');
+var fs = require('fs');
function toPathname(dirname, relativePath) {
var pathname = path.resolve(dirname, relativePath).replace(/\\/g, '/');
@@ -8,14 +10,43 @@ function toPathname(dirname, relativePath) {
return pathname;
}
+function toFileUrl(dirname, relativePath) {
+ return 'file://' + toPathname(dirname, relativePath);
+}
+
exports.toFileUrl = function (dirname) {
- return function (relativePath) {
- return 'file://' + toPathname(dirname, relativePath);
- };
+ return function (relativePath) {
+ return toFileUrl(dirname, relativePath);
+ };
};
exports.toPathname = function (dirname) {
- return function (relativePath) {
- return toPathname(dirname, relativePath);
- };
+ return function (relativePath) {
+ return toPathname(dirname, relativePath);
+ };
+};
+
+exports.load = function (dirname) {
+ var fileCache = Object.create(null);
+
+ return function (name, options) {
+ options = options || {};
+
+ var file = path.resolve(dirname, 'files/' + name + '.html');
+
+ if (!options.url) {
+ options.url = toFileUrl(dirname, file);
+ }
+
+ var contents = fileCache[file] || fs.readFileSync(file, 'utf8');
+ var doc = jsdom.jsdom(null, null, options);
+ var window = doc.createWindow();
+
+ doc.parent = window;
+ window.loadComplete = function () {};
+
+ doc.innerHTML = contents;
+ fileCache[file] = contents;
+ return doc;
+ };
};

0 comments on commit fd21229

Please sign in to comment.