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, send: 0 },
61 success = function() { counter.success++ },
62 error = function() { counter.error++ },
63 complete = function() { counter.complete++ },
64 send = function() { counter.send++ };
66 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
67 // start with successful test
68 $.ajax({url: "data/name.php", beforeSend: send, success: success, error: error, complete: function() {
69 ok( counter.error == 0, 'Check succesful request' );
70 ok( counter.success == 2, 'Check succesful request' );
71 ok( counter.complete == 3, 'Check succesful request' );
72 ok( counter.send == 2, 'Check succesful request' );
73 counter.error = counter.success = counter.complete = counter.send = 0;
75 $.ajax({url: "data/name.php?wait=5", beforeSend: send, success: success, error: error, complete: function() {
76 ok( counter.error == 2, 'Check failed request' );
77 ok( counter.success == 0, 'Check failed request' );
78 ok( counter.complete == 3, 'Check failed request' );
79 ok( counter.send == 2, 'Check failed request' );
85 test("test global handlers - failure", 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 $.ajax({url: "data/name.php", global: false, beforeSend: send, success: success, error: error, complete: function() {
96 ok( counter.error == 0, 'Check sucesful request without globals' );
97 ok( counter.success == 1, 'Check sucesful request without globals' );
98 ok( counter.complete == 0, 'Check sucesful request without globals' );
99 ok( counter.send == 1, 'Check sucesful request without globals' );
100 counter.error = counter.success = counter.complete = counter.send = 0;
102 $.ajax({url: "data/name.php?wait=5", global: false, beforeSend: send, success: success, error: error, complete: function() {
103 ok( counter.error == 1, 'Check failed request without globals' );
104 ok( counter.success == 0, 'Check failed request without globals' );
105 ok( counter.complete == 0, 'Check failed request without globals' );
106 ok( counter.send == 1, 'Check failed request without globals' );
112 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
115 $.get('data/dashboard.xml', function(xml) {
117 $('tab', xml).each(function() {
118 content.push($(this).text());
120 ok( content[0] == 'blabla', 'Check first tab');
121 ok( content[1] == 'blublu', 'Check second tab');
126 test("$.getIfModified(String, Hash, Function)", function() {
129 $.getIfModified("data/name.php", function(msg) {
130 ok( /^ERROR/.test(msg), 'Check ifModified' );
135 test("$.getScript(String, Function) - with callback", function() {
138 $.getScript("data/test.js", function() {
139 ok( foobar == "bar", 'Check if script was evaluated' );
140 setTimeout(start, 100);
144 test("$.getScript(String, Function) - no callback", function() {
147 $.getScript("data/test.js");
150 test("$.getJSON(String, Hash, Function) - JSON array", function() {
153 $.getJSON("data/json.php", {json: "array"}, function(json) {
154 ok( json[0].name == 'John', 'Check JSON: first, name' );
155 ok( json[0].age == 21, 'Check JSON: first, age' );
156 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
157 ok( json[1].age == 25, 'Check JSON: second, age' );
162 test("$.getJSON(String, Hash, Function) - JSON object", function() {
165 $.getJSON("data/json.php", function(json) {
166 ok( json.data.lang == 'en', 'Check JSON: lang' );
167 ok( json.data.length == 25, 'Check JSON: length' );
172 test("$.post(String, Hash, Function) - simple with xml", function() {
175 $.post("data/name.php", {xml: "5-2"}, function(xml){
176 $('math', xml).each(function() {
177 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
178 ok( $('result', this).text() == '3', 'Check for XML' );
184 test("$.ajaxTimeout(Number) - with global timeout", function() {
189 var pass = function() {
192 ok( true, 'Check local and global callbacks after timeout' );
193 clearTimeout(timeout);
194 $('#main').unbind("ajaxError");
198 var fail = function() {
199 ok( false, 'Check for timeout failed' );
202 timeout = setTimeout(fail, 1500);
203 $('#main').ajaxError(pass);
206 url: "data/name.php?wait=5",
212 test("$.ajaxTimeout(Number) with localtimeout", function() {
213 stop(); $.ajaxTimeout(50);
217 url: "data/name.php?wait=1",
219 ok( false, 'Check for local timeout failed' );
222 success: function() {
223 ok( true, 'Check for local timeout' );
231 test("$.ajax - simple get", function() {
236 url: "data/name.php?name=foo",
237 success: function(msg){
238 ok( msg == 'bar', 'Check for GET' );
244 test("$.ajax - simple post", function() {
249 url: "data/name.php",
251 success: function(msg){
252 ok( msg == 'pan', 'Check for POST' );
258 test("$.ajax - dataType html", function() {
262 var verifyEvaluation = function() {
263 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
268 url: "data/test.html",
269 success: function(data) {
270 ok( data.match(/^html text/), 'Check content for datatype html' );
271 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
272 setTimeout(verifyEvaluation, 600);
277 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
281 url: "data/with_fries.xml", dataType: "xml", type: "GET", data: "", success: function(resp) {
282 ok( $("properties", resp).length == 1, 'properties in responseXML' );
283 ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
284 ok( $("thing", resp).length == 2, 'things in responseXML' );
290 test("$.ajax - beforeSend", function() {
293 var customHeader = "value";
295 url: "data/name.php",
297 beforeSend: function(xml) {
298 xml.setRequestHeader('X-Custom-Header', customHeader);
300 success: function(data) {
301 ok( data == customHeader, "check return value, should be the custom header sent" );