Skip to content

Instantly share code, notes, and snippets.

@abriening
Created October 4, 2013 13:09
Show Gist options
  • Save abriening/6825638 to your computer and use it in GitHub Desktop.
Save abriening/6825638 to your computer and use it in GitHub Desktop.
Loads html fixtures for javascript "unit" testing. Works with FireFox and fixtures in the same folder (will get same-origin-policy errors otherwise). Works with mocha/qunit/jasmine via phantom. I should remove dependency on jQuery ($ parameter).
function Fixtures($) {
var id, container;
function setup(files) {
for(var i = 0; i < files.length; i++) {
load(files[i])
}
}
function createContainer() {
container = $('<div id="' + id + '" />');
$('body').prepend(container);
}
function cleanup() {
if (container) container.remove();
id = "fixture-" + hash();
container = undefined;
}
function hash() {
return new Date().getTime();
}
function load(path) {
if (container == undefined) createContainer();
var request = new XMLHttpRequest()
request.open("GET", path + "?_=" + hash(), false)
request.send(null)
container.append(request.responseText)
}
cleanup()
setup(Array.prototype.slice.call(arguments, 1))
return { load: load, cleanup: cleanup }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment