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[bar]=&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[]=baz&foo[]=42&foo[]=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[bar]=baz&foo[beep]=42&foo[quux]=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
25 test("pass-through request object", function() {
29 var success = function() {
33 var url = "data/name.php";
34 ok( $.get(url, success), "get" );
35 ok( $.getIfModified(url, success), "getIfModified" );
36 ok( $.post(url, success), "post" );
37 ok( $.getScript("data/test.js", success), "script" );
38 ok( $.getJSON("data/json.php", success), "json" );
39 ok( $.ajax({url: url, success: success}), "generic" );
42 test("synchronous request", function() {
43 ok( /^{ "data"/.test( $.ajax({url: "data/json.php", async: false}).responseText ), "check returned text" );
46 test("synchronous request with callbacks", function() {
48 $.ajax({url: "data/json.php", async: false, success: function(data) { result = data; } });
49 ok( /^{ "data"/.test( result ), "check returned text" );
52 test("load(String, Object, Function) - simple: inject text into DOM", function() {
55 $('#first').load("data/name.php", function() {
56 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
61 test("load(String, Object, Function) - inject without callback", function() {
63 stop(true); // check if load can be called with only url
64 $('#first').load("data/name.php");
67 test("load(String, Object, Function) - check scripts", function() {
71 var verifyEvaluation = function() {
72 ok( foobar == "bar", 'Check if script src was evaluated after load' );
73 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
74 ok( $('#ap').html() == 'bar', 'Check if script evaluation has modified DOM');
77 $('#first').load('data/test.html', function() {
78 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
79 ok( testFoo == "foo", 'Check if script was evaluated after load' );
80 setTimeout(verifyEvaluation, 600);
84 test("test global handlers - success", function() {
87 var counter = { complete: 0, success: 0, error: 0, send: 0 },
88 success = function() { counter.success++ },
89 error = function() { counter.error++ },
90 complete = function() { counter.complete++ },
91 send = function() { counter.send++ };
93 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
94 // start with successful test
95 $.ajax({url: "data/name.php", beforeSend: send, success: success, error: error, complete: function() {
96 ok( counter.error == 0, 'Check succesful request' );
97 ok( counter.success == 2, 'Check succesful request' );
98 ok( counter.complete == 3, 'Check succesful request' );
99 ok( counter.send == 2, 'Check succesful request' );
100 counter.error = counter.success = counter.complete = counter.send = 0;
102 $.ajax({url: "data/name.php?wait=5", beforeSend: send, success: success, error: error, complete: function() {
103 ok( counter.error == 2, 'Check failed request' );
104 ok( counter.success == 0, 'Check failed request' );
105 ok( counter.complete == 3, 'Check failed request' );
106 ok( counter.send == 2, 'Check failed request' );
112 test("test global handlers - failure", function() {
115 var counter = { complete: 0, success: 0, error: 0, send: 0 },
116 success = function() { counter.success++ },
117 error = function() { counter.error++ },
118 complete = function() { counter.complete++ },
119 send = function() { counter.send++ };
121 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
122 $.ajax({url: "data/name.php", global: false, beforeSend: send, success: success, error: error, complete: function() {
123 ok( counter.error == 0, 'Check sucesful request without globals' );
124 ok( counter.success == 1, 'Check sucesful request without globals' );
125 ok( counter.complete == 0, 'Check sucesful request without globals' );
126 ok( counter.send == 1, 'Check sucesful request without globals' );
127 counter.error = counter.success = counter.complete = counter.send = 0;
129 $.ajax({url: "data/name.php?wait=5", global: false, beforeSend: send, success: success, error: error, complete: function() {
131 ok( counter.error == 1, 'Check failed request without globals' );
132 ok( counter.success == 0, 'Check failed request without globals' );
133 ok( counter.complete == 0, 'Check failed request without globals' );
134 ok( counter.send == 1, 'Check failed request without globals' );
140 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
143 $.get('data/dashboard.xml', function(xml) {
145 $('tab', xml).each(function() {
146 content.push($(this).text());
148 ok( content[0] == 'blabla', 'Check first tab');
149 ok( content[1] == 'blublu', 'Check second tab');
154 test("$.getIfModified(String, Hash, Function)", function() {
157 $.getIfModified("data/name.php", function(msg) {
158 ok( /^ERROR/.test(msg), 'Check ifModified' );
163 test("$.getScript(String, Function) - with callback", function() {
166 $.getScript("data/test.js", function() {
167 ok( foobar == "bar", 'Check if script was evaluated' );
168 setTimeout(start, 100);
172 test("$.getScript(String, Function) - no callback", function() {
175 $.getScript("data/test.js");
178 test("$.getJSON(String, Hash, Function) - JSON array", function() {
181 $.getJSON("data/json.php", {json: "array"}, function(json) {
182 ok( json[0].name == 'John', 'Check JSON: first, name' );
183 ok( json[0].age == 21, 'Check JSON: first, age' );
184 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
185 ok( json[1].age == 25, 'Check JSON: second, age' );
190 test("$.getJSON(String, Hash, Function) - JSON object", function() {
193 $.getJSON("data/json.php", function(json) {
194 ok( json.data.lang == 'en', 'Check JSON: lang' );
195 ok( json.data.length == 25, 'Check JSON: length' );
200 test("$.post(String, Hash, Function) - simple with xml", function() {
203 $.post("data/name.php", {xml: "5-2"}, function(xml){
204 $('math', xml).each(function() {
205 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
206 ok( $('result', this).text() == '3', 'Check for XML' );
212 test("$.ajaxTimeout(Number) - with global timeout", function() {
217 var pass = function() {
220 ok( true, 'Check local and global callbacks after timeout' );
221 clearTimeout(timeout);
222 $('#main').unbind("ajaxError");
226 var fail = function() {
227 ok( false, 'Check for timeout failed' );
230 timeout = setTimeout(fail, 1500);
231 $('#main').ajaxError(pass);
234 url: "data/name.php?wait=5",
242 test("$.ajaxTimeout(Number) with localtimeout", function() {
243 stop(); $.ajaxTimeout(50);
247 url: "data/name.php?wait=1",
249 ok( false, 'Check for local timeout failed' );
252 success: function() {
253 ok( true, 'Check for local timeout' );
261 test("$.ajax - simple get", function() {
266 url: "data/name.php?name=foo",
267 success: function(msg){
268 ok( msg == 'bar', 'Check for GET' );
274 test("$.ajax - simple post", function() {
279 url: "data/name.php",
281 success: function(msg){
282 ok( msg == 'pan', 'Check for POST' );
288 test("$.ajax - dataType html", function() {
292 var verifyEvaluation = function() {
293 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
298 url: "data/test.html",
299 success: function(data) {
300 ok( data.match(/^html text/), 'Check content for datatype html' );
301 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
302 setTimeout(verifyEvaluation, 600);
307 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
311 url: "data/with_fries.xml", dataType: "xml", type: "GET", data: "", success: function(resp) {
312 ok( $("properties", resp).length == 1, 'properties in responseXML' );
313 ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
314 ok( $("thing", resp).length == 2, 'things in responseXML' );
320 test("$.ajax - beforeSend", function() {
325 url: "data/name.php",
327 beforeSend: function(xml) {
330 success: function(data) {
331 ok( check, "check beforeSend was executed" );
337 test("ajaxSetup()", function() {
341 url: "data/name.php?name=foo",
342 success: function(msg){
343 ok( msg == 'bar', 'Check for GET' );