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