3 if ( location.protocol != "file:" ) {
5 test("serialize()", function() {
7 var data = $(':input').not('button').serialize();
8 // ignore button, IE takes text content as value, not relevant for this test
9 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' );
12 test("param", function() {
14 var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
15 ok( $.param(params) == "foo=bar&baz=42&quux=All%20your%20base%20are%20belong%20to%20us", "simple" );
17 params = {someName: [1, 2, 3], regularThing: "blah" };
18 ok( $.param(params) == "someName=1&someName=2&someName=3®ularThing=blah", "with array" );
20 params = {"foo[]":["baz", 42, "All your base are belong to us"]};
21 ok( $.param(params) == "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All%20your%20base%20are%20belong%20to%20us", "more array" );
23 params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
24 ok( $.param(params) == "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All%20your%20base%20are%20belong%20to%20us", "even more arrays" );
27 test("pass-through request object", function() {
31 var success = function() {
35 var target = "data/name.php";
36 ok( $.get(url(target), success), "get" );
37 ok( $.getIfModified(url(target), success), "getIfModified" );
38 ok( $.post(url(target), success), "post" );
39 ok( $.getScript(url("data/test.js"), success), "script" );
40 ok( $.getJSON(url("data/json.php"), success), "json" );
41 ok( $.ajax({url: url(target), success: success}), "generic" );
44 test("synchronous request", function() {
45 ok( /^{ "data"/.test( $.ajax({url: url("data/json.php"), async: false}).responseText ), "check returned text" );
48 test("synchronous request with callbacks", function() {
51 $.ajax({url: url("data/json.php"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
52 ok( /^{ "data"/.test( result ), "check returned text" );
55 test("load(String, Object, Function) - simple: inject text into DOM", function() {
58 $('#first').load(url("data/name.php"), function() {
59 ok( /^ERROR/.test($('#first').text()), 'Check if content was injected into the DOM' );
64 test("load(String, Object, Function) - inject without callback", function() {
66 stop(true); // check if load can be called with only url
67 $('#first').load("data/name.php");
70 test("load(String, Object, Function) - check scripts", function() {
75 var verifyEvaluation = function() {
76 ok( foobar == "bar", 'Check if script src was evaluated after load' );
77 ok( $('#ap').html() == 'bar', 'Check if script evaluation has modified DOM');
80 $('#first').load(url('data/test.php'), function() {
81 ok( $('#first').html().match(/^html text/), 'Check content after loading html' );
82 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
83 ok( testFoo == "foo", 'Check if script was evaluated after load' );
84 setTimeout(verifyEvaluation, 600);
88 test("load(String, Object, Function) - check file with only a script tag", function() {
92 $('#first').load(url('data/test2.php'), function() {
93 ok( $('#foo').html() == 'foo', 'Check if script evaluation has modified DOM');
94 ok( testFoo == "foo", 'Check if script was evaluated after load' );
99 test("test global handlers - success", function() {
102 var counter = { complete: 0, success: 0, error: 0, send: 0 },
103 success = function() { counter.success++ },
104 error = function() { counter.error++ },
105 complete = function() { counter.complete++ },
106 send = function() { counter.send++ };
108 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
109 // start with successful test
110 $.ajax({url: url("data/name.php"), beforeSend: send, success: success, error: error, complete: function() {
111 ok( counter.error == 0, 'Check succesful request' );
112 ok( counter.success == 2, 'Check succesful request' );
113 ok( counter.complete == 3, 'Check succesful request' );
114 ok( counter.send == 2, 'Check succesful request' );
115 counter.error = counter.success = counter.complete = counter.send = 0;
117 $.ajax({url: url("data/name.php?wait=5"), beforeSend: send, success: success, error: error, complete: function() {
118 ok( counter.error == 2, 'Check failed request' );
119 ok( counter.success == 0, 'Check failed request' );
120 ok( counter.complete == 3, 'Check failed request' );
121 ok( counter.send == 2, 'Check failed request' );
127 test("test global handlers - failure", function() {
130 var counter = { complete: 0, success: 0, error: 0, send: 0 },
131 success = function() { counter.success++ },
132 error = function() { counter.error++ },
133 complete = function() { counter.complete++ },
134 send = function() { counter.send++ };
136 $('#foo').ajaxStart(complete).ajaxStop(complete).ajaxSend(send).ajaxComplete(complete).ajaxError(error).ajaxSuccess(success);
137 $.ajax({url: url("data/name.php"), global: false, beforeSend: send, success: success, error: error, complete: function() {
138 ok( counter.error == 0, 'Check sucesful request without globals' );
139 ok( counter.success == 1, 'Check sucesful request without globals' );
140 ok( counter.complete == 0, 'Check sucesful request without globals' );
141 ok( counter.send == 1, 'Check sucesful request without globals' );
142 counter.error = counter.success = counter.complete = counter.send = 0;
144 $.ajax({url: url("data/name.php?wait=5"), global: false, beforeSend: send, success: success, error: error, complete: function() {
146 ok( counter.error == 1, 'Check failed request without globals' );
147 ok( counter.success == 0, 'Check failed request without globals' );
148 ok( counter.complete == 0, 'Check failed request without globals' );
149 ok( counter.send == 1, 'Check failed request without globals' );
155 test("$.get(String, Hash, Function) - parse xml and use text() on nodes", function() {
158 $.get(url('data/dashboard.xml'), function(xml) {
160 $('tab', xml).each(function() {
161 content.push($(this).text());
163 ok( content[0] == 'blabla', 'Check first tab');
164 ok( content[1] == 'blublu', 'Check second tab');
169 test("$.getIfModified(String, Hash, Function)", function() {
172 $.getIfModified(url("data/name.php"), function(msg) {
173 ok( /^ERROR/.test(msg), 'Check ifModified' );
178 test("$.getScript(String, Function) - with callback", function() {
181 $.getScript(url("data/test.js"), function() {
182 ok( foobar == "bar", 'Check if script was evaluated' );
183 setTimeout(start, 100);
187 test("$.getScript(String, Function) - no callback", function() {
190 $.getScript(url("data/test.js"));
193 test("$.getJSON(String, Hash, Function) - JSON array", function() {
196 $.getJSON(url("data/json.php"), {json: "array"}, function(json) {
197 ok( json[0].name == 'John', 'Check JSON: first, name' );
198 ok( json[0].age == 21, 'Check JSON: first, age' );
199 ok( json[1].name == 'Peter', 'Check JSON: second, name' );
200 ok( json[1].age == 25, 'Check JSON: second, age' );
205 test("$.getJSON(String, Hash, Function) - JSON object", function() {
208 $.getJSON(url("data/json.php"), function(json) {
209 ok( json.data.lang == 'en', 'Check JSON: lang' );
210 ok( json.data.length == 25, 'Check JSON: length' );
215 test("$.post(String, Hash, Function) - simple with xml", function() {
218 $.post(url("data/name.php"), {xml: "5-2"}, function(xml){
219 $('math', xml).each(function() {
220 ok( $('calculation', this).text() == '5-2', 'Check for XML' );
221 ok( $('result', this).text() == '3', 'Check for XML' );
227 test("$.ajaxTimeout(Number) - with global timeout", function() {
232 var pass = function() {
235 ok( true, 'Check local and global callbacks after timeout' );
236 clearTimeout(timeout);
237 $('#main').unbind("ajaxError");
241 var fail = function() {
242 ok( false, 'Check for timeout failed' );
245 timeout = setTimeout(fail, 1500);
246 $('#main').ajaxError(pass);
249 url: url("data/name.php?wait=5"),
257 test("$.ajaxTimeout(Number) with localtimeout", function() {
258 stop(); $.ajaxTimeout(50);
262 url: url("data/name.php?wait=1"),
264 ok( false, 'Check for local timeout failed' );
267 success: function() {
268 ok( true, 'Check for local timeout' );
276 test("$.ajax - simple get", function() {
281 url: url("data/name.php?name=foo"),
282 success: function(msg){
283 ok( msg == 'bar', 'Check for GET' );
289 test("$.ajax - simple post", function() {
294 url: url("data/name.php"),
296 success: function(msg){
297 ok( msg == 'pan', 'Check for POST' );
303 test("$.ajax - dataType html", function() {
308 var verifyEvaluation = function() {
309 ok( foobar == "bar", 'Check if script src was evaluated for datatype html' );
314 url: url("data/test.php"),
315 success: function(data) {
316 ok( data.match(/^html text/), 'Check content for datatype html' );
317 ok( testFoo == "foo", 'Check if script was evaluated for datatype html' );
318 setTimeout(verifyEvaluation, 600);
323 test("$.ajax - xml: non-namespace elements inside namespaced elements", function() {
327 url: url("data/with_fries.xml"),
329 success: function(resp) {
330 ok( $("properties", resp).length == 1, 'properties in responseXML' );
331 ok( $("jsconf", resp).length == 1, 'jsconf in responseXML' );
332 ok( $("thing", resp).length == 2, 'things in responseXML' );
338 test("$.ajax - beforeSend", function() {
343 url: url("data/name.php"),
345 beforeSend: function(xml) {
348 success: function(data) {
349 ok( check, "check beforeSend was executed" );
355 test("ajaxSetup()", function() {
359 url: url("data/name.php?name=foo"),
360 success: function(msg){
361 ok( msg == 'bar', 'Check for GET' );
368 test("evalScripts() with no script elements", function() {
371 var data = "this is just some bogus text";
372 $('#foo').html(data);
373 ok ( true, 'before evalScripts()');
375 $('#foo').evalScripts();
377 ok (false, 'exception evaluating scripts: ' + e.message);
379 ok ( true, 'after evalScripts()');