Total revamping of the ajax unit tests

This commit is contained in:
jaubourg 2012-11-26 03:31:19 +01:00
parent 53a06660d8
commit d31cef38f2
4 changed files with 1474 additions and 2265 deletions

View File

@ -38,6 +38,7 @@
"stop": true, "stop": true,
"expect": true, "expect": true,
"raises": true, "raises": true,
"ajaxTest": true,
"testIframe": true, "testIframe": true,
"testIframeWithCallback": true, "testIframeWithCallback": true,
"createDashboardXML": true, "createDashboardXML": true,
@ -50,6 +51,7 @@
"q": true, "q": true,
"amdDefined": true, "amdDefined": true,
"fireNative": true, "fireNative": true,
"Globals": true,
"hasPHP": true, "hasPHP": true,
"isLocal": true, "isLocal": true,
"originaljQuery": true, "originaljQuery": true,

View File

@ -143,6 +143,77 @@ function url( value ) {
return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random() * 100000, 10); 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 () { (function () {
this.testIframe = function( fileName, name, fn ) { this.testIframe = function( fileName, name, fn ) {

View File

@ -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() {
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 * QUnit hooks
*/ */
@ -292,11 +317,14 @@ function testSubproject( label, url, risTests ) {
// Reset internal jQuery state // Reset internal jQuery state
jQuery.event.global = {}; jQuery.event.global = {};
if ( ajaxSettings ) { if ( ajaxSettings ) {
jQuery.ajaxSettings = jQuery.extend( {}, ajaxSettings ); jQuery.ajaxSettings = jQuery.extend( true, {}, ajaxSettings );
} else { } else {
delete jQuery.ajaxSettings; delete jQuery.ajaxSettings;
} }
// Cleanup globals
Globals.cleanup();
// Let QUnit reset the fixture // Let QUnit reset the fixture
reset.apply( this, arguments ); reset.apply( this, arguments );
}; };

File diff suppressed because it is too large Load Diff