3 // Safari 3 randomly crashes when running these tests,
4 // but only in the full suite - you can run just the Ajax
5 // tests and they'll pass
6 if ( !jQuery.browser.safari ) {
8 test("serialize()", function() {
10 // ignore button, IE takes text content as value, not relevant for this test
11 equals( $(':input').not('button').serialize(),
12 '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=',
13 'Check form serialization as query string');
16 test("$.param()", function() {
18 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
19 equals( $.param(params), "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
21 params = {someName: [1, 2, 3], regularThing: "blah" };
22 equals( $.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
24 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
25 equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All%20your%20base%20are%20belong%20to%20us", "more array" );
27 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
28 equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
31 test("synchronous request", function() {
33 ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
36 test("synchronous request with callbacks", function() {
39 $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
40 ok( /^{ "data"/.test( result ), "check returned text" );
43 test("pass-through request object", function() {
47 var target = "data/name.html";
49 var success = function() {
55 /* Test disabled, too many simultaneous requests
56 ok( $.get(url(target), success), "get" );
57 ok( $.getIfModified(url(target), success), "getIfModified" );
58 ok( $.post(url(target), success), "post" );
59 ok( $.getScript(url("data/test.js"), success), "script" );
60 ok( $.getJSON(url("data/json_obj.js"), success), "json" );
62 ok( $.ajax({url: url(target), success: success}), "generic" );
65 test("global ajaxSettings", function() {
68 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
69 var orig = { url: "data/with_fries.xml", data: null };
72 $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
74 t = jQuery.extend({}, orig);
76 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
78 t = jQuery.extend({}, orig);
81 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
83 t = jQuery.extend({}, orig);
84 t.data = { zoo: 'a', ping: 'b' };
86 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' }" );
88 jQuery.ajaxSettings = tmp;
91 test("load(String)", function() {
93 stop(true); // check if load can be called with only url
94 $('#first').load("data/name.html", start);
97 test("load('url selector')", function() {
99 stop(true); // check if load can be called with only url
100 $('#first').load("data/test3.html div.user", function(){
101 equals( $(this).children("div").length, 2, "Verify that specific elements were injected" );
106 test("load(String, Function) - simple: inject text into DOM", function() {
109 $('#first').load(url("data/name.html"), function() {
110 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
115 test("load(String, Function) - check scripts", function() {
118 window.testFoo = undefined;
119 window.foobar = null;
120 var verifyEvaluation = function() {
121 equals( foobar, "bar", 'Check if script src was evaluated after load' );
122 equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
125 $('#first').load(url('data/test.html'), function() {
126 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
127 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
128 equals( testFoo, "foo", 'Check if script was evaluated after load' );
129 setTimeout(verifyEvaluation, 600);
133 test("load(String, Function) - check file with only a script tag", function() {
137 $('#first').load(url('data/test2.html'), function() {
138 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
139 ok( testFoo == "foo", 'Check if script was evaluated after load' );
144 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
147 $.get(url('data/dashboard.xml'), function(xml) {
149 $('tab', xml).each(function() {
150 content.push($(this).text());
152 equals( content[0], 'blabla', 'Check first tab');
153 equals( content[1], 'blublu', 'Check second tab');
158 test("$.getIfModified(String, Hash, Function)", function() {
161 $.getIfModified(url("data/name.html"), function(msg) {
162 ok( /^ERROR/.test(msg), 'Check ifModified' );
167 test("$.getScript(String, Function) - with callback", function() {
170 $.getScript(url("data/test.js"), function() {
171 equals( foobar, "bar", 'Check if script was evaluated' );
172 setTimeout(start, 100);
176 test("$.getScript(String, Function) - no callback", function() {
179 $.getScript(url("data/test.js"), start);
182 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
186 url: url("data/with_fries.xml"),
188 success: function(resp) {
189 equals( $("properties", resp).length, 1, 'properties in responseXML' );
190 equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
191 equals( $("thing", resp).length, 2, 'things in responseXML' );
197 test("test global handlers - success", function() {
198 expect( isLocal ? 4 : 8 );
201 var counter = { complete: 0, success: 0, error: 0, send: 0 },
202 success = function() { counter.success++ },
203 error = function() { counter.error++ },
204 complete = function() { counter.complete++ },
205 send = function() { counter.send++ };
207 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
209 // start with successful test
210 $.ajax({url: url("data/name.html"), beforeSend: send, success: success, error: error, complete: function() {
211 equals( counter.error, 0, 'Check succesful request, error callback' );
212 equals( counter.success, 2, 'Check succesful request, success callback' );
213 equals( counter.complete, 3, 'Check succesful request, complete callback' );
214 equals( counter.send, 2, 'Check succesful request, send callback' );
217 counter.error = counter.success = counter.complete = counter.send = 0;
220 $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() {
221 equals( counter.error, 2, 'Check failed request, error callback' );
222 equals( counter.success, 0, 'Check failed request, success callback' );
223 equals( counter.complete, 3, 'Check failed request, failed callback' );
224 equals( counter.send, 2, 'Check failed request, send callback' );
232 test("test global handlers - failure", function() {
233 expect( isLocal ? 4 : 8 );
236 var counter = { complete: 0, success: 0, error: 0, send: 0 },
237 success = function() { counter.success++ },
238 error = function() { counter.error++ },
239 complete = function() { counter.complete++ },
240 send = function() { counter.send++ };
244 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
246 $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() {
247 ok( counter.error == 0, 'Check sucesful request without globals' );
248 ok( counter.success == 1, 'Check sucesful request without globals' );
249 ok( counter.complete == 0, 'Check sucesful request without globals' );
250 ok( counter.send == 1, 'Check sucesful request without globals' );
253 counter.error = counter.success = counter.complete = counter.send = 0;
256 $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
258 ok( counter.error == 1, 'Check failed request without globals' );
259 ok( counter.success == 0, 'Check failed request without globals' );
260 ok( counter.complete == 0, 'Check failed request without globals' );
261 ok( counter.send == 1, 'Check failed request without globals' );
269 test("$.ajax - beforeSend", function() {
275 $.ajaxSetup({ timeout: 0 });
278 url: url("data/name.html"),
279 beforeSend: function(xml) {
282 success: function(data) {
283 ok( check, "check beforeSend was executed" );
289 test("$.ajax - dataType html", function() {
296 var verifyEvaluation = function() {
297 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
298 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
304 url: url("data/test.html"),
305 success: function(data) {
307 ok( data.match(/^html text/), 'Check content for datatype html' );
308 setTimeout(verifyEvaluation, 600);
315 test("$.getJSON(String, Hash, Function) - JSON array", function() {
318 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
319 ok( json[0].name == 'John', 'Check JSON: first, name' );
320 ok( json[0].age == 21, 'Check JSON: first, age' );
321 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
322 ok( json[1].age == 25, 'Check JSON: second, age' );
327 test("$.getJSON(String, Function) - JSON object", function() {
330 $.getJSON(url("data/json.php"), function(json) {
331 ok( json.data.lang == 'en', 'Check JSON: lang' );
332 ok( json.data.length == 25, 'Check JSON: length' );
337 test("$.post(String, Hash, Function) - simple with xml", function() {
340 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
341 $('math', xml).each(function() {
342 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
343 ok( $('result', this).text() == '3', 'Check for XML' );
349 test("$.ajaxTimeout(Number) - with global timeout", function() {
356 var pass = function() {
359 ok( true, 'Check local and global callbacks after timeout' );
360 $('#main').unbind("ajaxError");
365 var fail = function(a,b,c) {
366 ok( false, 'Check for timeout failed ' + a + ' ' + b );
370 $('#main').ajaxError(pass);
374 url: url("data/name.php?wait=5"),
383 test("$.ajaxTimeout(Number) with localtimeout", function() {
384 stop(); $.ajaxTimeout(50);
388 url: url("data/name.php?wait=1"),
390 ok( false, 'Check for local timeout failed' );
393 success: function() {
394 ok( true, 'Check for local timeout' );
402 test("$.ajax - simple get", function() {
407 url: url("data/name.php?name=foo"),
408 success: function(msg){
409 ok( msg == 'bar', 'Check for GET' );
415 test("$.ajax - simple post", function() {
420 url: url("data/name.php"),
422 success: function(msg){
423 ok( msg == 'pan', 'Check for POST' );
429 test("ajaxSetup()", function() {
433 url: url("data/name.php?name=foo"),
434 success: function(msg){
435 ok( msg == 'bar', 'Check for GET' );
442 test("custom timeout does not set error message when timeout occurs, see #970", function() {
445 url: "data/name.php?wait=10",
447 error: function(request, status) {
448 ok( status != null, "status shouldn't be null in error handler" );
449 equals( "timeout", status );