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() {
49 $.ajax({url: "data/json.php", async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
50 ok( /^{ "data"/.test( result ), "check returned text" );
53 test("load(String, Object, Function) - simple: inject text into DOM", function() {
56 $('#first').load("data/name.php", function() {
57 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
62 test("load(String, Object, Function) - inject without callback", function() {
64 stop(true); // check if load can be called with only url
65 $('#first').load("data/name.php");
68 test("load(String, Object, Function) - check scripts", function() {
72 var verifyEvaluation = function() {
73 ok( foobar == "bar", 'Check if script src was evaluated after load' );
74 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
75 ok( $('#ap').html() == 'bar', 'Check if script evaluation has modified DOM');
78 $('#first').load('data/test.html', function() {
79 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
80 ok( testFoo == "foo", 'Check if script was evaluated after load' );
81 setTimeout(verifyEvaluation, 600);
85 test("test global handlers - success", function() {
88 var counter = { complete: 0, success: 0, error: 0, send: 0 },
89 success = function() { counter.success++ },
90 error = function() { counter.error++ },
91 complete = function() { counter.complete++ },
92 send = function() { counter.send++ };
94 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
95 // start with successful test
96 $.ajax({url: "data/name.php", beforeSend: send, success: success, error: error, complete: function() {
97 ok( counter.error == 0, 'Check succesful request' );
98 ok( counter.success == 2, 'Check succesful request' );
99 ok( counter.complete == 3, 'Check succesful request' );
100 ok( counter.send == 2, 'Check succesful request' );
101 counter.error = counter.success = counter.complete = counter.send = 0;
103 $.ajax({url: "data/name.php?wait=5", beforeSend: send, success: success, error: error, complete: function() {
104 ok( counter.error == 2, 'Check failed request' );
105 ok( counter.success == 0, 'Check failed request' );
106 ok( counter.complete == 3, 'Check failed request' );
107 ok( counter.send == 2, 'Check failed request' );
113 test("test global handlers - failure", 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 $.ajax({url: "data/name.php", global: false, beforeSend: send, success: success, error: error, complete: function() {
124 ok( counter.error == 0, 'Check sucesful request without globals' );
125 ok( counter.success == 1, 'Check sucesful request without globals' );
126 ok( counter.complete == 0, 'Check sucesful request without globals' );
127 ok( counter.send == 1, 'Check sucesful request without globals' );
128 counter.error = counter.success = counter.complete = counter.send = 0;
130 $.ajax({url: "data/name.php?wait=5", global: false, beforeSend: send, success: success, error: error, complete: function() {
132 ok( counter.error == 1, 'Check failed request without globals' );
133 ok( counter.success == 0, 'Check failed request without globals' );
134 ok( counter.complete == 0, 'Check failed request without globals' );
135 ok( counter.send == 1, 'Check failed request without globals' );
141 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
144 $.get('data/dashboard.xml', function(xml) {
146 $('tab', xml).each(function() {
147 content.push($(this).text());
149 ok( content[0] == 'blabla', 'Check first tab');
150 ok( content[1] == 'blublu', 'Check second tab');
155 test("$.getIfModified(String, Hash, Function)", function() {
158 $.getIfModified("data/name.php", function(msg) {
159 ok( /^ERROR/.test(msg), 'Check ifModified' );
164 test("$.getScript(String, Function) - with callback", function() {
167 $.getScript("data/test.js", function() {
168 ok( foobar == "bar", 'Check if script was evaluated' );
169 setTimeout(start, 100);
173 test("$.getScript(String, Function) - no callback", function() {
176 $.getScript("data/test.js");
179 test("$.getJSON(String, Hash, Function) - JSON array", function() {
182 $.getJSON("data/json.php", {json: "array"}, function(json) {
183 ok( json[0].name == 'John', 'Check JSON: first, name' );
184 ok( json[0].age == 21, 'Check JSON: first, age' );
185 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
186 ok( json[1].age == 25, 'Check JSON: second, age' );
191 test("$.getJSON(String, Hash, Function) - JSON object", function() {
194 $.getJSON("data/json.php", function(json) {
195 ok( json.data.lang == 'en', 'Check JSON: lang' );
196 ok( json.data.length == 25, 'Check JSON: length' );
201 test("$.post(String, Hash, Function) - simple with xml", function() {
204 $.post("data/name.php", {xml: "5-2"}, function(xml){
205 $('math', xml).each(function() {
206 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
207 ok( $('result', this).text() == '3', 'Check for XML' );
213 test("$.ajaxTimeout(Number) - with global timeout", function() {
218 var pass = function() {
221 ok( true, 'Check local and global callbacks after timeout' );
222 clearTimeout(timeout);
223 $('#main').unbind("ajaxError");
227 var fail = function() {
228 ok( false, 'Check for timeout failed' );
231 timeout = setTimeout(fail, 1500);
232 $('#main').ajaxError(pass);
235 url: "data/name.php?wait=5",
243 test("$.ajaxTimeout(Number) with localtimeout", function() {
244 stop(); $.ajaxTimeout(50);
248 url: "data/name.php?wait=1",
250 ok( false, 'Check for local timeout failed' );
253 success: function() {
254 ok( true, 'Check for local timeout' );
262 test("$.ajax - simple get", function() {
267 url: "data/name.php?name=foo",
268 success: function(msg){
269 ok( msg == 'bar', 'Check for GET' );
275 test("$.ajax - simple post", function() {
280 url: "data/name.php",
282 success: function(msg){
283 ok( msg == 'pan', 'Check for POST' );
289 test("$.ajax - dataType html", function() {
293 var verifyEvaluation = function() {
294 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
299 url: "data/test.html",
300 success: function(data) {
301 ok( data.match(/^html text/), 'Check content for datatype html' );
302 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
303 setTimeout(verifyEvaluation, 600);
308 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
312 url: "data/with_fries.xml", dataType: "xml", type: "GET", data: "", success: function(resp) {
313 ok( $("properties", resp).length == 1, 'properties in responseXML' );
314 ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
315 ok( $("thing", resp).length == 2, 'things in responseXML' );
321 test("$.ajax - beforeSend", function() {
326 url: "data/name.php",
328 beforeSend: function(xml) {
331 success: function(data) {
332 ok( check, "check beforeSend was executed" );
338 test("ajaxSetup()", function() {
342 url: "data/name.php?name=foo",
343 success: function(msg){
344 ok( msg == 'bar', 'Check for GET' );