3 test("serialize()", function() {
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%5Bbar%5D=&name=name&=foobar&select1=&select2=3&select3=1', 'Check form serialization as query string' );
10 test("param", function() {
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" );
15 params = {someName: [1, 2, 3], regularThing: "blah" };
16 ok( $.param(params) == "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
18 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
19 ok( $.param(params) == "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All%20your%20base%20are%20belong%20to%20us", "more array" );
21 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
22 ok( $.param(params) == "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
25 test("evalScripts() with no script elements", function() {
28 var data = "this is just some bogus text";
30 ok ( true, 'before evalScripts()');
32 $('#foo').evalScripts();
34 ok (false, 'exception evaluating scripts: ' + e.message);
36 ok ( true, 'after evalScripts()');
39 test("synchronous request", function() {
40 ok( /^{ "data"/.test( $.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
43 test("synchronous request with callbacks", function() {
46 $.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
47 ok( /^{ "data"/.test( result ), "check returned text" );
50 test("pass-through request object", function() {
54 var success = function() {
58 var target = "data/name.html";
59 ok( $.get(url(target), success), "get" );
60 ok( $.getIfModified(url(target), success), "getIfModified" );
61 ok( $.post(url(target), success), "post" );
62 ok( $.getScript(url("data/test.js"), success), "script" );
63 ok( $.getJSON(url("data/json_obj.js"), success), "json" );
64 ok( $.ajax({url: url(target), success: success}), "generic" );
67 test("load(String, Object, Function) - simple: inject text into DOM", function() {
70 $('#first').load(url("data/name.html"), function() {
71 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
76 test("load(String, Object, Function) - inject without callback", function() {
78 stop(true); // check if load can be called with only url
79 $('#first').load("data/name.html");
82 if ( location.protocol != "file:" ) {
84 test("load(String, Object, Function) - check scripts", function() {
87 window.testFoo = undefined;
89 var verifyEvaluation = function() {
90 ok( foobar == "bar", 'Check if script src was evaluated after load' );
91 ok( $('#ap').html() == 'bar', 'Check if script evaluation has modified DOM');
94 $('#first').load(url('data/test.php'), function() {
95 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
96 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
97 ok( testFoo == "foo", 'Check if script was evaluated after load' );
98 setTimeout(verifyEvaluation, 600);
102 test("load(String, Object, Function) - check file with only a script tag", function() {
106 $('#first').load(url('data/test2.php'), function() {
107 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
108 ok( testFoo == "foo", 'Check if script was evaluated after load' );
113 test("test global handlers - success", function() {
116 var counter = { complete: 0, success: 0, error: 0, send: 0 },
117 success = function() { counter.success++ },
118 error = function() { counter.error++ },
119 complete = function() { counter.complete++ },
120 send = function() { counter.send++ };
122 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
123 // start with successful test
124 $.ajax({url: url("data/name.php"), beforeSend: send, success: success, error: error, complete: function() {
125 ok( counter.error == 0, 'Check succesful request' );
126 ok( counter.success == 2, 'Check succesful request' );
127 ok( counter.complete == 3, 'Check succesful request' );
128 ok( counter.send == 2, 'Check succesful request' );
129 counter.error = counter.success = counter.complete = counter.send = 0;
131 $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() {
132 ok( counter.error == 2, 'Check failed request' );
133 ok( counter.success == 0, 'Check failed request' );
134 ok( counter.complete == 3, 'Check failed request' );
135 ok( counter.send == 2, 'Check failed request' );
141 test("test global handlers - failure", function() {
144 var counter = { complete: 0, success: 0, error: 0, send: 0 },
145 success = function() { counter.success++ },
146 error = function() { counter.error++ },
147 complete = function() { counter.complete++ },
148 send = function() { counter.send++ };
150 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
151 $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() {
152 ok( counter.error == 0, 'Check sucesful request without globals' );
153 ok( counter.success == 1, 'Check sucesful request without globals' );
154 ok( counter.complete == 0, 'Check sucesful request without globals' );
155 ok( counter.send == 1, 'Check sucesful request without globals' );
156 counter.error = counter.success = counter.complete = counter.send = 0;
158 $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
160 ok( counter.error == 1, 'Check failed request without globals' );
161 ok( counter.success == 0, 'Check failed request without globals' );
162 ok( counter.complete == 0, 'Check failed request without globals' );
163 ok( counter.send == 1, 'Check failed request without globals' );
169 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
172 $.get(url('data/dashboard.xml'), function(xml) {
174 $('tab', xml).each(function() {
175 content.push($(this).text());
177 ok( content[0] == 'blabla', 'Check first tab');
178 ok( content[1] == 'blublu', 'Check second tab');
183 test("$.getIfModified(String, Hash, Function)", function() {
186 $.getIfModified(url("data/name.php"), function(msg) {
187 ok( /^ERROR/.test(msg), 'Check ifModified' );
192 test("$.getScript(String, Function) - with callback", function() {
195 $.getScript(url("data/test.js"), function() {
196 ok( foobar == "bar", 'Check if script was evaluated' );
197 setTimeout(start, 100);
201 test("$.getScript(String, Function) - no callback", function() {
204 $.getScript(url("data/test.js"));
207 test("$.getJSON(String, Hash, Function) - JSON array", function() {
210 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
211 ok( json[0].name == 'John', 'Check JSON: first, name' );
212 ok( json[0].age == 21, 'Check JSON: first, age' );
213 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
214 ok( json[1].age == 25, 'Check JSON: second, age' );
219 test("$.getJSON(String, Hash, Function) - JSON object", function() {
222 $.getJSON(url("data/json.php"), function(json) {
223 ok( json.data.lang == 'en', 'Check JSON: lang' );
224 ok( json.data.length == 25, 'Check JSON: length' );
229 test("$.post(String, Hash, Function) - simple with xml", function() {
232 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
233 $('math', xml).each(function() {
234 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
235 ok( $('result', this).text() == '3', 'Check for XML' );
241 test("$.ajaxTimeout(Number) - with global timeout", function() {
246 var pass = function() {
249 ok( true, 'Check local and global callbacks after timeout' );
250 clearTimeout(timeout);
251 $('#main').unbind("ajaxError");
255 var fail = function() {
256 ok( false, 'Check for timeout failed' );
259 timeout = setTimeout(fail, 1500);
260 $('#main').ajaxError(pass);
263 url: url("data/name.php?wait=5"),
271 test("$.ajaxTimeout(Number) with localtimeout", function() {
272 stop(); $.ajaxTimeout(50);
276 url: url("data/name.php?wait=1"),
278 ok( false, 'Check for local timeout failed' );
281 success: function() {
282 ok( true, 'Check for local timeout' );
290 test("$.ajax - simple get", function() {
295 url: url("data/name.php?name=foo"),
296 success: function(msg){
297 ok( msg == 'bar', 'Check for GET' );
303 test("$.ajax - simple post", function() {
308 url: url("data/name.php"),
310 success: function(msg){
311 ok( msg == 'pan', 'Check for POST' );
317 test("$.ajax - dataType html", function() {
322 var verifyEvaluation = function() {
323 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
328 url: url("data/test.php"),
329 success: function(data) {
330 ok( data.match(/^html text/), 'Check content for datatype html' );
331 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
332 setTimeout(verifyEvaluation, 600);
337 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
341 url: url("data/with_fries.xml"),
343 success: function(resp) {
344 ok( $("properties", resp).length == 1, 'properties in responseXML' );
345 ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
346 ok( $("thing", resp).length == 2, 'things in responseXML' );
352 test("$.ajax - beforeSend", function() {
357 url: url("data/name.php"),
359 beforeSend: function(xml) {
362 success: function(data) {
363 ok( check, "check beforeSend was executed" );
369 test("ajaxSetup()", function() {
373 url: url("data/name.php?name=foo"),
374 success: function(msg){
375 ok( msg == 'bar', 'Check for GET' );
382 test("custom timeout does not set error message when timeout occurs, see #970", function() {
385 url: "data/name.php?wait=10",
387 error: function(request, status) {
388 ok( status != null, "status shouldn't be null in error handler" );
389 equals( "timeout", status );