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("load(String, Object, Function) - simple: inject text into DOM", function() {
28 $('#first').load("data/name.php", function() {
29 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
34 test("load(String, Object, Function) - inject without callback", function() {
36 stop(); // check if load can be called with only url
37 $('#first').load("data/name.php");
40 test("load(String, Object, Function) - check scripts", function() {
44 var verifyEvaluation = function() {
45 ok( foobar == "bar", 'Check if script src was evaluated after load' );
46 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
47 ok( $('#ap').html() == 'bar', 'Check if script evaluation has modified DOM');
50 $('#first').load('data/test.html', function() {
51 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
52 ok( testFoo == "foo", 'Check if script was evaluated after load' );
53 setTimeout(verifyEvaluation, 600);
57 test("test global handlers - success", function() {
60 var counter = { complete: 0, success: 0, error: 0 },
61 success = function() { counter.success++ },
62 error = function() { counter.error++ },
63 complete = function() { counter.complete++ };
65 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
66 // start with successful test
67 $.ajax({url: "data/name.php", success: success, error: error, complete: function() {
68 ok( counter.error == 0, 'Check succesful request' );
69 ok( counter.success == 2, 'Check succesful request' );
70 ok( counter.complete == 3, 'Check succesful request' );
71 counter.error = counter.success = counter.complete = 0;
73 $.ajax({url: "data/name.php?wait=5", success: success, error: error, complete: function() {
74 ok( counter.error == 2, 'Check failed request' );
75 ok( counter.success == 0, 'Check failed request' );
76 ok( counter.complete == 3, 'Check failed request' );
82 test("test global handlers - failure", function() {
85 var counter = { complete: 0, success: 0, error: 0 },
86 success = function() { counter.success++ },
87 error = function() { counter.error++ };
89 $.ajax({url: "data/name.php", global: false, success: success, error: error, complete: function() {
90 ok( counter.error == 0, 'Check sucesful request without globals' );
91 ok( counter.success == 1, 'Check sucesful request without globals' );
92 ok( counter.complete == 0, 'Check sucesful request without globals' );
93 counter.error = counter.success = counter.complete = 0;
95 $.ajax({url: "data/name.php?wait=5", global: false, success: success, error: error, complete: function() {
96 ok( counter.error == 1, 'Check failed request without globals' );
97 ok( counter.success == 0, 'Check failed request without globals' );
98 ok( counter.complete == 0, 'Check failed request without globals' );
104 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
107 $.get('data/dashboard.xml', function(xml) {
109 $('tab', xml).each(function() {
110 content.push($(this).text());
112 ok( content[0] == 'blabla', 'Check first tab');
113 ok( content[1] == 'blublu', 'Check second tab');
118 test("$.getIfModified(String, Hash, Function)", function() {
121 $.getIfModified("data/name.php", function(msg) {
122 ok( /^ERROR/.test(msg), 'Check ifModified' );
127 test("$.getScript(String, Function) - with callback", function() {
130 $.getScript("data/test.js", function() {
131 ok( foobar == "bar", 'Check if script was evaluated' );
132 setTimeout(start, 100);
136 test("$.getScript(String, Function) - no callback", function() {
139 $.getScript("data/test.js");
142 test("$.getJSON(String, Hash, Function) - JSON array", function() {
145 $.getJSON("data/json.php", {json: "array"}, function(json) {
146 ok( json[0].name == 'John', 'Check JSON: first, name' );
147 ok( json[0].age == 21, 'Check JSON: first, age' );
148 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
149 ok( json[1].age == 25, 'Check JSON: second, age' );
154 test("$.getJSON(String, Hash, Function) - JSON object", function() {
157 $.getJSON("data/json.php", function(json) {
158 ok( json.data.lang == 'en', 'Check JSON: lang' );
159 ok( json.data.length == 25, 'Check JSON: length' );
164 test("$.post(String, Hash, Function) - simple with xml", function() {
167 $.post("data/name.php", {xml: "5-2"}, function(xml){
168 $('math', xml).each(function() {
169 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
170 ok( $('result', this).text() == '3', 'Check for XML' );
176 test("$.ajaxTimeout(Number) - with global timeout", function() {
181 var pass = function() {
184 ok( true, 'Check local and global callbacks after timeout' );
185 clearTimeout(timeout);
186 $('#main').unbind("ajaxError");
190 var fail = function() {
191 ok( false, 'Check for timeout failed' );
194 timeout = setTimeout(fail, 1500);
195 $('#main').ajaxError(pass);
198 url: "data/name.php?wait=5",
204 test("$.ajaxTimeout(Number) with localtimeout", function() {
205 stop(); $.ajaxTimeout(50);
209 url: "data/name.php?wait=1",
211 ok( false, 'Check for local timeout failed' );
214 success: function() {
215 ok( true, 'Check for local timeout' );
223 test("$.ajax - simple get", function() {
228 url: "data/name.php?name=foo",
229 success: function(msg){
230 ok( msg == 'bar', 'Check for GET' );
236 test("$.ajax - simple post", function() {
241 url: "data/name.php",
243 success: function(msg){
244 ok( msg == 'pan', 'Check for POST' );
250 test("$.ajax - dataType html", function() {
254 var verifyEvaluation = function() {
255 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
260 url: "data/test.html",
261 success: function(data) {
262 ok( data.match(/^html text/), 'Check content for datatype html' );
263 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
264 setTimeout(verifyEvaluation, 600);
269 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
273 url: "data/with_fries.xml", dataType: "xml", type: "GET", data: "", success: function(resp) {
274 ok( $("properties", resp).length == 1, 'properties in responseXML' );
275 ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
276 ok( $("thing", resp).length == 2, 'things in responseXML' );
282 test("$.ajax - preprocess", function() {
285 var customHeader = "value";
287 url: "data/name.php",
289 beforeSend: function(xml) {
290 xml.setRequestHeader('X-Custom-Header', customHeader);
292 success: function(data) {
293 ok( data == customHeader, "check return value, should be the custom header sent" );