Skip to content
Permalink
Browse files

Total revamping of the ajax unit tests

  • Loading branch information...
jaubourg committed Nov 26, 2012
1 parent 53a0666 commit d31cef38f2aa76ca71ed18aa97d035a84c7e33e6
Showing with 1,523 additions and 2,314 deletions.
  1. +2 −0 test/.jshintrc
  2. +71 −0 test/data/testinit.js
  3. +29 −1 test/data/testrunner.js
  4. +1,421 −2,313 test/unit/ajax.js
@@ -38,6 +38,7 @@
"stop": true,
"expect": true,
"raises": true,
"ajaxTest": true,
"testIframe": true,
"testIframeWithCallback": true,
"createDashboardXML": true,
@@ -50,6 +51,7 @@
"q": true,
"amdDefined": true,
"fireNative": true,
"Globals": true,
"hasPHP": true,
"isLocal": true,
"originaljQuery": true,
@@ -143,6 +143,77 @@ function url( value ) {
return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random() * 100000, 10);
}

// Ajax testing helper
var ajaxTest = (function() {

var resolved = $.Deferred().resolve();

function getRequests( options ) {
var requests = options.requests || options.request || options || [];
if ( !jQuery.isArray(requests) ) {
requests = [ requests ];
}
return requests;
}

return function( title, expect, options ) {
if ( jQuery.isFunction(options) ) {
options = options();
}
options = options || [];
asyncTest( title, expect, function() {
setTimeout(function() {
if ( options.setup ) {
options.setup();
}
var ajaxSettings = jQuery.ajaxSetup( {}, {} );
aborted = false,
abort = function( reason ) {
if ( !aborted ) {
aborted = true;
ok( false, "unexpected " + reason );
jQuery.each( requests, function( _, request ) {
request.abort();
});
}
},
requestOptions = getRequests( options ),
requests = jQuery.map( requestOptions, function( options ) {
var request = ( options.create || jQuery.ajax )( options );
if ( options.afterSend ) {
options.afterSend( request );
}
return request;
});
requests = jQuery.map( requests, function( request, index ) {
function callIfDefined( type, type2 ) {
var handler = requestOptions[ index ][ type ] || !!requestOptions[ index ][ type2 ];
return handler ? function() {
if ( !aborted && jQuery.isFunction( handler ) ) {
handler.apply( this, arguments );
}
return resolved;
} : function() {
abort( type );
return resolved;
}
}
var promise = request.then( callIfDefined( "done", "success" ), callIfDefined( "fail", "error" ) );
promise.abort = request.abort;
return promise;
});
jQuery.when.apply( jQuery, requests ).done(
options.teardown,
function() {
jQuery.ajaxSetup( ajaxSettings );
setTimeout( start, 0 );
}
);
}, 0 );
});
};
})();

(function () {

this.testIframe = function( fileName, name, fn ) {
@@ -134,6 +134,31 @@ function testSubproject( label, url, risTests ) {
}
}

// Register globals for cleanup and the cleanup code itself
// Explanation at http://perfectionkills.com/understanding-delete/#ie_bugs
var Globals = (function() {

This comment has been minimized.

@dmethvin

dmethvin Nov 26, 2012

Member

A global named Globals to manage globals! 👌

var globals = {};
return QUnit.config.noglobals ? {
register: function( name ) {
globals[ name ] = true;
jQuery.globalEval( "var " + name );
},
cleanup: function() {
var name,
current = globals;
globals = {};
for ( name in current ) {
jQuery.globalEval( "try { " +
"delete " + ( jQuery.support.deleteExpando ? "window['" + name + "']" : name ) +
"; } catch( x ) {}" );
}
}
} : {
register: jQuery.noop,
cleanup: jQuery.noop
};
})();

/**
* QUnit hooks
*/
@@ -292,10 +317,13 @@ function testSubproject( label, url, risTests ) {
// Reset internal jQuery state
jQuery.event.global = {};
if ( ajaxSettings ) {
jQuery.ajaxSettings = jQuery.extend( {}, ajaxSettings );
jQuery.ajaxSettings = jQuery.extend( true, {}, ajaxSettings );
} else {
delete jQuery.ajaxSettings;
}

// Cleanup globals
Globals.cleanup();

// Let QUnit reset the fixture
reset.apply( this, arguments );
Oops, something went wrong.

0 comments on commit d31cef3

Please sign in to comment.
You can’t perform that action at this time.