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