Created
October 4, 2013 13:09
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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