Brought back a husk method to represent evalScripts. I didn't realize so many plugins...
[jquery.git] / src / ajax / ajaxTest.js
1 module("ajax");
2
3 // Safari 3 randomly crashes when running these tests,
4 // but only in the full suite - you can run just the Ajax
5 // tests and they'll pass
6 if ( !jQuery.browser.safari ) {
7
8 test("serialize()", function() {
9         expect(1);
10         // ignore button, IE takes text content as value, not relevant for this test
11         equals( $(':input').not('button').serialize(), 
12                 '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=', 
13                 'Check form serialization as query string');
14 });
15
16 test("$.param()", function() {
17         expect(4);
18         var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
19         equals( $.param(params), "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
20         
21         params = {someName: [1, 2, 3], regularThing: "blah" };
22         equals( $.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
23         
24         params = {"foo[]":["baz", 42, "All your base are belong to us"]};
25         equals( $.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All%20your%20base%20are%20belong%20to%20us", "more array" );
26         
27         params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
28         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 });
30
31 test("synchronous request", function() {
32         expect(1);
33         ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
34 });
35
36 test("synchronous request with callbacks", function() {
37         expect(2);
38         var result;
39         $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
40         ok( /^{ "data"/.test( result ), "check returned text" );
41 });
42
43 test("pass-through request object", function() {
44         expect(1);
45         stop(true);
46         
47         var target = "data/name.html";
48         var count = 0;
49         var success = function() {
50                 // Disabled
51                 //if(count++ == 5)
52                 start();
53         };
54         
55         /* Test disabled, too many simultaneous requests
56         ok( $.get(url(target), success), "get" );
57         ok( $.getIfModified(url(target), success), "getIfModified" );
58         ok( $.post(url(target), success), "post" );
59         ok( $.getScript(url("data/test.js"), success), "script" );
60         ok( $.getJSON(url("data/json_obj.js"), success), "json" );
61         */
62         ok( $.ajax({url: url(target), success: success}), "generic" );
63 });
64
65 test("global ajaxSettings", function() {
66         expect(3);
67
68         var tmp = jQuery.extend({}, jQuery.ajaxSettings);
69         var orig = { url: "data/with_fries.xml", data: null };
70         var t;
71
72         $.ajaxSetup({ data: {foo: 'bar', bar: 'BAR'} });
73
74         t = jQuery.extend({}, orig);
75         $.ajax(t);
76         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending null" );
77
78         t = jQuery.extend({}, orig);
79         t.data = {};
80         $.ajax(t);
81         ok( t.url.indexOf('foo') > -1 && t.url.indexOf('bar') > -1, "Check extending {}" );
82
83         t = jQuery.extend({}, orig);
84         t.data = { zoo: 'a', ping: 'b' };
85         $.ajax(t);
86         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' }" );
87         
88         jQuery.ajaxSettings = tmp;
89 });
90
91 test("load(String)", function() {
92         expect(1);
93         stop(true); // check if load can be called with only url
94         $('#first').load("data/name.html", start);
95 });
96
97 test("load(String, Function) - simple: inject text into DOM", function() {
98         expect(2);
99         stop();
100         $('#first').load(url("data/name.html"), function() {
101                 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
102                 start();
103         });
104 });
105
106 test("load(String, Function) - check scripts", function() {
107         expect(7);
108         stop();
109         window.testFoo = undefined;
110         window.foobar = null;
111         var verifyEvaluation = function() {
112           equals( foobar, "bar", 'Check if script src was evaluated after load' );
113           equals( $('#ap').html(), 'bar', 'Check if script evaluation has modified DOM');
114           start();
115         };
116         $('#first').load(url('data/test.html'), function() {
117           ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
118           equals( $('#foo').html(), 'foo', 'Check if script evaluation has modified DOM');
119           equals( testFoo, "foo", 'Check if script was evaluated after load' );
120           setTimeout(verifyEvaluation, 600);
121         });
122 });
123
124 test("load(String, Function) - check file with only a script tag", function() {
125         expect(3);
126         stop();
127         testFoo = undefined;
128         $('#first').load(url('data/test2.html'), function() {
129           ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
130           ok( testFoo == "foo", 'Check if script was evaluated after load' );
131           start();
132         });
133 });
134
135 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
136         expect(2);
137         stop();
138         $.get(url('data/dashboard.xml'), function(xml) {
139                 var content = [];
140                 $('tab', xml).each(function() {
141                         content.push($(this).text());
142                 });
143                 equals( content[0], 'blabla', 'Check first tab');
144                 equals( content[1], 'blublu', 'Check second tab');
145                 start();
146         });
147 });
148
149 test("$.getIfModified(String, Hash, Function)", function() {
150         expect(1);
151         stop();
152         $.getIfModified(url("data/name.html"), function(msg) {
153             ok( /^ERROR/.test(msg), 'Check ifModified' );
154             start();
155         });
156 });
157
158 test("$.getScript(String, Function) - with callback", function() {
159         expect(2);
160         stop();
161         $.getScript(url("data/test.js"), function() {
162                 equals( foobar, "bar", 'Check if script was evaluated' );
163                 setTimeout(start, 100);
164         });
165 });
166
167 test("$.getScript(String, Function) - no callback", function() {
168         expect(1);
169         stop(true);
170         $.getScript(url("data/test.js"), start);
171 });
172
173 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
174         expect(3);
175         stop();
176         $.ajax({
177           url: url("data/with_fries.xml"),
178           dataType: "xml",
179           success: function(resp) {
180             equals( $("properties", resp).length, 1, 'properties in responseXML' );
181             equals( $("jsconf", resp).length, 1, 'jsconf in responseXML' );
182             equals( $("thing", resp).length, 2, 'things in responseXML' );
183             start();
184           }
185         });
186 });
187
188 test("test global handlers - success", function() {
189         expect( isLocal ? 4 : 8 );
190         stop();
191         
192         var counter = { complete: 0, success: 0, error: 0, send: 0 },
193                 success = function() { counter.success++ },
194                 error = function() { counter.error++ },
195                 complete = function() { counter.complete++ },
196                 send = function() { counter.send++ };
197
198         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
199         
200         // start with successful test
201         $.ajax({url: url("data/name.html"), beforeSend: send, success: success, error: error, complete: function() {
202           equals( counter.error, 0, 'Check succesful request, error callback' );
203           equals( counter.success, 2, 'Check succesful request, success callback' );
204           equals( counter.complete, 3, 'Check succesful request, complete callback' );
205           equals( counter.send, 2, 'Check succesful request, send callback' );
206           
207           if ( !isLocal ) {
208                   counter.error = counter.success = counter.complete = counter.send = 0;
209                   $.ajaxTimeout(500);
210                   
211                   $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() {
212                         equals( counter.error, 2, 'Check failed request, error callback' );
213                         equals( counter.success, 0, 'Check failed request, success callback' );
214                         equals( counter.complete, 3, 'Check failed request, failed callback' );
215                         equals( counter.send, 2, 'Check failed request, send callback' );
216                         start();
217                   }});
218           } else
219                   start();
220         }});
221 });
222
223 test("test global handlers - failure", function() {
224         expect( isLocal ? 4 : 8 );
225         stop();
226         
227         var counter = { complete: 0, success: 0, error: 0, send: 0 },
228                 success = function() { counter.success++ },
229                 error = function() { counter.error++ },
230                 complete = function() { counter.complete++ },
231                 send = function() { counter.send++ };
232                 
233         $.ajaxTimeout(0);
234         
235         $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
236         
237         $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() {
238           ok( counter.error == 0, 'Check sucesful request without globals' );
239           ok( counter.success == 1, 'Check sucesful request without globals' );
240           ok( counter.complete == 0, 'Check sucesful request without globals' );
241           ok( counter.send == 1, 'Check sucesful request without globals' );
242           
243           if ( !isLocal ) {
244                   counter.error = counter.success = counter.complete = counter.send = 0;
245                   $.ajaxTimeout(500);
246                   
247                   $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
248                          var x = counter;
249                          ok( counter.error == 1, 'Check failed request without globals' );
250                          ok( counter.success == 0, 'Check failed request without globals' );
251                          ok( counter.complete == 0, 'Check failed request without globals' );
252                          ok( counter.send == 1, 'Check failed request without globals' );
253                          start();
254                   }});
255           } else
256                   start();
257         }});
258 });
259
260 test("$.ajax - beforeSend", function() {
261         expect(1);
262         stop();
263         
264         var check = false;
265         
266         $.ajaxSetup({ timeout: 0 });
267         
268         $.ajax({
269                 url: url("data/name.html"), 
270                 beforeSend: function(xml) {
271                         check = true;
272                 },
273                 success: function(data) {
274                         ok( check, "check beforeSend was executed" );
275                         start();
276                 }
277         });
278 });
279
280 test("$.ajax - dataType html", function() {
281         expect(5);
282         stop();
283         
284         foobar = null;
285         testFoo = undefined;
286         
287         var verifyEvaluation = function() {
288           ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
289           ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
290           start();
291         };
292         
293         $.ajax({
294           dataType: "html",
295           url: url("data/test.html"),
296           success: function(data) {
297                 $("#ap").html(data);
298             ok( data.match(/^html text/), 'Check content for datatype html' );
299             setTimeout(verifyEvaluation, 600);
300           }
301         });
302 });
303
304 if ( !isLocal ) {
305
306 test("$.getJSON(String, Hash, Function) - JSON array", function() {
307         expect(4);
308         stop();
309         $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
310           ok( json[0].name == 'John', 'Check JSON: first, name' );
311           ok( json[0].age == 21, 'Check JSON: first, age' );
312           ok( json[1].name == 'Peter', 'Check JSON: second, name' );
313           ok( json[1].age == 25, 'Check JSON: second, age' );
314           start();
315         });
316 });
317
318 test("$.getJSON(String, Function) - JSON object", function() {
319         expect(2);
320         stop();
321         $.getJSON(url("data/json.php"), function(json) {
322           ok( json.data.lang == 'en', 'Check JSON: lang' );
323           ok( json.data.length == 25, 'Check JSON: length' );
324           start();
325         });
326 });
327
328 test("$.post(String, Hash, Function) - simple with xml", function() {
329         expect(2);
330         stop();
331         $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
332           $('math', xml).each(function() {
333                     ok( $('calculation', this).text() == '5-2', 'Check for XML' );
334                     ok( $('result', this).text() == '3', 'Check for XML' );
335                  });
336           start();
337         });
338 });
339
340 test("$.ajaxTimeout(Number) - with global timeout", function() {
341         stop();
342         
343         var passed = 0;
344
345         $.ajaxTimeout(1000);
346         
347         var pass = function() {
348                 passed++;
349                 if ( passed == 2 ) {
350                         ok( true, 'Check local and global callbacks after timeout' );
351                 $('#main').unbind("ajaxError");
352                         start();
353                 }
354         };
355         
356         var fail = function(a,b,c) {
357                 ok( false, 'Check for timeout failed ' + a + ' ' + b );
358                 start();
359         };
360         
361         $('#main').ajaxError(pass);
362         
363         $.ajax({
364           type: "GET",
365           url: url("data/name.php?wait=5"),
366           error: pass,
367           success: fail
368         });
369         
370         // reset timeout
371         $.ajaxTimeout(0);
372 });
373
374 test("$.ajaxTimeout(Number) with localtimeout", function() {
375         stop(); $.ajaxTimeout(50);
376         $.ajax({
377           type: "GET",
378           timeout: 5000,
379           url: url("data/name.php?wait=1"),
380           error: function() {
381                    ok( false, 'Check for local timeout failed' );
382                    start();
383           },
384           success: function() {
385             ok( true, 'Check for local timeout' );
386             start();
387           }
388         });
389         // reset timeout
390         $.ajaxTimeout(0);
391 });
392
393 test("$.ajax - simple get", function() {
394         expect(1);
395         stop();
396         $.ajax({
397           type: "GET",
398           url: url("data/name.php?name=foo"),
399           success: function(msg){
400             ok( msg == 'bar', 'Check for GET' );
401             start();
402           }
403         });
404 });
405
406 test("$.ajax - simple post", function() {
407         expect(1);
408         stop();
409         $.ajax({
410           type: "POST",
411           url: url("data/name.php"),
412           data: "name=peter",
413           success: function(msg){
414             ok( msg == 'pan', 'Check for POST' );
415             start();
416           }
417         });
418 });
419
420 test("ajaxSetup()", function() {
421         expect(1);
422         stop();
423         $.ajaxSetup({
424                 url: url("data/name.php?name=foo"),
425                 success: function(msg){
426                 ok( msg == 'bar', 'Check for GET' );
427                         start();
428                 }
429         });
430         $.ajax();
431 });
432
433 test("custom timeout does not set error message when timeout occurs, see #970", function() {
434         stop();
435         $.ajax({
436                 url: "data/name.php?wait=10",
437                 timeout: 500,
438                 error: function(request, status) {
439                         ok( status != null, "status shouldn't be null in error handler" );
440                         equals( "timeout", status );
441                         start();
442                 }
443         });
444 });
445
446 }
447
448 }