Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous modules loaders support #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/JasmineAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@
* @author olmo.maldonado@gmail.com (Olmo Maldonado)
*/
(function(){
var depLoadedListener = null;
var dependenciesLoading = [];
dependenciesLoading.pendingCount = 0;


jasmine.dependencyLoading = function (dependencyName) {
if(!dependenciesLoading.pendingCount)
dependenciesLoading.pendingCount = 0;
var i = dependenciesLoading.indexOf(dependencyName)
if(i < 0) {
dependenciesLoading.push(dependencyName);
dependenciesLoading.pendingCount++;
}
}

jasmine.dependencyLoaded = function (dependencyName) {
var dli = dependenciesLoading.indexOf(dependencyName)
if(dli <0 ) {
return;
}
delete dependenciesLoading[dli];
dependenciesLoading.pendingCount--;
var hasMore = dependenciesLoading.pendingCount>0;
if(depLoadedListener) {
depLoadedListener(dependencyName, hasMore);
}
}


var Env = function(onTestDone, onComplete){
Expand All @@ -29,8 +56,19 @@ Env.prototype.exclusive = 0;

Env.prototype.execute = function(){
collectMode = false;
playback();
jasmine.Env.prototype.execute.call(this);
if(dependenciesLoading.pendingCount == 0) {
playback();
jasmine.Env.prototype.execute.call(this);
} else {
var env = this;
depLoadedListener = function(depGroup, hasMore) {
if(!hasMore) {
playback();
jasmine.Env.prototype.execute.call(env);
depLoadedListener = null;
}
};
}
};


Expand Down
52 changes: 52 additions & 0 deletions src/WaitForCurl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Makes JasmineAdapter waiting for CurlJS dependencies
*/
(function() {

//wrap around curl() function to inform JasmineAdapter about loading dependencies
if(typeof window.curl == "function" && !window.curl.asyncJasmineInstrumented) {
var origCurl = window.curl;
window.curl = function() {
var args = [];
for(var i=0; i<arguments.length; i++) {
args[i] = arguments[i];
}
var code = "["+args.join(",")+"]";
jasmine.dependencyLoading(code);
return origCurl.apply(this, args).then(function(res) {
jasmine.dependencyLoaded(code);
}, function(err) {
console.error("Failed to load dependencies", err);
});
}
window.curl.asyncJasmineInstrumented = true; //avoid double instrumentation
} else {
console.warn("curl.js not loaded");
}

/**
* Get URL of the folder from which last script was loaded.
*/
jasmine.util.getLastScriptFolder = function () {
var scripts = document.getElementsByTagName("script");
if(!scripts || scripts.length < 1)
return null;
var scriptUrl = scripts[scripts.length-1].attributes.src.value;
var lastSlash = scriptUrl.lastIndexOf("/");
var folderUrl = lastSlash>0?scriptUrl.substring(0, lastSlash+1):scriptUrl;
return folderUrl;
}

/**
* Resolves references to parent folder. For example, foo/bar/../baz would became foo/baz
*/
jasmine.util.normalizeUrl = function(url) {
var redundantFolderRegex = /\/[\w\d_]+\/\.\.\//;
do {
var urlLength = url.length;
url = url.replace(redundantFolderRegex, "/");
} while(urlLength != url.length)
return url;
}

})();