Fix for #534
[jquery.git] / src / ajax / ajaxTest.js
1 module("ajax");
2
3 test("serialize()", function() {
4         expect(1);
5         var data = $(':input').not('button').serialize();
6         // ignore button, IE takes text content as value, not relevant for this test
7         ok( data == 'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo[bar]=&name=name&=foobar&select1=&select2=3&select3=1', 'Check form serialization as query string' );
8 });
9
10 test("param", function() {
11         expect(4);
12         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
13         ok( $.param(params) == "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
14         
15         params = {someName: [1, 2, 3], regularThing: "blah" };
16         ok( $.param(params) == "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
17         
18         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
19         ok( $.param(params) == "foo[]=baz&foo[]=42&foo[]=All%20your%20base%20are%20belong%20to%20us", "more array" );
20         
21         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
22         ok( $.param(params) == "foo[bar]=baz&foo[beep]=42&foo[quux]=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
23 });
24
25 test("pass-through request object", function() {
26         expect(7);
27         stop(true);
28         var count = 0;
29         var success = function() {
30                 if(count++ == 6)
31                         start();
32         }
33         var url = "data/name.php";
34         ok( $.get(url, success), "get" );
35         ok( $.getIfModified(url, success), "getIfModified" );
36         ok( $.post(url, success), "post" );
37         ok( $.getScript("data/test.js", success), "script" );
38         ok( $.getJSON("data/json.php", success), "json" );
39         ok( $.ajax({url: url, success: success}), "generic" );
40 });
41
42 test("synchronous request", function() {
43         ok( /^{ "data"/.test( $.ajax({url: "data/json.php", async: false}).responseText ), "check returned text" );
44 });
45
46 test("synchronous request with callbacks", function() {
47         expect(2);
48         var result;
49         $.ajax({url: "data/json.php", async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
50         ok( /^{ "data"/.test( result ), "check returned text" );
51 });
52
53 test("load(String, Object, Function) - simple: inject text into DOM", function() {
54         expect(2);
55         stop();
56         $('#first').load("data/name.php", function() {
57                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
58                 start();
59         });
60 });
61
62 test("load(String, Object, Function) - inject without callback", function() {
63         expect(1);
64         stop(true); // check if load can be called with only url
65         $('#first').load("data/name.php");
66 });
67
68 test("load(String, Object, Function) - check scripts", function() {
69         expect(7);
70         stop();
71         testFoo = undefined;
72         var verifyEvaluation = function() {
73           ok( foobar == "bar", 'Check if script src was evaluated after load' );
74           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
75           ok( $('#ap').html() == 'bar', 'Check if script evaluation has modified DOM');
76           start();
77         };
78         $('#first').load('data/test.html', function() {
79           ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
80           ok( testFoo == "foo", 'Check if script was evaluated after load' );
81           setTimeout(verifyEvaluation, 600);
82         });
83 });
84
85 test("test global handlers - success", function() {
86         expect(8);
87         stop();
88         var counter = { complete: 0, success: 0, error: 0, send: 0 },
89                 success = function() { counter.success++ },
90                 error = function() { counter.error++ },
91                 complete = function() { counter.complete++ },
92                 send = function() { counter.send++ };
93
94         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
95         // start with successful test
96         $.ajax({url: "data/name.php", beforeSend: send, success: success, error: error, complete: function() {
97           ok( counter.error == 0, 'Check succesful request' );
98           ok( counter.success == 2, 'Check succesful request' );
99           ok( counter.complete == 3, 'Check succesful request' );
100           ok( counter.send == 2, 'Check succesful request' );
101           counter.error = counter.success = counter.complete = counter.send = 0;
102           $.ajaxTimeout(500);
103           $.ajax({url: "data/name.php?wait=5", beforeSend: send, success: success, error: error, complete: function() {
104             ok( counter.error == 2, 'Check failed request' );
105             ok( counter.success == 0, 'Check failed request' );
106             ok( counter.complete == 3, 'Check failed request' );
107             ok( counter.send == 2, 'Check failed request' );
108             start();
109           }});
110         }});
111 });
112
113 test("test global handlers - failure", function() {
114         expect(8);
115         stop();
116         var counter = { complete: 0, success: 0, error: 0, send: 0 },
117                 success = function() { counter.success++ },
118                 error = function() { counter.error++ },
119                 complete = function() { counter.complete++ },
120                 send = function() { counter.send++ };
121         $.ajaxTimeout(0);
122         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
123         $.ajax({url: "data/name.php", global: false, beforeSend: send, success: success, error: error, complete: function() {
124           ok( counter.error == 0, 'Check sucesful request without globals' );
125           ok( counter.success == 1, 'Check sucesful request without globals' );
126           ok( counter.complete == 0, 'Check sucesful request without globals' );
127           ok( counter.send == 1, 'Check sucesful request without globals' );
128           counter.error = counter.success = counter.complete = counter.send = 0;
129           $.ajaxTimeout(500);
130           $.ajax({url: "data/name.php?wait=5", global: false, beforeSend: send, success: success, error: error, complete: function() {
131                  var x = counter;
132              ok( counter.error == 1, 'Check failed request without globals' );
133              ok( counter.success == 0, 'Check failed request without globals' );
134              ok( counter.complete == 0, 'Check failed request without globals' );
135              ok( counter.send == 1, 'Check failed request without globals' );
136              start();
137           }});
138         }});
139 });
140
141 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
142         expect(2);
143         stop();
144         $.get('data/dashboard.xml', function(xml) {
145                 var content = [];
146                 $('tab', xml).each(function() {
147                         content.push($(this).text());
148                 });
149                 ok( content[0] == 'blabla', 'Check first tab');
150                 ok( content[1] == 'blublu', 'Check second tab');
151                 start();
152         });
153 });
154
155 test("$.getIfModified(String, Hash, Function)", function() {
156         expect(1);
157         stop();
158         $.getIfModified("data/name.php", function(msg) {
159             ok( /^ERROR/.test(msg), 'Check ifModified' );
160             start();
161         });
162 });
163
164 test("$.getScript(String, Function) - with callback", function() {
165         expect(2);
166         stop();
167         $.getScript("data/test.js", function() {
168                 ok( foobar == "bar", 'Check if script was evaluated' );
169                 setTimeout(start, 100);
170         });
171 });
172
173 test("$.getScript(String, Function) - no callback", function() {
174         expect(1);
175         stop(true);
176         $.getScript("data/test.js");
177 });
178
179 test("$.getJSON(String, Hash, Function) - JSON array", function() {
180         expect(4);
181         stop();
182         $.getJSON("data/json.php", {json: "array"}, function(json) {
183           ok( json[0].name == 'John', 'Check JSON: first, name' );
184           ok( json[0].age == 21, 'Check JSON: first, age' );
185           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
186           ok( json[1].age == 25, 'Check JSON: second, age' );
187           start();
188         });
189 });
190
191 test("$.getJSON(String, Hash, Function) - JSON object", function() {
192         expect(2);
193         stop();
194         $.getJSON("data/json.php", function(json) {
195           ok( json.data.lang == 'en', 'Check JSON: lang' );
196           ok( json.data.length == 25, 'Check JSON: length' );
197           start();
198         });
199 });
200
201 test("$.post(String, Hash, Function) - simple with xml", function() {
202         expect(2);
203         stop();
204         $.post("data/name.php", {xml: "5-2"}, function(xml){
205           $('math', xml).each(function() {
206                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
207                     ok( $('result', this).text() == '3', 'Check for XML' );
208                  });
209           start();
210         });
211 });
212
213 test("$.ajaxTimeout(Number) - with global timeout", function() {
214         stop();
215         var passed = 0;
216         var timeout;
217         $.ajaxTimeout(1000);
218         var pass = function() {
219                 passed++;
220                 if(passed == 2) {
221                         ok( true, 'Check local and global callbacks after timeout' );
222                         clearTimeout(timeout);
223              $('#main').unbind("ajaxError");
224                         start();
225                 }
226         };
227         var fail = function() {
228                 ok( false, 'Check for timeout failed' );
229                 start();
230         };
231         timeout = setTimeout(fail, 1500);
232         $('#main').ajaxError(pass);
233         $.ajax({
234           type: "GET",
235           url: "data/name.php?wait=5",
236           error: pass,
237           success: fail
238         });
239         // reset timeout
240         $.ajaxTimeout(0);
241 });
242
243 test("$.ajaxTimeout(Number) with localtimeout", function() {
244         stop(); $.ajaxTimeout(50);
245         $.ajax({
246           type: "GET",
247           timeout: 5000,
248           url: "data/name.php?wait=1",
249           error: function() {
250                    ok( false, 'Check for local timeout failed' );
251                    start();
252           },
253           success: function() {
254             ok( true, 'Check for local timeout' );
255             start();
256           }
257         });
258         // reset timeout
259         $.ajaxTimeout(0);
260 });
261
262 test("$.ajax - simple get", function() {
263         expect(1);
264         stop();
265         $.ajax({
266           type: "GET",
267           url: "data/name.php?name=foo",
268           success: function(msg){
269             ok( msg == 'bar', 'Check for GET' );
270             start();
271           }
272         });
273 });
274
275 test("$.ajax - simple post", function() {
276         expect(1);
277         stop();
278         $.ajax({
279           type: "POST",
280           url: "data/name.php",
281           data: "name=peter",
282           success: function(msg){
283             ok( msg == 'pan', 'Check for POST' );
284             start();
285           }
286         });
287 });
288         
289 test("$.ajax - dataType html", function() {
290         expect(5);
291         stop();
292         testFoo = undefined;
293         var verifyEvaluation = function() {
294           ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
295           start();
296         };
297         $.ajax({
298           dataType: "html",
299           url: "data/test.html",
300           success: function(data) {
301             ok( data.match(/^html text/), 'Check content for datatype html' );
302             ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
303             setTimeout(verifyEvaluation, 600);
304           }
305         });
306 });
307         
308 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
309         expect(3);
310         stop();
311         $.ajax({
312           url: "data/with_fries.xml", dataType: "xml", type: "GET", data: "", success: function(resp) {
313             ok( $("properties", resp).length == 1, 'properties in responseXML' );
314             ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
315             ok( $("thing", resp).length == 2, 'things in responseXML' );
316             start();
317           }
318         });
319 });
320
321 test("$.ajax - beforeSend", function() {
322         expect(1);
323         stop();
324         var check = false;
325         $.ajax({
326                 url: "data/name.php", 
327                 data: {'req': true},
328                 beforeSend: function(xml) {
329                         check = true
330                 },
331                 success: function(data) {
332                         ok( check, "check beforeSend was executed" );
333                         start();
334                 }
335         });
336 });
337
338 test("ajaxSetup()", function() {
339         expect(1);
340         stop();
341         $.ajaxSetup({
342                 url: "data/name.php?name=foo",
343                 success: function(msg){
344                 ok( msg == 'bar', 'Check for GET' );
345                         start();
346                 }
347         });
348         $.ajax();
349 });