Fixed synchrnous requests, improved AJAX inline documentation
[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();
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("load(String, Object, Function) - simple: inject text into DOM", function() {
47         expect(2);
48         stop();
49         $('#first').load("data/name.php", function() {
50                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
51                 start();
52         });
53 });
54
55 test("load(String, Object, Function) - inject without callback", function() {
56         expect(1);
57         stop(); // check if load can be called with only url
58         $('#first').load("data/name.php");
59 });
60
61 test("load(String, Object, Function) - check scripts", function() {
62         expect(7);
63         stop();
64         testFoo = undefined;
65         var verifyEvaluation = function() {
66           ok( foobar == "bar", 'Check if script src was evaluated after load' );
67           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
68           ok( $('#ap').html() == 'bar', 'Check if script evaluation has modified DOM');
69           start();
70         };
71         $('#first').load('data/test.html', function() {
72           ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
73           ok( testFoo == "foo", 'Check if script was evaluated after load' );
74           setTimeout(verifyEvaluation, 600);
75         });
76 });
77
78 test("test global handlers - success", function() {
79         expect(8);
80         stop();
81         var counter = { complete: 0, success: 0, error: 0, send: 0 },
82                 success = function() { counter.success++ },
83                 error = function() { counter.error++ },
84                 complete = function() { counter.complete++ },
85                 send = function() { counter.send++ };
86
87         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
88         // start with successful test
89         $.ajax({url: "data/name.php", beforeSend: send, success: success, error: error, complete: function() {
90           ok( counter.error == 0, 'Check succesful request' );
91           ok( counter.success == 2, 'Check succesful request' );
92           ok( counter.complete == 3, 'Check succesful request' );
93           ok( counter.send == 2, 'Check succesful request' );
94           counter.error = counter.success = counter.complete = counter.send = 0;
95           $.ajaxTimeout(500);
96           $.ajax({url: "data/name.php?wait=5", beforeSend: send, success: success, error: error, complete: function() {
97             ok( counter.error == 2, 'Check failed request' );
98             ok( counter.success == 0, 'Check failed request' );
99             ok( counter.complete == 3, 'Check failed request' );
100             ok( counter.send == 2, 'Check failed request' );
101             start();
102           }});
103         }});
104 });
105
106 test("test global handlers - failure", function() {
107         expect(8);
108         stop();
109         var counter = { complete: 0, success: 0, error: 0, send: 0 },
110                 success = function() { counter.success++ },
111                 error = function() { counter.error++ },
112                 complete = function() { counter.complete++ },
113                 send = function() { counter.send++ };
114         $.ajaxTimeout(0);
115         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
116         $.ajax({url: "data/name.php", global: false, beforeSend: send, success: success, error: error, complete: function() {
117           ok( counter.error == 0, 'Check sucesful request without globals' );
118           ok( counter.success == 1, 'Check sucesful request without globals' );
119           ok( counter.complete == 0, 'Check sucesful request without globals' );
120           ok( counter.send == 1, 'Check sucesful request without globals' );
121           counter.error = counter.success = counter.complete = counter.send = 0;
122           $.ajaxTimeout(500);
123           $.ajax({url: "data/name.php?wait=5", global: false, beforeSend: send, success: success, error: error, complete: function() {
124              ok( counter.error == 1, 'Check failed request without globals' );
125              ok( counter.success == 0, 'Check failed request without globals' );
126              ok( counter.complete == 0, 'Check failed request without globals' );
127              ok( counter.send == 1, 'Check failed request without globals' );
128              start();
129           }});
130         }});
131 });
132
133 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
134         expect(2);
135         stop();
136         $.get('data/dashboard.xml', function(xml) {
137                 var content = [];
138                 $('tab', xml).each(function() {
139                         content.push($(this).text());
140                 });
141                 ok( content[0] == 'blabla', 'Check first tab');
142                 ok( content[1] == 'blublu', 'Check second tab');
143                 start();
144         });
145 });
146
147 test("$.getIfModified(String, Hash, Function)", function() {
148         expect(1);
149         stop();
150         $.getIfModified("data/name.php", function(msg) {
151             ok( /^ERROR/.test(msg), 'Check ifModified' );
152             start();
153         });
154 });
155
156 test("$.getScript(String, Function) - with callback", function() {
157         expect(2);
158         stop();
159         $.getScript("data/test.js", function() {
160                 ok( foobar == "bar", 'Check if script was evaluated' );
161                 setTimeout(start, 100);
162         });
163 });
164
165 test("$.getScript(String, Function) - no callback", function() {
166         expect(1);
167         stop();
168         $.getScript("data/test.js");
169 });
170
171 test("$.getJSON(String, Hash, Function) - JSON array", function() {
172         expect(4);
173         stop();
174         $.getJSON("data/json.php", {json: "array"}, function(json) {
175           ok( json[0].name == 'John', 'Check JSON: first, name' );
176           ok( json[0].age == 21, 'Check JSON: first, age' );
177           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
178           ok( json[1].age == 25, 'Check JSON: second, age' );
179           start();
180         });
181 });
182
183 test("$.getJSON(String, Hash, Function) - JSON object", function() {
184         expect(2);
185         stop();
186         $.getJSON("data/json.php", function(json) {
187           ok( json.data.lang == 'en', 'Check JSON: lang' );
188           ok( json.data.length == 25, 'Check JSON: length' );
189           start();
190         });
191 });
192
193 test("$.post(String, Hash, Function) - simple with xml", function() {
194         expect(2);
195         stop();
196         $.post("data/name.php", {xml: "5-2"}, function(xml){
197           $('math', xml).each(function() {
198                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
199                     ok( $('result', this).text() == '3', 'Check for XML' );
200                  });
201           start();
202         });
203 });
204
205 test("$.ajaxTimeout(Number) - with global timeout", function() {
206         stop();
207         var passed = 0;
208         var timeout;
209         $.ajaxTimeout(1000);
210         var pass = function() {
211                 passed++;
212                 if(passed == 2) {
213                         ok( true, 'Check local and global callbacks after timeout' );
214                         clearTimeout(timeout);
215              $('#main').unbind("ajaxError");
216                         start();
217                 }
218         };
219         var fail = function() {
220                 ok( false, 'Check for timeout failed' );
221                 start();
222         };
223         timeout = setTimeout(fail, 1500);
224         $('#main').ajaxError(pass);
225         $.ajax({
226           type: "GET",
227           url: "data/name.php?wait=5",
228           error: pass,
229           success: fail
230         });
231 });
232
233 test("$.ajaxTimeout(Number) with localtimeout", function() {
234         stop(); $.ajaxTimeout(50);
235         $.ajax({
236           type: "GET",
237           timeout: 5000,
238           url: "data/name.php?wait=1",
239           error: function() {
240                    ok( false, 'Check for local timeout failed' );
241                    start();
242           },
243           success: function() {
244             ok( true, 'Check for local timeout' );
245             start();
246           }
247         });
248         // reset timeout
249         $.ajaxTimeout(0);
250 });
251
252 test("$.ajax - simple get", function() {
253         expect(1);
254         stop();
255         $.ajax({
256           type: "GET",
257           url: "data/name.php?name=foo",
258           success: function(msg){
259             ok( msg == 'bar', 'Check for GET' );
260             start();
261           }
262         });
263 });
264
265 test("$.ajax - simple post", function() {
266         expect(1);
267         stop();
268         $.ajax({
269           type: "POST",
270           url: "data/name.php",
271           data: "name=peter",
272           success: function(msg){
273             ok( msg == 'pan', 'Check for POST' );
274             start();
275           }
276         });
277 });
278         
279 test("$.ajax - dataType html", function() {
280         expect(5);
281         stop();
282         testFoo = undefined;
283         var verifyEvaluation = function() {
284           ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
285           start();
286         };
287         $.ajax({
288           dataType: "html",
289           url: "data/test.html",
290           success: function(data) {
291             ok( data.match(/^html text/), 'Check content for datatype html' );
292             ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
293             setTimeout(verifyEvaluation, 600);
294           }
295         });
296 });
297         
298 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
299         expect(3);
300         stop();
301         $.ajax({
302           url: "data/with_fries.xml", dataType: "xml", type: "GET", data: "", success: function(resp) {
303             ok( $("properties", resp).length == 1, 'properties in responseXML' );
304             ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
305             ok( $("thing", resp).length == 2, 'things in responseXML' );
306             start();
307           }
308         });
309 });
310
311 test("$.ajax - beforeSend", function() {
312         expect(1);
313         stop();
314         var customHeader = "value";
315         $.ajax({
316                 url: "data/name.php", 
317                 data: {'req': true},
318                 beforeSend: function(xml) {
319                         xml.setRequestHeader('X-Custom-Header', customHeader);
320                 },
321                 success: function(data) {
322                         ok( data == customHeader, "check return value, should be the custom header sent" );
323                         start();
324                 }
325         });
326 });