Skip to content

Instantly share code, notes, and snippets.

@prettycode
Last active December 16, 2015 07:49
Show Gist options
  • Save prettycode/5401842 to your computer and use it in GitHub Desktop.
Save prettycode/5401842 to your computer and use it in GitHub Desktop.
Module loader for testing
var example = {
//js: 'https://gist.github.com/prettycode/5401842/raw/example.js'
js: 'http://fiddle.jshell.net/js/heyoffline.js?StillNoSpring'
}
loadScript(example.js, {
document: {
createElement: function(tag) {
return {
setAttribute: function(key, value) {
this._attribute[key] = value;
},
submit: function() {
if (this._attribute['action'] !== example.js) {
throw;
}
}
};
}
}
});
// this is the script that needs to be tested
function submitForm(url, data) {
var formEl = document.createElement('form');
for (var key in data) {
if (data.hasOwnProperty(key)) {
formEl.setAttribute(key, data[key]);
}
}
formEl.action = url;
formEl.submit();
}
function loadScript(url, context) {
var scriptEl = document.createElement('script'),
contextKeys = Object.keys(context).join();
jQuery.get(url, function(scriptText) {
var tempKey = '$' + Date.now();
window[tempKey] = Object.keys(context).map(function(key) {
return context[key];
});
scriptEl.innerHTML = '' +
'(function(' + contextKeys + ') {' +
scriptText +
'}).apply(this, window.' + tempKey + ');'
;
document.body.appendChild(scriptEl);
delete window[tempKey];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment