Skip to content

Loading…

Asynchronous modules loaders support #18

Open
wants to merge 2 commits into from

1 participant

@paul-lysak

I've added functions jasmine.dependencyLoading() and jasmine.dependencyLoaded() in order to support anynchronous module loaders.
First function can be called to inform that module load has started, and the second to inform that module load has finished.
Env.execute() call will be delayed until all modules finish loading.

Script WaitForCurl.js uses these functions to support module loading by curl.js library.
Similar WaitForMyFavoriteLibrary.js adapters can be written for another libraries with asynchronous module support, like YUI, Dojo, RequireJS etc.

U-SYNAPSE\Pa... added some commits
U-SYNAPSE\Pavel.Lysak Added callbacks for anynchronous module loading.
This time with correct changes history :)
059b768
U-SYNAPSE\Pavel.Lysak Added callbacks for anynchronous module loading 181f610
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 12, 2011
  1. Added callbacks for anynchronous module loading.

    U-SYNAPSE\Pavel.Lysak committed
    This time with correct changes history :)
  2. Added callbacks for anynchronous module loading

    U-SYNAPSE\Pavel.Lysak committed
Showing with 92 additions and 2 deletions.
  1. +40 −2 src/JasmineAdapter.js
  2. +52 −0 src/WaitForCurl.js
View
42 src/JasmineAdapter.js
@@ -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){
@@ -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;
+ }
+ };
+ }
};
View
52 src/WaitForCurl.js
@@ -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;
+ }
+
+})();
Something went wrong with that request. Please try again.