X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fajax.js;h=c6557f4edc77e276bd8e5caaea0f429edeceb4e6;hb=e454e8305d7c159c3bcffafeb09ea98b342777ad;hp=84d35188db640837275546abf2b2a41ac5b81695;hpb=be4a8c32ac77c3b1cdf1b598943b23bdb3119467;p=jquery.git diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 84d3518..c6557f4 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -136,6 +136,27 @@ test("$.ajax - beforeSend", function() { }); }); +test("$.ajax - beforeSend, cancel request (#2688)", function() { + expect(2); + var request = $.ajax({ + url: url("data/name.html"), + beforeSend: function() { + ok( true, "beforeSend got called, canceling" ); + return false; + }, + success: function() { + ok( false, "request didn't get canceled" ); + }, + complete: function() { + ok( false, "request didn't get canceled" ); + }, + error: function() { + ok( false, "request didn't get canceled" ); + } + }); + ok( request === false, "canceled request must return false instead of XMLHttpRequest instance" ); +}); + var foobar; test("$.ajax - dataType html", function() { @@ -317,6 +338,19 @@ test("load('url selector')", function() { }); }); +test("load(String, Function) with ajaxSetup on dataType json, see #2046", function() { + expect(1); + stop(); + $.ajaxSetup({ dataType: "json" }); + $("#first").ajaxComplete(function (e, xml, s) { + equals( s.dataType, "html", "Verify the load() dataType was html" ); + $("#first").unbind("ajaxComplete"); + $.ajaxSetup({ dataType: "" }); + start(); + }); + $('#first').load("data/test3.html"); +}); + test("load(String, Function) - simple: inject text into DOM", function() { expect(2); stop(); @@ -332,15 +366,15 @@ test("load(String, Function) - check scripts", function() { window.testFoo = undefined; window.foobar = null; var verifyEvaluation = function() { - equals( foobar, "bar", 'Check if script src was evaluated after load' ); - equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM'); - start(); + equals( foobar, "bar", 'Check if script src was evaluated after load' ); + equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM'); + start(); }; $('#first').load(url('data/test.html'), function() { - ok( $('#first').html().match(/^html text/), 'Check content after loading html' ); - equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM'); - equals( testFoo, "foo", 'Check if script was evaluated after load' ); - setTimeout(verifyEvaluation, 600); + ok( $('#first').html().match(/^html text/), 'Check content after loading html' ); + equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM'); + equals( testFoo, "foo", 'Check if script was evaluated after load' ); + setTimeout(verifyEvaluation, 600); }); }); @@ -349,9 +383,9 @@ test("load(String, Function) - check file with only a script tag", function() { stop(); testFoo = undefined; $('#first').load(url('data/test2.html'), function() { - ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM'); - ok( testFoo == "foo", 'Check if script was evaluated after load' ); - start(); + ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM'); + ok( testFoo == "foo", 'Check if script was evaluated after load' ); + start(); }); }); @@ -372,6 +406,7 @@ test("$.get(String, Hash, Function) - parse xml and use text() on nodes", functi test("$.getScript(String, Function) - with callback", function() { expect(2); stop(); + window.foobar = null; $.getScript(url("data/test.js"), function() { equals( foobar, "bar", 'Check if script was evaluated' ); setTimeout(start, 100); @@ -398,6 +433,10 @@ test("$.ajax() - JSONP, Local", function() { success: function(data){ ok( data.data, "JSON results returned (GET, no callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, no callback)" ); + plus(); } }); @@ -407,6 +446,10 @@ test("$.ajax() - JSONP, Local", function() { success: function(data){ ok( data.data, "JSON results returned (GET, url callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, url callback)" ); + plus(); } }); @@ -417,16 +460,24 @@ test("$.ajax() - JSONP, Local", function() { success: function(data){ ok( data.data, "JSON results returned (GET, data callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data callback)" ); + plus(); } }); $.ajax({ url: "data/jsonp.php", dataType: "jsonp", - data: { callback: "?" }, + jsonp: "callback", success: function(data){ ok( data.data, "JSON results returned (GET, data obj callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data obj callback)" ); + plus(); } }); @@ -437,6 +488,10 @@ test("$.ajax() - JSONP, Local", function() { success: function(data){ ok( data.data, "JSON results returned (POST, no callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data obj callback)" ); + plus(); } }); @@ -448,17 +503,25 @@ test("$.ajax() - JSONP, Local", function() { success: function(data){ ok( data.data, "JSON results returned (POST, data callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (POST, data callback)" ); + plus(); } }); $.ajax({ type: "POST", url: "data/jsonp.php", - data: { callback: "?" }, + jsonp: "callback", dataType: "jsonp", success: function(data){ ok( data.data, "JSON results returned (POST, data obj callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (POST, data obj callback)" ); + plus(); } }); }); @@ -479,6 +542,10 @@ test("$.ajax() - JSONP, Remote", function() { success: function(data){ ok( data.data, "JSON results returned (GET, no callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, no callback)" ); + plus(); } }); @@ -488,6 +555,10 @@ test("$.ajax() - JSONP, Remote", function() { success: function(data){ ok( data.data, "JSON results returned (GET, url callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, url callback)" ); + plus(); } }); @@ -498,16 +569,24 @@ test("$.ajax() - JSONP, Remote", function() { success: function(data){ ok( data.data, "JSON results returned (GET, data callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data callback)" ); + plus(); } }); $.ajax({ url: base + "data/jsonp.php", dataType: "jsonp", - data: { callback: "?" }, + jsonp: "callback", success: function(data){ ok( data.data, "JSON results returned (GET, data obj callback)" ); plus(); + }, + error: function(data){ + ok( false, "Ajax error JSON (GET, data obj callback)" ); + plus(); } }); }); @@ -519,6 +598,46 @@ test("$.ajax() - script, Remote", function() { stop(); + window.foobar = null; + $.ajax({ + url: base + "data/test.js", + dataType: "script", + success: function(data){ + ok( foobar, "Script results returned (GET, no callback)" ); + start(); + } + }); +}); + +test("$.ajax() - script, Remote with POST", function() { + expect(3); + + var base = window.location.href.replace(/\?.*$/, ""); + + stop(); + + window.foobar = null; + $.ajax({ + url: base + "data/test.js", + type: "POST", + dataType: "script", + success: function(data, status){ + ok( foobar, "Script results returned (GET, no callback)" ); + equals( status, "success", "Script results returned (GET, no callback)" ); + start(); + } + }); +}); + +test("$.ajax() - script, Remote with scheme-less URL", function() { + expect(2); + + var base = window.location.href.replace(/\?.*$/, ""); + base = base.replace(/^.*?\/\//, "//"); + + stop(); + + window.foobar = null; $.ajax({ url: base + "data/test.js", dataType: "script", @@ -551,15 +670,15 @@ test("$.getJSON(String, Function) - JSON object", function() { }); }); -test("$.getJSON(String, Function) - Remote JSON object with assignment", function() { +test("$.getJSON(String, Function) - JSON object with absolute url to local content", function() { expect(2); var base = window.location.href.replace(/\?.*$/, ""); stop(); - $.getJSON(base + "data/json_assigned_obj.js", function() { - ok( typeof json_assigned_obj == "object", 'Check JSON loaded' ); - equals( json_assigned_obj.test, "worked", 'Check JSON obj.test' ); + $.getJSON(url(base + "data/json.php"), function(json) { + ok( json.data.lang == 'en', 'Check JSON: lang' ); + ok( json.data.length == 25, 'Check JSON: length' ); start(); }); });