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