3 // Safari 3 crashes when running these tests, sigh
4 if ( !jQuery.browser.safari ) {
6 test("serialize()", function() {
8 // ignore button, IE takes text content as value, not relevant for this test
9 equals( $(':input').not('button').serialize(),
10 '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=',
11 'Check form serialization as query string');
14 test("$.param()", function() {
16 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
17 equals( $.param(params), "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
19 params = {someName: [1, 2, 3], regularThing: "blah" };
20 equals( $.param(params), "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
22 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
23 equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All%20your%20base%20are%20belong%20to%20us", "more array" );
25 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
26 equals( $.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
29 test("synchronous request", function() {
31 ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
34 test("synchronous request with callbacks", function() {
37 $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
38 ok( /^{ "data"/.test( result ), "check returned text" );
41 test("pass-through request object", function() {
45 var target = "data/name.html";
47 var success = function() {
52 ok( $.get(url(target), success), "get" );
53 ok( $.getIfModified(url(target), success), "getIfModified" );
54 ok( $.post(url(target), success), "post" );
55 ok( $.getScript(url("data/test.js"), success), "script" );
56 ok( $.getJSON(url("data/json_obj.js"), success), "json" );
57 ok( $.ajax({url: url(target), success: success}), "generic" );
60 test("global ajaxSettings", function() {
63 var tmp = jQuery.extend({}, jQuery.ajaxSettings);
64 var orig = { url: "data/with_fries.xml", data: null };
67 $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
69 t = jQuery.extend({}, orig);
71 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
73 t = jQuery.extend({}, orig);
76 ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
78 t = jQuery.extend({}, orig);
79 t.data = { zoo: 'a', ping: 'b' };
81 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' }" );
83 jQuery.ajaxSettings = tmp;
86 test("load(String)", function() {
88 stop(true); // check if load can be called with only url
89 $('#first').load("data/name.html", start);
92 test("load(String, Function) - simple: inject text into DOM", function() {
95 $('#first').load(url("data/name.html"), function() {
96 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
101 test("load(String, Function) - check scripts", function() {
104 window.testFoo = undefined;
105 window.foobar = null;
106 var verifyEvaluation = function() {
107 equals( foobar, "bar", 'Check if script src was evaluated after load' );
108 equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
111 $('#first').load(url('data/test.html'), function() {
112 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
113 equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
114 equals( testFoo, "foo", 'Check if script was evaluated after load' );
115 setTimeout(verifyEvaluation, 600);
119 test("load(String, Function) - check file with only a script tag", function() {
123 $('#first').load(url('data/test2.html'), function() {
124 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
125 ok( testFoo == "foo", 'Check if script was evaluated after load' );
130 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
133 $.get(url('data/dashboard.xml'), function(xml) {
135 $('tab', xml).each(function() {
136 content.push($(this).text());
138 equals( content[0], 'blabla', 'Check first tab');
139 equals( content[1], 'blublu', 'Check second tab');
144 test("$.getIfModified(String, Hash, Function)", function() {
147 $.getIfModified(url("data/name.html"), function(msg) {
148 ok( /^ERROR/.test(msg), 'Check ifModified' );
153 test("$.getScript(String, Function) - with callback", function() {
156 $.getScript(url("data/test.js"), function() {
157 equals( foobar, "bar", 'Check if script was evaluated' );
158 setTimeout(start, 100);
162 test("$.getScript(String, Function) - no callback", function() {
165 $.getScript(url("data/test.js"), start);
168 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
172 url: url("data/with_fries.xml"),
174 success: function(resp) {
175 equals( $("properties", resp).length, 1, 'properties in responseXML' );
176 equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
177 equals( $("thing", resp).length, 2, 'things in responseXML' );
183 test("test global handlers - success", function() {
184 expect( isLocal ? 4 : 8 );
187 var counter = { complete: 0, success: 0, error: 0, send: 0 },
188 success = function() { counter.success++ },
189 error = function() { counter.error++ },
190 complete = function() { counter.complete++ },
191 send = function() { counter.send++ };
193 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
195 // start with successful test
196 $.ajax({url: url("data/name.html"), beforeSend: send, success: success, error: error, complete: function() {
197 equals( counter.error, 0, 'Check succesful request, error callback' );
198 equals( counter.success, 2, 'Check succesful request, success callback' );
199 equals( counter.complete, 3, 'Check succesful request, complete callback' );
200 equals( counter.send, 2, 'Check succesful request, send callback' );
203 counter.error = counter.success = counter.complete = counter.send = 0;
206 $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() {
207 equals( counter.error, 2, 'Check failed request, error callback' );
208 equals( counter.success, 0, 'Check failed request, success callback' );
209 equals( counter.complete, 3, 'Check failed request, failed callback' );
210 equals( counter.send, 2, 'Check failed request, send callback' );
218 test("test global handlers - failure", function() {
219 expect( isLocal ? 4 : 8 );
222 var counter = { complete: 0, success: 0, error: 0, send: 0 },
223 success = function() { counter.success++ },
224 error = function() { counter.error++ },
225 complete = function() { counter.complete++ },
226 send = function() { counter.send++ };
230 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
232 $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() {
233 ok( counter.error == 0, 'Check sucesful request without globals' );
234 ok( counter.success == 1, 'Check sucesful request without globals' );
235 ok( counter.complete == 0, 'Check sucesful request without globals' );
236 ok( counter.send == 1, 'Check sucesful request without globals' );
239 counter.error = counter.success = counter.complete = counter.send = 0;
242 $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
244 ok( counter.error == 1, 'Check failed request without globals' );
245 ok( counter.success == 0, 'Check failed request without globals' );
246 ok( counter.complete == 0, 'Check failed request without globals' );
247 ok( counter.send == 1, 'Check failed request without globals' );
255 test("$.ajax - beforeSend", function() {
261 $.ajaxSetup({ timeout: 0 });
264 url: url("data/name.html"),
265 beforeSend: function(xml) {
268 success: function(data) {
269 ok( check, "check beforeSend was executed" );
275 test("$.ajax - dataType html", function() {
282 var verifyEvaluation = function() {
283 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
289 url: url("data/test.html"),
290 success: function(data) {
292 ok( data.match(/^html text/), 'Check content for datatype html' );
293 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
294 setTimeout(verifyEvaluation, 600);
301 test("$.getJSON(String, Hash, Function) - JSON array", function() {
304 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
305 ok( json[0].name == 'John', 'Check JSON: first, name' );
306 ok( json[0].age == 21, 'Check JSON: first, age' );
307 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
308 ok( json[1].age == 25, 'Check JSON: second, age' );
313 test("$.getJSON(String, Function) - JSON object", function() {
316 $.getJSON(url("data/json.php"), function(json) {
317 ok( json.data.lang == 'en', 'Check JSON: lang' );
318 ok( json.data.length == 25, 'Check JSON: length' );
323 test("$.post(String, Hash, Function) - simple with xml", function() {
326 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
327 $('math', xml).each(function() {
328 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
329 ok( $('result', this).text() == '3', 'Check for XML' );
335 test("$.ajaxTimeout(Number) - with global timeout", function() {
342 var pass = function() {
345 ok( true, 'Check local and global callbacks after timeout' );
346 $('#main').unbind("ajaxError");
351 var fail = function(a,b,c) {
352 ok( false, 'Check for timeout failed ' + a + ' ' + b );
356 $('#main').ajaxError(pass);
360 url: url("data/name.php?wait=5"),
369 test("$.ajaxTimeout(Number) with localtimeout", function() {
370 stop(); $.ajaxTimeout(50);
374 url: url("data/name.php?wait=1"),
376 ok( false, 'Check for local timeout failed' );
379 success: function() {
380 ok( true, 'Check for local timeout' );
388 test("$.ajax - simple get", function() {
393 url: url("data/name.php?name=foo"),
394 success: function(msg){
395 ok( msg == 'bar', 'Check for GET' );
401 test("$.ajax - simple post", function() {
406 url: url("data/name.php"),
408 success: function(msg){
409 ok( msg == 'pan', 'Check for POST' );
415 test("ajaxSetup()", function() {
419 url: url("data/name.php?name=foo"),
420 success: function(msg){
421 ok( msg == 'bar', 'Check for GET' );
428 test("custom timeout does not set error message when timeout occurs, see #970", function() {
431 url: "data/name.php?wait=10",
433 error: function(request, status) {
434 ok( status != null, "status shouldn't be null in error handler" );
435 equals( "timeout", status );