X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fajax%2FajaxTest.js;h=f158bae2120993a450108bed487a5a84a36a6627;hb=f96bf1041553775a94c1034c97e253e350217173;hp=4d4bc0791d2e1137573d87f1fa96ecfc2c618337;hpb=f948f026809eab3d238cf6dc369cb5b86b61ce8c;p=jquery.git diff --git a/src/ajax/ajaxTest.js b/src/ajax/ajaxTest.js index 4d4bc07..f158bae 100644 --- a/src/ajax/ajaxTest.js +++ b/src/ajax/ajaxTest.js @@ -3,14 +3,191 @@ module("ajax"); // Safari 3 randomly crashes when running these tests, // but only in the full suite - you can run just the Ajax // tests and they'll pass -if ( !jQuery.browser.safari ) { +//if ( !jQuery.browser.safari ) { -test("serialize()", function() { +test("$.ajax() - success callbacks", function() { + expect( 8 ); + + $.ajaxSetup({ timeout: 0 }); + + stop(); + + setTimeout(function(){ + $('#foo').ajaxStart(function(){ + ok( true, "ajaxStart" ); + }).ajaxStop(function(){ + ok( true, "ajaxStop" ); + start(); + }).ajaxSend(function(){ + ok( true, "ajaxSend" ); + }).ajaxComplete(function(){ + ok( true, "ajaxComplete" ); + }).ajaxError(function(){ + ok( false, "ajaxError" ); + }).ajaxSuccess(function(){ + ok( true, "ajaxSuccess" ); + }); + + $.ajax({ + url: url("data/name.html"), + beforeSend: function(){ ok(true, "beforeSend"); }, + success: function(){ ok(true, "success"); }, + error: function(){ ok(false, "error"); }, + complete: function(){ ok(true, "complete"); } + }); + }, 13); +}); + +if ( !isLocal ) { + test("$.ajax() - error callbacks", function() { + expect( 8 ); + stop(); + + $('#foo').ajaxStart(function(){ + ok( true, "ajaxStart" ); + }).ajaxStop(function(){ + ok( true, "ajaxStop" ); + start(); + }).ajaxSend(function(){ + ok( true, "ajaxSend" ); + }).ajaxComplete(function(){ + ok( true, "ajaxComplete" ); + }).ajaxError(function(){ + ok( true, "ajaxError" ); + }).ajaxSuccess(function(){ + ok( false, "ajaxSuccess" ); + }); + + $.ajaxSetup({ timeout: 500 }); + + $.ajax({ + url: url("data/name.php?wait=5"), + beforeSend: function(){ ok(true, "beforeSend"); }, + success: function(){ ok(false, "success"); }, + error: function(){ ok(true, "error"); }, + complete: function(){ ok(true, "complete"); } + }); + }); +} + +test("$.ajax() - disabled globals", function() { + expect( 3 ); + stop(); + + $('#foo').ajaxStart(function(){ + ok( false, "ajaxStart" ); + }).ajaxStop(function(){ + ok( false, "ajaxStop" ); + }).ajaxSend(function(){ + ok( false, "ajaxSend" ); + }).ajaxComplete(function(){ + ok( false, "ajaxComplete" ); + }).ajaxError(function(){ + ok( false, "ajaxError" ); + }).ajaxSuccess(function(){ + ok( false, "ajaxSuccess" ); + }); + + $.ajax({ + global: false, + url: url("data/name.html"), + beforeSend: function(){ ok(true, "beforeSend"); }, + success: function(){ ok(true, "success"); }, + error: function(){ ok(false, "error"); }, + complete: function(){ + ok(true, "complete"); + setTimeout(function(){ start(); }, 13); + } + }); +}); + +test("$.ajax - xml: non-namespace elements inside namespaced elements", function() { + expect(3); + stop(); + $.ajax({ + url: url("data/with_fries.xml"), + dataType: "xml", + success: function(resp) { + equals( $("properties", resp).length, 1, 'properties in responseXML' ); + equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' ); + equals( $("thing", resp).length, 2, 'things in responseXML' ); + start(); + } + }); +}); + +test("$.ajax - beforeSend", function() { expect(1); - // ignore button, IE takes text content as value, not relevant for this test - equals( $(':input').not('button').serialize(), - 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo%5Bbar%5D=&name=name&=foobar&select1=&select2=3&select3=1&test=&id=', + stop(); + + var check = false; + + $.ajaxSetup({ timeout: 0 }); + + $.ajax({ + url: url("data/name.html"), + beforeSend: function(xml) { + check = true; + }, + success: function(data) { + ok( check, "check beforeSend was executed" ); + start(); + } + }); +}); + +var foobar; + +test("$.ajax - dataType html", function() { + expect(5); + stop(); + + foobar = null; + testFoo = undefined; + + var verifyEvaluation = function() { + ok( testFoo == "foo", 'Check if script was evaluated for datatype html' ); + ok( foobar == "bar", 'Check if script src was evaluated for datatype html' ); + start(); + }; + + $.ajax({ + dataType: "html", + url: url("data/test.html"), + success: function(data) { + $("#ap").html(data); + ok( data.match(/^html text/), 'Check content for datatype html' ); + setTimeout(verifyEvaluation, 600); + } + }); +}); + +test("serialize()", function() { + expect(6); + + equals( $('#form').serialize(), + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1", + 'Check form serialization as query string'); + + equals( $('#form :input').serialize(), + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1", + 'Check input serialization as query string'); + + equals( $('#testForm').serialize(), + 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', 'Check form serialization as query string'); + + equals( $('#testForm :input').serialize(), + 'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=', + 'Check input serialization as query string'); + + equals( $('#form, #testForm').serialize(), + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=", + 'Multiple form serialization as query string'); + + equals( $('#form, #testForm :input').serialize(), + "action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=", + 'Mixed form/input serialization as query string'); }); test("$.param()", function() { @@ -54,7 +231,6 @@ test("pass-through request object", function() { /* Test disabled, too many simultaneous requests ok( $.get(url(target), success), "get" ); - ok( $.getIfModified(url(target), success), "getIfModified" ); ok( $.post(url(target), success), "post" ); ok( $.getScript(url("data/test.js"), success), "script" ); ok( $.getJSON(url("data/json_obj.js"), success), "json" ); @@ -66,23 +242,23 @@ test("global ajaxSettings", function() { expect(3); var tmp = jQuery.extend({}, jQuery.ajaxSettings); - var orig = { url: "data/with_fries.xml", data: null }; + var orig = { url: "data/with_fries.xml", data: null }; var t; $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} }); - t = jQuery.extend({}, orig); - $.ajax(t); + t = jQuery.extend({}, orig); + $.ajax(t); ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" ); - t = jQuery.extend({}, orig); + t = jQuery.extend({}, orig); t.data = {}; - $.ajax(t); + $.ajax(t); ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" ); - t = jQuery.extend({}, orig); + t = jQuery.extend({}, orig); t.data = { zoo: 'a', ping: 'b' }; - $.ajax(t); + $.ajax(t); ok( t.url.indexOf('ping') > -1 && t.url.indexOf('zoo') > -1 && t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending { zoo: 'a', ping: 'b' }" ); jQuery.ajaxSettings = tmp; @@ -94,6 +270,15 @@ test("load(String)", function() { $('#first').load("data/name.html", start); }); +test("load('url selector')", function() { + expect(1); + stop(true); // check if load can be called with only url + $('#first').load("data/test3.html div.user", function(){ + equals( $(this).children("div").length, 2, "Verify that specific elements were injected" ); + start(); + }); +}); + test("load(String, Function) - simple: inject text into DOM", function() { expect(2); stop(); @@ -146,15 +331,6 @@ test("$.get(String, Hash, Function) - parse xml and use text() on nodes", functi }); }); -test("$.getIfModified(String, Hash, Function)", function() { - expect(1); - stop(); - $.getIfModified(url("data/name.html"), function(msg) { - ok( /^ERROR/.test(msg), 'Check ifModified' ); - start(); - }); -}); - test("$.getScript(String, Function) - with callback", function() { expect(2); stop(); @@ -170,139 +346,153 @@ test("$.getScript(String, Function) - no callback", function() { $.getScript(url("data/test.js"), start); }); -test("$.ajax - xml: non-namespace elements inside namespaced elements", function() { - expect(3); +if ( !isLocal ) { + +test("$.ajax() - JSONP, Local", function() { + expect(7); + + var count = 0; + function plus(){ if ( ++count == 7 ) start(); } + stop(); + $.ajax({ - url: url("data/with_fries.xml"), - dataType: "xml", - success: function(resp) { - equals( $("properties", resp).length, 1, 'properties in responseXML' ); - equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' ); - equals( $("thing", resp).length, 2, 'things in responseXML' ); - start(); - } + url: "data/jsonp.php", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (GET, no callback)" ); + plus(); + } }); -}); -test("test global handlers - success", function() { - expect( isLocal ? 4 : 8 ); - stop(); - - var counter = { complete: 0, success: 0, error: 0, send: 0 }, - success = function() { counter.success++ }, - error = function() { counter.error++ }, - complete = function() { counter.complete++ }, - send = function() { counter.send++ }; + $.ajax({ + url: "data/jsonp.php?callback=?", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (GET, url callback)" ); + plus(); + } + }); - $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success); - - // start with successful test - $.ajax({url: url("data/name.html"), beforeSend: send, success: success, error: error, complete: function() { - equals( counter.error, 0, 'Check succesful request, error callback' ); - equals( counter.success, 2, 'Check succesful request, success callback' ); - equals( counter.complete, 3, 'Check succesful request, complete callback' ); - equals( counter.send, 2, 'Check succesful request, send callback' ); - - if ( !isLocal ) { - counter.error = counter.success = counter.complete = counter.send = 0; - $.ajaxTimeout(500); - - $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() { - equals( counter.error, 2, 'Check failed request, error callback' ); - equals( counter.success, 0, 'Check failed request, success callback' ); - equals( counter.complete, 3, 'Check failed request, failed callback' ); - equals( counter.send, 2, 'Check failed request, send callback' ); - start(); - }}); - } else - start(); - }}); -}); + $.ajax({ + url: "data/jsonp.php", + dataType: "jsonp", + data: "callback=?", + success: function(data){ + ok( data.data, "JSON results returned (GET, data callback)" ); + plus(); + } + }); -test("test global handlers - failure", function() { - expect( isLocal ? 4 : 8 ); - stop(); - - var counter = { complete: 0, success: 0, error: 0, send: 0 }, - success = function() { counter.success++ }, - error = function() { counter.error++ }, - complete = function() { counter.complete++ }, - send = function() { counter.send++ }; - - $.ajaxTimeout(0); - - $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success); - - $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() { - ok( counter.error == 0, 'Check sucesful request without globals' ); - ok( counter.success == 1, 'Check sucesful request without globals' ); - ok( counter.complete == 0, 'Check sucesful request without globals' ); - ok( counter.send == 1, 'Check sucesful request without globals' ); - - if ( !isLocal ) { - counter.error = counter.success = counter.complete = counter.send = 0; - $.ajaxTimeout(500); - - $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() { - var x = counter; - ok( counter.error == 1, 'Check failed request without globals' ); - ok( counter.success == 0, 'Check failed request without globals' ); - ok( counter.complete == 0, 'Check failed request without globals' ); - ok( counter.send == 1, 'Check failed request without globals' ); - start(); - }}); - } else - start(); - }}); + $.ajax({ + url: "data/jsonp.php", + dataType: "jsonp", + data: { callback: "?" }, + success: function(data){ + ok( data.data, "JSON results returned (GET, data obj callback)" ); + plus(); + } + }); + + $.ajax({ + type: "POST", + url: "data/jsonp.php", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (POST, no callback)" ); + plus(); + } + }); + + $.ajax({ + type: "POST", + url: "data/jsonp.php", + data: "callback=?", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (POST, data callback)" ); + plus(); + } + }); + + $.ajax({ + type: "POST", + url: "data/jsonp.php", + data: { callback: "?" }, + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (POST, data obj callback)" ); + plus(); + } + }); }); -test("$.ajax - beforeSend", function() { - expect(1); +test("$.ajax() - JSONP, Remote", function() { + expect(4); + + var count = 0; + function plus(){ if ( ++count == 4 ) start(); } + + var base = window.location.href.replace(/\?.*$/, ""); + stop(); - - var check = false; - - $.ajaxSetup({ timeout: 0 }); - + $.ajax({ - url: url("data/name.html"), - beforeSend: function(xml) { - check = true; - }, - success: function(data) { - ok( check, "check beforeSend was executed" ); - start(); + url: base + "data/jsonp.php", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (GET, no callback)" ); + plus(); + } + }); + + $.ajax({ + url: base + "data/jsonp.php?callback=?", + dataType: "jsonp", + success: function(data){ + ok( data.data, "JSON results returned (GET, url callback)" ); + plus(); + } + }); + + $.ajax({ + url: base + "data/jsonp.php", + dataType: "jsonp", + data: "callback=?", + success: function(data){ + ok( data.data, "JSON results returned (GET, data callback)" ); + plus(); + } + }); + + $.ajax({ + url: base + "data/jsonp.php", + dataType: "jsonp", + data: { callback: "?" }, + success: function(data){ + ok( data.data, "JSON results returned (GET, data obj callback)" ); + plus(); } }); }); -test("$.ajax - dataType html", function() { - expect(5); +test("$.ajax() - script, Remote", function() { + expect(2); + + var base = window.location.href.replace(/\?.*$/, ""); + stop(); - - foobar = null; - testFoo = undefined; - - var verifyEvaluation = function() { - ok( testFoo == "foo", 'Check if script was evaluated for datatype html' ); - ok( foobar == "bar", 'Check if script src was evaluated for datatype html' ); - start(); - }; - + $.ajax({ - dataType: "html", - url: url("data/test.html"), - success: function(data) { - $("#ap").html(data); - ok( data.match(/^html text/), 'Check content for datatype html' ); - setTimeout(verifyEvaluation, 600); - } + url: base + "data/test.js", + dataType: "script", + success: function(data){ + ok( foobar, "Script results returned (GET, no callback)" ); + start(); + } }); }); -if ( !isLocal ) { - test("$.getJSON(String, Hash, Function) - JSON array", function() { expect(4); stop(); @@ -337,12 +527,12 @@ test("$.post(String, Hash, Function) - simple with xml", function() { }); }); -test("$.ajaxTimeout(Number) - with global timeout", function() { +test("$.ajaxSetup({timeout: Number}) - with global timeout", function() { stop(); var passed = 0; - $.ajaxTimeout(1000); + $.ajaxSetup({timeout: 1000}); var pass = function() { passed++; @@ -368,11 +558,13 @@ test("$.ajaxTimeout(Number) - with global timeout", function() { }); // reset timeout - $.ajaxTimeout(0); + $.ajaxSetup({timeout: 0}); }); -test("$.ajaxTimeout(Number) with localtimeout", function() { - stop(); $.ajaxTimeout(50); +test("$.ajaxSetup({timeout: Number}) with localtimeout", function() { + stop(); + $.ajaxSetup({timeout: 50}); + $.ajax({ type: "GET", timeout: 5000, @@ -386,8 +578,9 @@ test("$.ajaxTimeout(Number) with localtimeout", function() { start(); } }); + // reset timeout - $.ajaxTimeout(0); + $.ajaxSetup({timeout: 0}); }); test("$.ajax - simple get", function() { @@ -445,4 +638,4 @@ test("custom timeout does not set error message when timeout occurs, see #970", } -} +//}