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