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