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(String, Function) - simple: inject text into DOM", function() {
100 $('#first').load(url("data/name.html"), function() {
101 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
106 test("load(String, Function) - check scripts", function() {
109 window.testFoo = undefined;
110 window.foobar = null;
111 var verifyEvaluation = function() {
112 equals( foobar, "bar", 'Check if script src was evaluated after load' );
113 equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
116 $('#first').load(url('data/test.html'), function() {
117 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
118 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
119 equals( testFoo, "foo", 'Check if script was evaluated after load' );
120 setTimeout(verifyEvaluation, 600);
124 test("load(String, Function) - check file with only a script tag", function() {
128 $('#first').load(url('data/test2.html'), function() {
129 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
130 ok( testFoo == "foo", 'Check if script was evaluated after load' );
135 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
138 $.get(url('data/dashboard.xml'), function(xml) {
140 $('tab', xml).each(function() {
141 content.push($(this).text());
143 equals( content[0], 'blabla', 'Check first tab');
144 equals( content[1], 'blublu', 'Check second tab');
149 test("$.getIfModified(String, Hash, Function)", function() {
152 $.getIfModified(url("data/name.html"), function(msg) {
153 ok( /^ERROR/.test(msg), 'Check ifModified' );
158 test("$.getScript(String, Function) - with callback", function() {
161 $.getScript(url("data/test.js"), function() {
162 equals( foobar, "bar", 'Check if script was evaluated' );
163 setTimeout(start, 100);
167 test("$.getScript(String, Function) - no callback", function() {
170 $.getScript(url("data/test.js"), start);
173 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
177 url: url("data/with_fries.xml"),
179 success: function(resp) {
180 equals( $("properties", resp).length, 1, 'properties in responseXML' );
181 equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
182 equals( $("thing", resp).length, 2, 'things in responseXML' );
188 test("test global handlers - success", function() {
189 expect( isLocal ? 4 : 8 );
192 var counter = { complete: 0, success: 0, error: 0, send: 0 },
193 success = function() { counter.success++ },
194 error = function() { counter.error++ },
195 complete = function() { counter.complete++ },
196 send = function() { counter.send++ };
198 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
200 // start with successful test
201 $.ajax({url: url("data/name.html"), beforeSend: send, success: success, error: error, complete: function() {
202 equals( counter.error, 0, 'Check succesful request, error callback' );
203 equals( counter.success, 2, 'Check succesful request, success callback' );
204 equals( counter.complete, 3, 'Check succesful request, complete callback' );
205 equals( counter.send, 2, 'Check succesful request, send callback' );
208 counter.error = counter.success = counter.complete = counter.send = 0;
211 $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() {
212 equals( counter.error, 2, 'Check failed request, error callback' );
213 equals( counter.success, 0, 'Check failed request, success callback' );
214 equals( counter.complete, 3, 'Check failed request, failed callback' );
215 equals( counter.send, 2, 'Check failed request, send callback' );
223 test("test global handlers - failure", function() {
224 expect( isLocal ? 4 : 8 );
227 var counter = { complete: 0, success: 0, error: 0, send: 0 },
228 success = function() { counter.success++ },
229 error = function() { counter.error++ },
230 complete = function() { counter.complete++ },
231 send = function() { counter.send++ };
235 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
237 $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() {
238 ok( counter.error == 0, 'Check sucesful request without globals' );
239 ok( counter.success == 1, 'Check sucesful request without globals' );
240 ok( counter.complete == 0, 'Check sucesful request without globals' );
241 ok( counter.send == 1, 'Check sucesful request without globals' );
244 counter.error = counter.success = counter.complete = counter.send = 0;
247 $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
249 ok( counter.error == 1, 'Check failed request without globals' );
250 ok( counter.success == 0, 'Check failed request without globals' );
251 ok( counter.complete == 0, 'Check failed request without globals' );
252 ok( counter.send == 1, 'Check failed request without globals' );
260 test("$.ajax - beforeSend", function() {
266 $.ajaxSetup({ timeout: 0 });
269 url: url("data/name.html"),
270 beforeSend: function(xml) {
273 success: function(data) {
274 ok( check, "check beforeSend was executed" );
280 test("$.ajax - dataType html", function() {
287 var verifyEvaluation = function() {
288 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
289 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
295 url: url("data/test.html"),
296 success: function(data) {
298 ok( data.match(/^html text/), 'Check content for datatype html' );
299 setTimeout(verifyEvaluation, 600);
306 test("$.getJSON(String, Hash, Function) - JSON array", function() {
309 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
310 ok( json[0].name == 'John', 'Check JSON: first, name' );
311 ok( json[0].age == 21, 'Check JSON: first, age' );
312 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
313 ok( json[1].age == 25, 'Check JSON: second, age' );
318 test("$.getJSON(String, Function) - JSON object", function() {
321 $.getJSON(url("data/json.php"), function(json) {
322 ok( json.data.lang == 'en', 'Check JSON: lang' );
323 ok( json.data.length == 25, 'Check JSON: length' );
328 test("$.post(String, Hash, Function) - simple with xml", function() {
331 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
332 $('math', xml).each(function() {
333 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
334 ok( $('result', this).text() == '3', 'Check for XML' );
340 test("$.ajaxTimeout(Number) - with global timeout", function() {
347 var pass = function() {
350 ok( true, 'Check local and global callbacks after timeout' );
351 $('#main').unbind("ajaxError");
356 var fail = function(a,b,c) {
357 ok( false, 'Check for timeout failed ' + a + ' ' + b );
361 $('#main').ajaxError(pass);
365 url: url("data/name.php?wait=5"),
374 test("$.ajaxTimeout(Number) with localtimeout", function() {
375 stop(); $.ajaxTimeout(50);
379 url: url("data/name.php?wait=1"),
381 ok( false, 'Check for local timeout failed' );
384 success: function() {
385 ok( true, 'Check for local timeout' );
393 test("$.ajax - simple get", function() {
398 url: url("data/name.php?name=foo"),
399 success: function(msg){
400 ok( msg == 'bar', 'Check for GET' );
406 test("$.ajax - simple post", function() {
411 url: url("data/name.php"),
413 success: function(msg){
414 ok( msg == 'pan', 'Check for POST' );
420 test("ajaxSetup()", function() {
424 url: url("data/name.php?name=foo"),
425 success: function(msg){
426 ok( msg == 'bar', 'Check for GET' );
433 test("custom timeout does not set error message when timeout occurs, see #970", function() {
436 url: "data/name.php?wait=10",
438 error: function(request, status) {
439 ok( status != null, "status shouldn't be null in error handler" );
440 equals( "timeout", status );