X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=test%2Funit%2Fajax.js;h=b44f0773f2f40303ff7545a9a78ebe2c9fe9c65c;hb=dc2e7317a90464e729fd9f29afaa16fa9c01487c;hp=2e8a455ebc48d2291854ecfbf9b51e2a38ee93e9;hpb=0e93b2e24c67237c1172b5d7b1f75f52904d6a1c;p=jquery.git diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 2e8a455..b44f077 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -1,4 +1,4 @@ -module("ajax"); +module("ajax", { teardown: moduleTeardown }); // Safari 3 randomly crashes when running these tests, // but only in the full suite - you can run just the Ajax @@ -599,6 +599,47 @@ test("jQuery.ajax context modification", function() { equals( obj.test, "foo", "Make sure the original object is maintained." ); }); +test("jQuery.ajax context modification through ajaxSetup", function() { + expect(4); + + stop(); + + var obj = {}; + + jQuery.ajaxSetup({ + context: obj + }); + + strictEqual( jQuery.ajaxSettings.context, obj, "Make sure the context is properly set in ajaxSettings." ); + + jQuery.ajax({ + url: url("data/name.html"), + complete: function() { + strictEqual( this, obj, "Make sure the original object is maintained." ); + jQuery.ajax({ + url: url("data/name.html"), + context: {}, + complete: function() { + ok( this !== obj, "Make sure overidding context is possible." ); + jQuery.ajaxSetup({ + context: false + }); + jQuery.ajax({ + url: url("data/name.html"), + beforeSend: function(){ + this.test = "foo2"; + }, + complete: function() { + ok( this !== obj, "Make sure unsetting context is possible." ); + start(); + } + }); + } + }); + } + }); +}); + test("jQuery.ajax() - disabled globals", function() { expect( 3 ); stop(); @@ -1117,10 +1158,10 @@ test("jQuery.getScript(String, Function) - no callback", function() { jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) { test("jQuery.ajax() - JSONP, " + label, function() { - expect(16); + expect(17); var count = 0; - function plus(){ if ( ++count == 16 ) start(); } + function plus(){ if ( ++count == 17 ) start(); } stop(); @@ -1343,15 +1384,15 @@ jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) jsonpCallback: "XXX", crossDomain: crossDomain, beforeSend: function() { - console.log( this.url ); + ok( /^data\/jsonp.php\?callback=XXX&_=\d+$/.test( this.url ) , + "The URL wasn't messed with (GET, custom callback name with no url manipulation)" ); + plus(); }, success: function(data){ - console.log(data); ok( data.data, "JSON results returned (GET, custom callback name with no url manipulation)" ); plus(); }, error: function(data){ - console.log(data); ok( false, "Ajax error JSON (GET, custom callback name with no url manipulation)" ); plus(); } @@ -1857,10 +1898,6 @@ test("jQuery ajax - atom+xml", function() { }); -test("jQuery.ajax - active counter", function() { - ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active ); -}); - test( "jQuery.ajax - Location object as url (#7531)", 1, function () { var success = false; try { @@ -1874,9 +1911,9 @@ test( "jQuery.ajax - Location object as url (#7531)", 1, function () { test( "jQuery.ajax - statusCode" , function() { - var count = 10; + var count = 12; - expect( 16 ); + expect( 20 ); stop(); function countComplete() { @@ -1938,8 +1975,82 @@ test( "jQuery.ajax - statusCode" , function() { } }).statusCode( createStatusCodes( "all (immediately with method)" , isSuccess ) ); + var testString = ""; + + jQuery.ajax( url( uri ), { + success: function( a , b , jXHR ) { + ok( isSuccess , "success" ); + var statusCode = {}; + statusCode[ jXHR.status ] = function() { + testString += "B"; + }; + jXHR.statusCode( statusCode ); + testString += "A"; + }, + error: function( jXHR ) { + ok( ! isSuccess , "error" ); + var statusCode = {}; + statusCode[ jXHR.status ] = function() { + testString += "B"; + }; + jXHR.statusCode( statusCode ); + testString += "A"; + }, + complete: function() { + strictEqual( testString , "AB" , "Test statusCode callbacks are ordered like " + + ( isSuccess ? "success" : "error" ) + " callbacks" ); + countComplete(); + } + } ); + }); +}); + +test("jQuery.ajax - transitive conversions", function() { + + expect( 8 ); + + stop(); + jQuery.when( + + jQuery.ajax( url("data/json.php") , { + converters: { + "json myjson": function( data ) { + ok( true , "converter called" ); + return data; + } + }, + dataType: "myjson", + success: function() { + ok( true , "Transitive conversion worked" ); + strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text" ); + strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType" ); + } + }), + + jQuery.ajax( url("data/json.php") , { + converters: { + "json myjson": function( data ) { + ok( true , "converter called (*)" ); + return data; + } + }, + contents: false, /* headers are wrong so we ignore them */ + dataType: "* myjson", + success: function() { + ok( true , "Transitive conversion worked (*)" ); + strictEqual( this.dataTypes[0] , "text" , "response was retrieved as text (*)" ); + strictEqual( this.dataTypes[1] , "myjson" , "request expected myjson dataType (*)" ); + } + }) + + ).then( start , start ); + +}); + +test("jQuery.ajax - active counter", function() { + ok( jQuery.active == 0, "ajax active counter should be zero: " + jQuery.active ); }); }