Skip to content

Commit

Permalink
More improvements per @jaubourg
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 committed Dec 3, 2012
1 parent ad690f8 commit 4ada325
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
50 changes: 28 additions & 22 deletions test/data/testinit.js
Expand Up @@ -160,34 +160,30 @@ function ajaxTest( title, expect, options ) {
}

var completed = false,
remaining = requestOptions.length,
complete = function() {
completed = true;
delete ajaxTest.abort;
},
abort = ajaxTest.abort = function( reason ) {
if ( !completed ) {
complete();
ok( false, "unexpected " + reason );
jQuery.each( requests, function( _, request ) {
if ( request && request.abort ) {
request.abort();
}
});
if ( !completed && --remaining === 0 ) {
completed = true;
delete ajaxTest.abort;
if ( options.teardown ) {
options.teardown();
}
start();
}
},
requests = jQuery.map( requestOptions, function( options, index ) {
requests = jQuery.map( requestOptions, function( options ) {
var request = ( options.create || jQuery.ajax )( options ),
callIfDefined = function( deferType, optionType ) {
var handler = options[ deferType ] || !!options[ optionType ];
return handler ?
function() {
if ( !completed && jQuery.isFunction( handler ) ) {
return function( _, status ) {
if ( !completed ) {
if ( !handler ) {
ok( false, "unexpected " + status );
} else if ( jQuery.isFunction( handler ) ) {
handler.apply( this, arguments );
}
} :
function() {
abort( optionType );
};
}
};
};

if ( options.afterSend ) {
Expand All @@ -196,10 +192,20 @@ function ajaxTest( title, expect, options ) {

return request
.done( callIfDefined( "done", "success" ) )
.fail( callIfDefined( "fail", "error" ) );
.fail( callIfDefined( "fail", "error" ) )
.always( complete );
});

jQuery.when.apply( jQuery, requests ).always( complete, options.teardown, start);
ajaxTest.abort = function( reason ) {
if ( !completed ) {
completed = true;
delete ajaxTest.abort;
ok( false, "aborted " + reason );
jQuery.each( requests, function( i, request ) {
request.abort();
});
}
};
});
};

Expand Down
2 changes: 1 addition & 1 deletion test/data/testrunner.js
Expand Up @@ -310,7 +310,7 @@ var Globals = (function() {
if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
equal( jQuery.active, 0, "No AJAX requests are still active" );
if ( ajaxTest.abort ) {
ajaxTest.abort("active request");
ajaxTest.abort("active requests");
}
oldActive = jQuery.active;
}
Expand Down

0 comments on commit 4ada325

Please sign in to comment.