Complete overhaul of the Ajax test suite, it's now passing in all browsers. In order...
[jquery.git] / src / ajax / ajaxTest.js
1 module("ajax");
2
3 // Safari 3 crashes when running these tests, sigh
4 if ( !jQuery.browser.safari ) {
5
6 test("serialize()", function() {
7         expect(1);
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');
12 });
13
14 test("$.param()", function() {
15         expect(4);
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" );
18         
19         params = {someName: [1, 2, 3], regularThing: "blah" };
20         equals( $.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
21         
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" );
24         
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" );
27 });
28
29 test("synchronous request", function() {
30         expect(1);
31         ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
32 });
33
34 test("synchronous request with callbacks", function() {
35         expect(2);
36         var result;
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" );
39 });
40
41 test("pass-through request object", function() {
42         expect(7);
43         stop(true);
44         
45         var target = "data/name.html";
46         var count = 0;
47         var success = function() {
48                 if(count++ == 5)
49                         start();
50         };
51         
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" );
58 });
59
60 test("global ajaxSettings", function() {
61         expect(3);
62
63         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
64         var orig = { url: "data/with_fries.xml", data: null };
65         var t;
66
67         $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
68
69         t = jQuery.extend({}, orig);
70         $.ajax(t);
71         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
72
73         t = jQuery.extend({}, orig);
74         t.data = {};
75         $.ajax(t);
76         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
77
78         t = jQuery.extend({}, orig);
79         t.data = { zoo: 'a', ping: 'b' };
80         $.ajax(t);
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' }" );
82         
83         jQuery.ajaxSettings = tmp;
84 });
85
86 test("load(String)", function() {
87         expect(1);
88         stop(true); // check if load can be called with only url
89         $('#first').load("data/name.html", start);
90 });
91
92 test("load(String, Function) - simple: inject text into DOM", function() {
93         expect(2);
94         stop();
95         $('#first').load(url("data/name.html"), function() {
96                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
97                 start();
98         });
99 });
100
101 test("load(String, Function) - check scripts", function() {
102         expect(7);
103         stop();
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');
109           start();
110         };
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);
116         });
117 });
118
119 test("load(String, Function) - check file with only a script tag", function() {
120         expect(3);
121         stop();
122         testFoo = undefined;
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' );
126           start();
127         });
128 });
129
130 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
131         expect(2);
132         stop();
133         $.get(url('data/dashboard.xml'), function(xml) {
134                 var content = [];
135                 $('tab', xml).each(function() {
136                         content.push($(this).text());
137                 });
138                 equals( content[0], 'blabla', 'Check first tab');
139                 equals( content[1], 'blublu', 'Check second tab');
140                 start();
141         });
142 });
143
144 test("$.getIfModified(String, Hash, Function)", function() {
145         expect(1);
146         stop();
147         $.getIfModified(url("data/name.html"), function(msg) {
148             ok( /^ERROR/.test(msg), 'Check ifModified' );
149             start();
150         });
151 });
152
153 test("$.getScript(String, Function) - with callback", function() {
154         expect(2);
155         stop();
156         $.getScript(url("data/test.js"), function() {
157                 equals( foobar, "bar", 'Check if script was evaluated' );
158                 setTimeout(start, 100);
159         });
160 });
161
162 test("$.getScript(String, Function) - no callback", function() {
163         expect(1);
164         stop(true);
165         $.getScript(url("data/test.js"), start);
166 });
167
168 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
169         expect(3);
170         stop();
171         $.ajax({
172           url: url("data/with_fries.xml"),
173           dataType: "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' );
178             start();
179           }
180         });
181 });
182
183 test("test global handlers - success", function() {
184         expect( isLocal ? 4 : 8 );
185         stop();
186         
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++ };
192
193         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
194         
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' );
201           
202           if ( !isLocal ) {
203                   counter.error = counter.success = counter.complete = counter.send = 0;
204                   $.ajaxTimeout(500);
205                   
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' );
211                         start();
212                   }});
213           } else
214                   start();
215         }});
216 });
217
218 test("test global handlers - failure", function() {
219         expect( isLocal ? 4 : 8 );
220         stop();
221         
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++ };
227                 
228         $.ajaxTimeout(0);
229         
230         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
231         
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' );
237           
238           if ( !isLocal ) {
239                   counter.error = counter.success = counter.complete = counter.send = 0;
240                   $.ajaxTimeout(500);
241                   
242                   $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
243                          var x = counter;
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' );
248                          start();
249                   }});
250           } else
251                   start();
252         }});
253 });
254
255 test("$.ajax - beforeSend", function() {
256         expect(1);
257         stop();
258         
259         var check = false;
260         
261         $.ajaxSetup({ timeout: 0 });
262         
263         $.ajax({
264                 url: url("data/name.html"), 
265                 beforeSend: function(xml) {
266                         check = true;
267                 },
268                 success: function(data) {
269                         ok( check, "check beforeSend was executed" );
270                         start();
271                 }
272         });
273 });
274
275 test("$.ajax - dataType html", function() {
276         expect(5);
277         stop();
278         
279         foobar = null;
280         testFoo = undefined;
281         
282         var verifyEvaluation = function() {
283           ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
284           start();
285         };
286         
287         $.ajax({
288           dataType: "html",
289           url: url("data/test.html"),
290           success: function(data) {
291                 $("#ap").html(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);
295           }
296         });
297 });
298
299 if ( !isLocal ) {
300
301 test("$.getJSON(String, Hash, Function) - JSON array", function() {
302         expect(4);
303         stop();
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' );
309           start();
310         });
311 });
312
313 test("$.getJSON(String, Function) - JSON object", function() {
314         expect(2);
315         stop();
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' );
319           start();
320         });
321 });
322
323 test("$.post(String, Hash, Function) - simple with xml", function() {
324         expect(2);
325         stop();
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' );
330                  });
331           start();
332         });
333 });
334
335 test("$.ajaxTimeout(Number) - with global timeout", function() {
336         stop();
337         
338         var passed = 0;
339
340         $.ajaxTimeout(1000);
341         
342         var pass = function() {
343                 passed++;
344                 if ( passed == 2 ) {
345                         ok( true, 'Check local and global callbacks after timeout' );
346                 $('#main').unbind("ajaxError");
347                         start();
348                 }
349         };
350         
351         var fail = function(a,b,c) {
352                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
353                 start();
354         };
355         
356         $('#main').ajaxError(pass);
357         
358         $.ajax({
359           type: "GET",
360           url: url("data/name.php?wait=5"),
361           error: pass,
362           success: fail
363         });
364         
365         // reset timeout
366         $.ajaxTimeout(0);
367 });
368
369 test("$.ajaxTimeout(Number) with localtimeout", function() {
370         stop(); $.ajaxTimeout(50);
371         $.ajax({
372           type: "GET",
373           timeout: 5000,
374           url: url("data/name.php?wait=1"),
375           error: function() {
376                    ok( false, 'Check for local timeout failed' );
377                    start();
378           },
379           success: function() {
380             ok( true, 'Check for local timeout' );
381             start();
382           }
383         });
384         // reset timeout
385         $.ajaxTimeout(0);
386 });
387
388 test("$.ajax - simple get", function() {
389         expect(1);
390         stop();
391         $.ajax({
392           type: "GET",
393           url: url("data/name.php?name=foo"),
394           success: function(msg){
395             ok( msg == 'bar', 'Check for GET' );
396             start();
397           }
398         });
399 });
400
401 test("$.ajax - simple post", function() {
402         expect(1);
403         stop();
404         $.ajax({
405           type: "POST",
406           url: url("data/name.php"),
407           data: "name=peter",
408           success: function(msg){
409             ok( msg == 'pan', 'Check for POST' );
410             start();
411           }
412         });
413 });
414
415 test("ajaxSetup()", function() {
416         expect(1);
417         stop();
418         $.ajaxSetup({
419                 url: url("data/name.php?name=foo"),
420                 success: function(msg){
421                 ok( msg == 'bar', 'Check for GET' );
422                         start();
423                 }
424         });
425         $.ajax();
426 });
427
428 test("custom timeout does not set error message when timeout occurs, see #970", function() {
429         stop();
430         $.ajax({
431                 url: "data/name.php?wait=10",
432                 timeout: 500,
433                 error: function(request, status) {
434                         ok( status != null, "status shouldn't be null in error handler" );
435                         equals( "timeout", status );
436                         start();
437                 }
438         });
439 });
440
441 }
442
443 }