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