Greatly simplifies ajaxTest

This commit is contained in:
jaubourg 2012-11-26 11:49:20 +01:00
parent d31cef38f2
commit 603e70b90e

View File

@ -144,75 +144,55 @@ function url( value ) {
} }
// Ajax testing helper // Ajax testing helper
var ajaxTest = (function() { function ajaxTest( title, expect, options ) {
var requestOptions;
var resolved = $.Deferred().resolve(); if ( jQuery.isFunction( options ) ) {
options = options();
function getRequests( options ) {
var requests = options.requests || options.request || options || [];
if ( !jQuery.isArray(requests) ) {
requests = [ requests ];
}
return requests;
} }
options = options || [];
return function( title, expect, options ) { requestOptions = options.requests || options.request || options;
if ( jQuery.isFunction(options) ) { if ( !jQuery.isArray( requestOptions ) ) {
options = options(); requestOptions = [ requestOptions ];
}
asyncTest( title, expect, function() {
if ( options.setup ) {
options.setup();
} }
options = options || []; var aborted = false,
asyncTest( title, expect, function() { abort = function( reason ) {
setTimeout(function() { if ( !aborted ) {
if ( options.setup ) { aborted = true;
options.setup(); ok( false, "unexpected " + reason );
} jQuery.each( requests, function( _, request ) {
var ajaxSettings = jQuery.ajaxSetup( {}, {} ); request.abort();
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 ]; requests = jQuery.map( requestOptions, function( options ) {
return handler ? function() { var request = ( options.create || jQuery.ajax )( options );
if ( !aborted && jQuery.isFunction( handler ) ) { if ( options.afterSend ) {
handler.apply( this, arguments ); options.afterSend( request );
} }
return resolved; return request;
} : function() { });
abort( type ); jQuery.when.apply( jQuery, jQuery.map( requests, function( request, index ) {
return resolved; function callIfDefined( deferType, optionType ) {
} var handler = requestOptions[ index ][ deferType ] || !!requestOptions[ index ][ optionType ];
return handler ? function() {
if ( !aborted && jQuery.isFunction( handler ) ) {
handler.apply( this, arguments );
} }
var promise = request.then( callIfDefined( "done", "success" ), callIfDefined( "fail", "error" ) ); } : function() {
promise.abort = request.abort; abort( optionType );
return promise; }
}); }
jQuery.when.apply( jQuery, requests ).done( return request.then( callIfDefined( "done", "success" ), callIfDefined( "fail", "error" ) );
options.teardown, }) ).always(
function() { options.teardown,
jQuery.ajaxSetup( ajaxSettings ); start
setTimeout( start, 0 ); );
} });
); };
}, 0 );
});
};
})();
(function () { (function () {