Make sure regular settings object is set as context for all Ajax requests, if none...
[jquery.git] / test / unit / ajax.js
index 3393780..4f90126 100644 (file)
@@ -95,7 +95,7 @@ test("jQuery.ajax() - abort", function() {
 });
 
 test("Ajax events with context", function() {
-       expect(6);
+       expect(14);
        
        stop();
        var context = document.createElement("div");
@@ -104,8 +104,16 @@ test("Ajax events with context", function() {
                equals( this, context, e.type );
        }
 
-       function callback(){
-               equals( this, context, "context is preserved on callback" );
+       function callback(msg){
+               return function(){
+                       equals( this, context, "context is preserved on callback " + msg );
+               };
+       }
+
+       function nocallback(msg){
+               return function(){
+                       equals( typeof this.url, "string", "context is settings on callback " + msg );
+               };
        }
        
        jQuery('#foo').add(context)
@@ -116,20 +124,36 @@ test("Ajax events with context", function() {
 
        jQuery.ajax({
                url: url("data/name.html"),
-               beforeSend: callback,
-               success: callback,
-               error: callback,
+               beforeSend: callback("beforeSend"),
+               success: callback("success"),
+               error: callback("error"),
                complete:function(){
-                       callback.call(this);
-                       setTimeout(proceed, 300);
+                       callback("complete").call(this);
+
+                       jQuery.ajax({
+                               url: url("data/404.html"),
+                               context: context,
+                               beforeSend: callback("beforeSend"),
+                               error: callback("error"),
+                               complete: function(){
+                                       callback("complete").call(this);
+
+                                       jQuery('#foo').add(context).unbind();
+
+                                       jQuery.ajax({
+                                               url: url("data/404.html"),
+                                               beforeSend: nocallback("beforeSend"),
+                                               error: nocallback("error"),
+                                               complete: function(){
+                                                       nocallback("complete").call(this);
+                                                       start();
+                                               }
+                                       });
+                               }
+                       });
                },
                context:context
        });
-       
-       function proceed(){
-               jQuery('#foo').add(context).unbind();
-               start();
-       }
 });
 
 test("jQuery.ajax() - disabled globals", function() {
@@ -282,7 +306,7 @@ test("serialize()", function() {
 });
 
 test("jQuery.param()", function() {
-       expect(17);
+       expect(18);
        
   equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
   
@@ -336,18 +360,19 @@ test("jQuery.param()", function() {
        params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
        equals( decodeURIComponent( jQuery.param(params,false) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure, forced not traditional" );
        
-
+       params = { param1: null };
+       equals( jQuery.param(params,false), "param1=null", "Make sure that null params aren't traversed." );
 });
 
 test("synchronous request", function() {
        expect(1);
-       ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
+       ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
 });
 
 test("synchronous request with callbacks", function() {
        expect(2);
        var result;
-       jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
+       jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
        ok( /^{ "data"/.test( result ), "check returned text" );
 });
 
@@ -821,6 +846,25 @@ test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
        });
 });
 
+test("jQuery.ajax() - malformed JSON", function() {
+       expect(1);
+
+       stop();
+
+       jQuery.ajax({
+               url: "data/badjson.js",
+               dataType: "json",
+               success: function(){
+                       ok( false, "Success." );
+                       start();
+               },
+               error: function(xhr, msg) {
+                       equals( "parsererror", msg, "A parse error occurred." );
+                       start();
+               }
+       });
+});
+
 test("jQuery.ajax() - script by content-type", function() {
        expect(1);