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