Got some XHR tests to run, still some hanging threads (need to investigate). Started...
[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%5Bbar%5D=&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%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=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%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
23 });
24
25 test("evalScripts() with no script elements", function() {
26     expect(2);
27
28     var data = "this is just some bogus text";
29     $('#foo').html(data);
30     ok ( true, 'before evalScripts()');
31     try {
32         $('#foo').evalScripts();
33     } catch(e) {
34         ok (false, 'exception evaluating scripts: ' + e.message);
35     }
36     ok ( true, 'after evalScripts()');
37 });
38
39 test("synchronous request", function() {
40         ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
41 });
42
43 test("synchronous request with callbacks", function() {
44         expect(2);
45         var result;
46         $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
47         ok( /^{ "data"/.test( result ), "check returned text" );
48 });
49
50 test("pass-through request object", function() {
51         expect(7);
52         stop(true);
53         var count = 0;
54         var success = function() {
55                 if(count++ == 6)
56                         start();
57         }
58         var target = "data/name.html";
59         ok( $.get(url(target), success), "get" );
60         ok( $.getIfModified(url(target), success), "getIfModified" );
61         ok( $.post(url(target), success), "post" );
62         ok( $.getScript(url("data/test.js"), success), "script" );
63         ok( $.getJSON(url("data/json_obj.js"), success), "json" );
64         ok( $.ajax({url: url(target), success: success}), "generic" );
65 });
66
67 test("load(String, Object, Function) - simple: inject text into DOM", function() {
68         expect(2);
69         stop();
70         $('#first').load(url("data/name.html"), function() {
71                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
72                 start();
73         });
74 });
75
76 test("load(String, Object, Function) - inject without callback", function() {
77         expect(1);
78         stop(true); // check if load can be called with only url
79         $('#first').load("data/name.html");
80 });
81
82 if ( location.protocol != "file:" ) {
83
84 test("load(String, Object, Function) - check scripts", function() {
85         expect(7);
86         stop();
87         window.testFoo = undefined;
88         window.foobar = null;
89         var verifyEvaluation = function() {
90           ok( foobar == "bar", 'Check if script src was evaluated after load' );
91           ok( $('#ap').html() == 'bar', 'Check if script evaluation has modified DOM');
92           start();
93         };
94         $('#first').load(url('data/test.php'), function() {
95           ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
96           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
97           ok( testFoo == "foo", 'Check if script was evaluated after load' );
98           setTimeout(verifyEvaluation, 600);
99         });
100 });
101
102 test("load(String, Object, Function) - check file with only a script tag", function() {
103         expect(3);
104         stop();
105         testFoo = undefined;
106         $('#first').load(url('data/test2.php'), function() {
107           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
108           ok( testFoo == "foo", 'Check if script was evaluated after load' );
109           start();
110         });
111 });
112
113 test("test global handlers - success", 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
122         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
123         // start with successful test
124         $.ajax({url: url("data/name.php"), beforeSend: send, success: success, error: error, complete: function() {
125           ok( counter.error == 0, 'Check succesful request' );
126           ok( counter.success == 2, 'Check succesful request' );
127           ok( counter.complete == 3, 'Check succesful request' );
128           ok( counter.send == 2, 'Check succesful request' );
129           counter.error = counter.success = counter.complete = counter.send = 0;
130           $.ajaxTimeout(500);
131           $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() {
132             ok( counter.error == 2, 'Check failed request' );
133             ok( counter.success == 0, 'Check failed request' );
134             ok( counter.complete == 3, 'Check failed request' );
135             ok( counter.send == 2, 'Check failed request' );
136             start();
137           }});
138         }});
139 });
140
141 test("test global handlers - failure", function() {
142         expect(8);
143         stop();
144         var counter = { complete: 0, success: 0, error: 0, send: 0 },
145                 success = function() { counter.success++ },
146                 error = function() { counter.error++ },
147                 complete = function() { counter.complete++ },
148                 send = function() { counter.send++ };
149         $.ajaxTimeout(0);
150         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
151         $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() {
152           ok( counter.error == 0, 'Check sucesful request without globals' );
153           ok( counter.success == 1, 'Check sucesful request without globals' );
154           ok( counter.complete == 0, 'Check sucesful request without globals' );
155           ok( counter.send == 1, 'Check sucesful request without globals' );
156           counter.error = counter.success = counter.complete = counter.send = 0;
157           $.ajaxTimeout(500);
158           $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
159                  var x = counter;
160              ok( counter.error == 1, 'Check failed request without globals' );
161              ok( counter.success == 0, 'Check failed request without globals' );
162              ok( counter.complete == 0, 'Check failed request without globals' );
163              ok( counter.send == 1, 'Check failed request without globals' );
164              start();
165           }});
166         }});
167 });
168
169 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
170         expect(2);
171         stop();
172         $.get(url('data/dashboard.xml'), function(xml) {
173                 var content = [];
174                 $('tab', xml).each(function() {
175                         content.push($(this).text());
176                 });
177                 ok( content[0] == 'blabla', 'Check first tab');
178                 ok( content[1] == 'blublu', 'Check second tab');
179                 start();
180         });
181 });
182
183 test("$.getIfModified(String, Hash, Function)", function() {
184         expect(1);
185         stop();
186         $.getIfModified(url("data/name.php"), function(msg) {
187             ok( /^ERROR/.test(msg), 'Check ifModified' );
188             start();
189         });
190 });
191
192 test("$.getScript(String, Function) - with callback", function() {
193         expect(2);
194         stop();
195         $.getScript(url("data/test.js"), function() {
196                 ok( foobar == "bar", 'Check if script was evaluated' );
197                 setTimeout(start, 100);
198         });
199 });
200
201 test("$.getScript(String, Function) - no callback", function() {
202         expect(1);
203         stop(true);
204         $.getScript(url("data/test.js"));
205 });
206
207 test("$.getJSON(String, Hash, Function) - JSON array", function() {
208         expect(4);
209         stop();
210         $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
211           ok( json[0].name == 'John', 'Check JSON: first, name' );
212           ok( json[0].age == 21, 'Check JSON: first, age' );
213           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
214           ok( json[1].age == 25, 'Check JSON: second, age' );
215           start();
216         });
217 });
218
219 test("$.getJSON(String, Hash, Function) - JSON object", function() {
220         expect(2);
221         stop();
222         $.getJSON(url("data/json.php"), function(json) {
223           ok( json.data.lang == 'en', 'Check JSON: lang' );
224           ok( json.data.length == 25, 'Check JSON: length' );
225           start();
226         });
227 });
228
229 test("$.post(String, Hash, Function) - simple with xml", function() {
230         expect(2);
231         stop();
232         $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
233           $('math', xml).each(function() {
234                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
235                     ok( $('result', this).text() == '3', 'Check for XML' );
236                  });
237           start();
238         });
239 });
240
241 test("$.ajaxTimeout(Number) - with global timeout", function() {
242         stop();
243         var passed = 0;
244         var timeout;
245         $.ajaxTimeout(1000);
246         var pass = function() {
247                 passed++;
248                 if(passed == 2) {
249                         ok( true, 'Check local and global callbacks after timeout' );
250                         clearTimeout(timeout);
251              $('#main').unbind("ajaxError");
252                         start();
253                 }
254         };
255         var fail = function() {
256                 ok( false, 'Check for timeout failed' );
257                 start();
258         };
259         timeout = setTimeout(fail, 1500);
260         $('#main').ajaxError(pass);
261         $.ajax({
262           type: "GET",
263           url: url("data/name.php?wait=5"),
264           error: pass,
265           success: fail
266         });
267         // reset timeout
268         $.ajaxTimeout(0);
269 });
270
271 test("$.ajaxTimeout(Number) with localtimeout", function() {
272         stop(); $.ajaxTimeout(50);
273         $.ajax({
274           type: "GET",
275           timeout: 5000,
276           url: url("data/name.php?wait=1"),
277           error: function() {
278                    ok( false, 'Check for local timeout failed' );
279                    start();
280           },
281           success: function() {
282             ok( true, 'Check for local timeout' );
283             start();
284           }
285         });
286         // reset timeout
287         $.ajaxTimeout(0);
288 });
289
290 test("$.ajax - simple get", function() {
291         expect(1);
292         stop();
293         $.ajax({
294           type: "GET",
295           url: url("data/name.php?name=foo"),
296           success: function(msg){
297             ok( msg == 'bar', 'Check for GET' );
298             start();
299           }
300         });
301 });
302
303 test("$.ajax - simple post", function() {
304         expect(1);
305         stop();
306         $.ajax({
307           type: "POST",
308           url: url("data/name.php"),
309           data: "name=peter",
310           success: function(msg){
311             ok( msg == 'pan', 'Check for POST' );
312             start();
313           }
314         });
315 });
316         
317 test("$.ajax - dataType html", function() {
318         expect(5);
319         stop();
320         foobar = null;
321         testFoo = undefined;
322         var verifyEvaluation = function() {
323           ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
324           start();
325         };
326         $.ajax({
327           dataType: "html",
328           url: url("data/test.php"),
329           success: function(data) {
330             ok( data.match(/^html text/), 'Check content for datatype html' );
331             ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
332             setTimeout(verifyEvaluation, 600);
333           }
334         });
335 });
336         
337 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
338         expect(3);
339         stop();
340         $.ajax({
341           url: url("data/with_fries.xml"),
342           dataType: "xml",
343           success: function(resp) {
344             ok( $("properties", resp).length == 1, 'properties in responseXML' );
345             ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
346             ok( $("thing", resp).length == 2, 'things in responseXML' );
347             start();
348           }
349         });
350 });
351
352 test("$.ajax - beforeSend", function() {
353         expect(1);
354         stop();
355         var check = false;
356         $.ajax({
357                 url: url("data/name.php"), 
358                 data: {'req': true},
359                 beforeSend: function(xml) {
360                         check = true
361                 },
362                 success: function(data) {
363                         ok( check, "check beforeSend was executed" );
364                         start();
365                 }
366         });
367 });
368
369 test("ajaxSetup()", function() {
370         expect(1);
371         stop();
372         $.ajaxSetup({
373                 url: url("data/name.php?name=foo"),
374                 success: function(msg){
375                 ok( msg == 'bar', 'Check for GET' );
376                         start();
377                 }
378         });
379         $.ajax();
380 });
381
382 test("custom timeout does not set error message when timeout occurs, see #970", function() {
383         stop();
384         $.ajax({
385                 url: "data/name.php?wait=10",
386                 timeout: 500,
387                 error: function(request, status) {
388                         ok( status != null, "status shouldn't be null in error handler" );
389                         equals( "timeout", status );
390                         start();
391                 }
392         });
393 });
394
395 }