| 41 | | // test string serialization |
| 42 | | test("serialize()", function() { |
| 43 | | var s = $("#form1").serialize(); |
| 44 | | ok( s.constructor == String, "type check"); |
| 45 | | ok( s.split('&').length == 13, "string array length"); |
| 46 | | }); |
| 47 | | |
| 48 | | // test support for input elements not contained within a form |
| 49 | | test("pseudo form", function() { |
| 50 | | var s = $("#pseudo").serialize(); |
| 51 | | ok( s.constructor == String, "type check"); |
| 52 | | ok( s.split('&').length == 3, "string array length"); |
| 53 | | }); |
| 54 | | |
| 55 | | // test form with inputs named 'action' and 'method' |
| 56 | | test("form attrs", function() { |
| 57 | | var f = $("#form1"); |
| 58 | | ok( f.attr('action').match('text.php'), "form 'action'"); |
| 59 | | ok( f.attr('method') == 'post', "form 'method'"); |
| 60 | | }); |
| 61 | | |
| 62 | | // test ajaxSubmit semantic support |
| 63 | | test("semantic test", function() { |
| | 54 | // test formToArray semantic support |
| | 55 | test("formToArray: semantic test", function() { |
| | 63 | // test string serialization |
| | 64 | test("serialize: param count", function() { |
| | 65 | var s = $("#form1").serialize(); |
| | 66 | ok( s.constructor == String, "type check"); |
| | 67 | ok( s.split('&').length == 13, "string array length"); |
| | 68 | }); |
| | 69 | |
| | 70 | // test support for input elements not contained within a form |
| | 71 | test("serialize: pseudo form", function() { |
| | 72 | var s = $("#pseudo").serialize(); |
| | 73 | ok( s.constructor == String, "type check"); |
| | 74 | ok( s.split('&').length == 3, "string array length"); |
| | 75 | }); |
| | 76 | |
| 72 | | test("ajaxSubmit1", function() { |
| 73 | | $('#targetDiv').html(""); |
| 74 | | stop(); |
| 75 | | $('#form3').ajaxSubmit('#targetDiv', function() { // post-callback |
| 76 | | ok( true, 'post-callback'); |
| 77 | | ok( $('#targetDiv').text().match("Lorem ipsum"), "targetDiv updated"); |
| 78 | | start(); |
| 79 | | }); |
| 80 | | }); |
| | 78 | test("ajaxSubmit: target == String", function() { |
| | 79 | $('#targetDiv').empty(); |
| | 80 | stop(); |
| | 81 | |
| | 82 | var opts = { |
| | 83 | target: '#targetDiv', |
| | 84 | after: function() { // post-callback |
| | 85 | ok( true, 'post-callback'); |
| | 86 | ok( $('#targetDiv').text().match("Lorem ipsum"), "targetDiv updated"); |
| | 87 | start(); |
| | 88 | } |
| | 89 | }; |
| | 90 | $('#form3').ajaxSubmit(opts); |
| | 91 | }); |
| | 92 | |
| | 93 | // test passing jQuery object as the target |
| | 94 | test("ajaxSubmit: target == jQuery object", function() { |
| | 95 | stop(); |
| | 96 | var target = $('#targetDiv'); |
| | 97 | target.empty(); |
| | 98 | |
| | 99 | var opts = { |
| | 100 | target: target, |
| | 101 | after: function(responseText) { // post-callback |
| | 102 | ok( true, 'post-callback'); |
| | 103 | ok( $('#targetDiv').text().match("Lorem ipsum"), "targetDiv updated"); |
| | 104 | start(); |
| | 105 | } |
| | 106 | }; |
| | 107 | |
| | 108 | $("#form2").ajaxSubmit(opts); |
| | 109 | }); |
| | 110 | |
| | 111 | // test passing DOM element as the target |
| | 112 | test("ajaxSubmit: target == DOM element", function() { |
| | 113 | stop(); |
| | 114 | $('#targetDiv').empty(); |
| | 115 | var target = $('#targetDiv')[0]; |
| | 116 | |
| | 117 | var opts = { |
| | 118 | target: target, |
| | 119 | after: function(responseText) { // post-callback |
| | 120 | ok( true, 'post-callback'); |
| | 121 | ok( $('#targetDiv').text().match("Lorem ipsum"), "targetDiv updated"); |
| | 122 | start(); |
| | 123 | } |
| | 124 | }; |
| | 125 | |
| | 126 | $("#form2").ajaxSubmit(opts); |
| | 127 | }); |
| | 128 | |
| | 129 | |
| 83 | | test("ajaxSubmit2", function() { |
| 84 | | $('#form3').ajaxSubmit('#targetDiv', null, function(a, jq) { // pre-callback |
| 85 | | ok( true, 'pre-callback'); |
| 86 | | ok( jq.attr('action').match('text.php'), "form 'action'"); |
| 87 | | ok( jq.attr('method') == 'get', "form 'method'"); |
| 88 | | ok( a.constructor == Array, "type check array"); |
| 89 | | ok( jq.jquery, "type check jQuery"); |
| 90 | | ok( jq[0].tagName.toLowerCase() == 'form', "jQuery arg == 'form'"); |
| 91 | | }); |
| | 132 | test("ajaxSubmit: pre-submit callback", function() { |
| | 133 | var opts = { |
| | 134 | before: function(a, jq) { // pre-submit callback |
| | 135 | ok( true, 'pre-submit callback'); |
| | 136 | ok( a.constructor == Array, "type check array"); |
| | 137 | ok( jq.jquery, "type check jQuery"); |
| | 138 | ok( jq[0].tagName.toLowerCase() == 'form', "jQuery arg == 'form'"); |
| | 139 | } |
| | 140 | }; |
| | 141 | |
| | 142 | $('#form3').ajaxSubmit(opts); |
| 95 | | test("ajaxSubmit3", function() { |
| 96 | | stop(); |
| 97 | | $('#form3').ajaxSubmit(function(responseText, statusText) { // post-callback |
| 98 | | ok( true, 'post-callback'); |
| 99 | | ok( responseText.match("Lorem ipsum"), "responseText"); |
| 100 | | ok( statusText == "success", "statusText"); |
| 101 | | start(); |
| 102 | | }); |
| 103 | | }); |
| 104 | | |
| 105 | | // test semantic support and url (action) override. passing no target and |
| 106 | | // no callback will cause the response string to be evaluated. |
| 107 | | // if the override of the form's action from text.php to json.php does |
| 108 | | // not work then the response from text.php will be evaled and that will |
| 109 | | // generate an error. |
| 110 | | test("ajaxSubmit4", function() { |
| | 146 | test("ajaxSubmit: post-submit callback", function() { |
| | 147 | stop(); |
| | 148 | |
| | 149 | var opts = { |
| | 150 | after: function(responseText, statusText) { // post-submit callback |
| | 151 | ok( true, 'post-submit callback'); |
| | 152 | ok( responseText.match("Lorem ipsum"), "responseText"); |
| | 153 | ok( statusText == "success", "statusText"); |
| | 154 | start(); |
| | 155 | } |
| | 156 | }; |
| | 157 | |
| | 158 | $('#form3').ajaxSubmit(opts); |
| | 159 | }); |
| | 160 | |
| | 161 | // test semantic support via ajaxSubmit's pre-submit callback |
| | 162 | test("ajaxSubmit: semantic test", function() { |
| 113 | | $('#form2').ajaxSubmit(null, null, function(a, jq) { // pre-callback |
| 114 | | ok( true, 'pre-callback'); |
| 115 | | ok( jq.attr('action').match('text.php'), "form 'action'"); |
| 116 | | ok( jq.attr('method') == 'post', "form 'method'"); |
| 117 | | ok( a.constructor == Array, "type check"); |
| 118 | | ok( jq.jquery, "type check jQuery"); |
| 119 | | for (var i=0; i < a.length; i++) { |
| 120 | | ok(a[i].name == testData[i], "match value at index="+i); |
| 121 | | } |
| 122 | | }, |
| 123 | | 'json.php',null,true); // semantic flag == true |
| | 165 | var opts = { |
| | 166 | semantic: true, |
| | 167 | before: function(a, jq) { // pre-submit callback |
| | 168 | ok( true, 'pre-submit callback'); |
| | 169 | ok( a.constructor == Array, "type check"); |
| | 170 | ok( jq.jquery, "type check jQuery"); |
| | 171 | for (var i=0; i < a.length; i++) { |
| | 172 | ok(a[i].name == testData[i], "match value at index="+i); |
| | 173 | } |
| | 174 | } |
| | 175 | }; |
| | 176 | |
| | 177 | $('#form2').ajaxSubmit(opts); |
| | 178 | }); |
| | 179 | |
| | 180 | // test json datatype |
| | 181 | test("ajaxSubmit: dataType == json", function() { |
| | 182 | stop(); |
| | 183 | |
| | 184 | var opts = { |
| | 185 | url: 'json.txt', |
| | 186 | dataType: 'json', |
| | 187 | after: function(data, statusText) { // post-submit callback |
| | 188 | // assert that the json data was evaluated |
| | 189 | ok( typeof data == 'object', 'json data type'); |
| | 190 | ok( data.name == 'jquery-test', 'json data contents'); |
| | 191 | start(); |
| | 192 | } |
| | 193 | }; |
| | 194 | |
| | 195 | $('#form2').ajaxSubmit(opts); |
| | 196 | }); |
| | 197 | |
| | 198 | // test script datatype |
| | 199 | test("ajaxSubmit: dataType == script", function() { |
| | 200 | stop(); |
| | 201 | |
| | 202 | var opts = { |
| | 203 | url: 'script.txt?' + new Date().getTime(), // don't let ie cache it |
| | 204 | dataType: 'script', |
| | 205 | after: function(responseText, statusText) { // post-submit callback |
| | 206 | ok( typeof formScriptTest == 'function', 'script evaluated'); |
| | 207 | ok( responseText.match('formScriptTest'), 'script returned'); |
| | 208 | start(); |
| | 209 | } |
| | 210 | }; |
| | 211 | |
| | 212 | $('#form2').ajaxSubmit(opts); |
| | 213 | }); |
| | 214 | |
| | 215 | // test xml datatype |
| | 216 | test("ajaxSubmit: dataType == xml", function() { |
| | 217 | stop(); |
| | 218 | |
| | 219 | var opts = { |
| | 220 | url: 'test.xml', |
| | 221 | dataType: 'xml', |
| | 222 | after: function(responseXML, statusText) { // post-submit callback |
| | 223 | ok( typeof responseXML == 'object', 'data type xml'); |
| | 224 | ok( $('test', responseXML).size() == 3, 'xml data query'); |
| | 225 | start(); |
| | 226 | } |
| | 227 | }; |
| | 228 | |
| | 229 | $('#form2').ajaxSubmit(opts); |
| | 230 | }); |
| | 231 | |
| | 232 | // test that args embedded in the action are honored; no real way |
| | 233 | // to assert this so successful callback is used to signal success |
| | 234 | test("ajaxSubmit: existing args in action attr", function() { |
| | 235 | stop(); |
| | 236 | |
| | 237 | var opts = { |
| | 238 | after: function() { // post-submit callback |
| | 239 | ok( true, 'post callback'); |
| | 240 | start(); |
| | 241 | } |
| | 242 | }; |
| | 243 | |
| | 244 | $('#form5').ajaxSubmit(opts); |
| 127 | | test("ajaxSubmit5", function() { |
| 128 | | $('#form3').ajaxSubmit('#targetDiv', function() { // post-callback |
| 129 | | ok( false, "should not hit this post-callback"); |
| 130 | | }, |
| 131 | | function(a, jq) { // pre-callback |
| 132 | | ok( true, 'pre-callback'); |
| 133 | | ok( a.constructor == Array, "type check"); |
| 134 | | ok( jq.jquery, "type check jQuery"); |
| 135 | | return false; // return false to abort submit |
| 136 | | }); |
| | 248 | test("ajaxSubmit: cancel submit", function() { |
| | 249 | |
| | 250 | var opts = { |
| | 251 | before: function(a, jq) { // pre-submit callback |
| | 252 | ok( true, 'pre-submit callback'); |
| | 253 | ok( a.constructor == Array, "type check"); |
| | 254 | ok( jq.jquery, "type check jQuery"); |
| | 255 | return false; // return false to abort submit |
| | 256 | }, |
| | 257 | after: function() { // post-submit callback |
| | 258 | ok( false, "should not hit this post-submit callback"); |
| | 259 | } |
| | 260 | }; |
| | 261 | |
| | 262 | $('#form3').ajaxSubmit(opts); |
| 140 | | test("ajaxSubmit6", function() { |
| 141 | | stop(); |
| 142 | | $("#pseudo").ajaxSubmit('#targetDiv', function() { // post-callback |
| 143 | | ok( true, 'post-callback'); |
| 144 | | start(); |
| 145 | | }, |
| 146 | | function(a, jq) { // pre-callback |
| 147 | | ok( true, 'pre-callback'); |
| 148 | | ok( a.constructor == Array, "type check"); |
| 149 | | ok( jq.jquery, "type check jQuery"); |
| 150 | | ok( jq[0].tagName.toLowerCase() == 'div', "jQuery arg == 'div'"); |
| 151 | | }, |
| 152 | | 'text.php', 'post'); |
| | 266 | test("ajaxSubmit: pseudo-form", function() { |
| | 267 | stop(); |
| | 268 | |
| | 269 | var opts = { |
| | 270 | before: function(a, jq) { // pre-submit callback |
| | 271 | ok( true, 'pre-submit callback'); |
| | 272 | ok( a.constructor == Array, "type check"); |
| | 273 | ok( jq.jquery, "type check jQuery"); |
| | 274 | ok( jq[0].tagName.toLowerCase() == 'div', "jQuery arg == 'div'"); |
| | 275 | }, |
| | 276 | after: function() { // post-submit callback |
| | 277 | ok( true, 'post-submit callback'); |
| | 278 | start(); |
| | 279 | }, |
| | 280 | // url and method must be provided for a pseudo form since they can |
| | 281 | // not be extracted from the markup |
| | 282 | url: 'text.php', |
| | 283 | method: 'post' |
| | 284 | }; |
| | 285 | |
| | 286 | $("#pseudo").ajaxSubmit(opts); |
| 156 | | test("ajaxSubmit7", function() { |
| 157 | | stop(); |
| 158 | | |
| 159 | | $("#form2").ajaxSubmit(null, function(responseText) { // post-callback |
| 160 | | ok( true, 'post-callback'); |
| 161 | | var data = eval.call(window, '('+responseText+')'); |
| 162 | | ok( data.name == 'jquery-test', 'evaled respone'); |
| 163 | | start(); |
| 164 | | }, |
| 165 | | null, 'json.php'); |
| 166 | | }); |
| 167 | | |
| 168 | | // test passing jQuery object as the target |
| 169 | | test("ajaxSubmit8", function() { |
| 170 | | stop(); |
| 171 | | var target = $('#targetDiv'); |
| 172 | | target.html(""); |
| 173 | | |
| 174 | | $("#form2").ajaxSubmit(target, function(responseText) { // post-callback |
| 175 | | ok( true, 'post-callback'); |
| 176 | | ok( $('#targetDiv').text().match("Lorem ipsum"), "targetDiv updated"); |
| 177 | | start(); |
| 178 | | }); |
| 179 | | }); |
| 180 | | |
| 181 | | // test passing DOM element as the target |
| 182 | | test("ajaxSubmit9", function() { |
| 183 | | stop(); |
| 184 | | var target = $('#targetDiv'); |
| 185 | | target.html(""); |
| 186 | | |
| 187 | | $("#form2").ajaxSubmit(target[0], function(responseText) { // post-callback |
| 188 | | ok( true, 'post-callback'); |
| 189 | | ok( $('#targetDiv').text().match("Lorem ipsum"), "targetDiv updated"); |
| 190 | | start(); |
| 191 | | }); |
| | 290 | test("ajaxSubmit: evaluate response", function() { |
| | 291 | stop(); |
| | 292 | |
| | 293 | var opts = { |
| | 294 | after: function(responseText) { // post-callback |
| | 295 | ok( true, 'post-callback'); |
| | 296 | var data = eval.call(window, '('+responseText+')'); |
| | 297 | ok( data.name == 'jquery-test', 'evaled respone'); |
| | 298 | start(); |
| | 299 | }, |
| | 300 | url: 'json.txt' |
| | 301 | }; |
| | 302 | |
| | 303 | $("#form2").ajaxSubmit(opts); |
| 195 | | test("ajaxForm1", function() { |
| 196 | | stop(); |
| 197 | | $("#form4").ajaxForm('#targetDiv', function() { |
| 198 | | ok( true, 'post-callback'); |
| 199 | | start(); |
| 200 | | }, |
| 201 | | function(a, jq) { |
| 202 | | ok( true, 'pre-callback'); |
| 203 | | ok( a.constructor == Array, "type check"); |
| 204 | | ok( jq.jquery, "type check jQuery"); |
| 205 | | }); |
| 206 | | $('#doSubmit')[0].click(); |
| | 307 | test("ajaxForm: pre and post callbacks", function() { |
| | 308 | stop(); |
| | 309 | |
| | 310 | var opts = { |
| | 311 | before: function(a, jq) { // pre-submit callback |
| | 312 | ok( true, 'pre-submit callback'); |
| | 313 | ok( a.constructor == Array, "type check"); |
| | 314 | ok( jq.jquery, "type check jQuery"); |
| | 315 | }, |
| | 316 | after: function() { // post-submit callback |
| | 317 | ok( true, 'post-submit callback'); |
| | 318 | start(); |
| | 319 | } |
| | 320 | }; |
| | 321 | |
| | 322 | $("#form4").ajaxForm(opts); |
| | 323 | $('#submitForm4')[0].click(); // trigger the submit button |
| 210 | | test("ajaxForm2", function() { |
| 211 | | $("#form4").ajaxForm(null, null, function(a, jq) { |
| 212 | | ok( true, 'pre-callback'); |
| 213 | | ok( a.constructor == Array, "type check"); |
| 214 | | ok( jq.jquery, "type check jQuery"); |
| 215 | | ok( arrayValue(a, 'myName') != null, "submit button"); |
| 216 | | }); |
| 217 | | $('#mySubmit')[0].click(); |
| 218 | | }); |
| 219 | | |
| | 327 | test("ajaxForm: capture submit element", function() { |
| | 328 | |
| | 329 | var opts = { |
| | 330 | before: function(a, jq) { // pre-callback |
| | 331 | ok( true, 'pre-callback'); |
| | 332 | ok( a.constructor == Array, "type check"); |
| | 333 | ok( jq.jquery, "type check jQuery"); |
| | 334 | ok( arrayValue(a, 'form4inputName') != null, "submit button"); |
| | 335 | } |
| | 336 | }; |
| | 337 | |
| | 338 | $("#form4").ajaxForm(opts); |
| | 339 | $('#submitForm4withName')[0].click(); |
| | 340 | }); |
| | 341 | |
| 221 | | test("ajaxForm3", function() { |
| 222 | | $("#form4").ajaxForm('#targetDiv', null, function(a, jq) { // pre-callback |
| 223 | | ok( true, 'pre-callback'); |
| 224 | | ok( a.constructor == Array, "type check"); |
| 225 | | ok( jq.jquery, "type check jQuery"); |
| 226 | | ok( arrayValue(a, 'myImage_x') != null, "x coord"); |
| 227 | | ok( arrayValue(a, 'myImage_y') != null, "y coord"); |
| 228 | | }); |
| 229 | | $('#imageSubmit')[0].click(); |
| | 343 | test("ajaxForm: capture submit image coordinates", function() { |
| | 344 | |
| | 345 | var opts = { |
| | 346 | before: function(a, jq) { // pre-callback |
| | 347 | ok( true, 'pre-callback'); |
| | 348 | ok( a.constructor == Array, "type check"); |
| | 349 | ok( jq.jquery, "type check jQuery"); |
| | 350 | ok( arrayValue(a, 'myImage_x') != null, "x coord"); |
| | 351 | ok( arrayValue(a, 'myImage_y') != null, "y coord"); |
| | 352 | } |
| | 353 | }; |
| | 354 | |
| | 355 | $("#form4").ajaxForm(opts); |
| | 356 | $('#form4imageSubmit')[0].click(); |
| 233 | | test("ajaxForm4", function() { |
| 234 | | $('#targetDiv').html(""); |
| 235 | | stop(); |
| 236 | | $("#form4").ajaxForm('#targetDiv', function() { |
| 237 | | ok( true, 'post-callback'); |
| 238 | | ok( $('#targetDiv').text().match("Lorem ipsum"), "targetDiv updated"); |
| 239 | | start(); |
| 240 | | }, |
| 241 | | function(a, jq) { |
| 242 | | ok( true, 'pre-callback'); |
| 243 | | ok( a.constructor == Array, "type check"); |
| 244 | | ok( jq.jquery, "type check jQuery"); |
| 245 | | }); |
| 246 | | $('#doSubmit')[0].click(); |
| | 360 | test("ajaxForm: update target div", function() { |
| | 361 | $('#targetDiv').empty(); |
| | 362 | stop(); |
| | 363 | |
| | 364 | var opts = { |
| | 365 | target: '#targetDiv', |
| | 366 | before: function(a, jq) { // pre-callback |
| | 367 | ok( true, 'pre-callback'); |
| | 368 | ok( a.constructor == Array, "type check"); |
| | 369 | ok( jq.jquery, "type check jQuery"); |
| | 370 | }, |
| | 371 | after: function() { |
| | 372 | ok( true, 'post-callback'); |
| | 373 | ok( $('#targetDiv').text().match("Lorem ipsum"), "targetDiv updated"); |
| | 374 | start(); |
| | 375 | } |
| | 376 | }; |
| | 377 | |
| | 378 | $("#form4").ajaxForm(opts); |
| | 379 | $('#submitForm4')[0].click(); |
| 346 | | <input type="submit" id="doSubmit" /> |
| 347 | | <input type="submit" id="mySubmit" name="myName" /> |
| 348 | | <input type="image" id="imageSubmit" name="myImage" src="submit.gif" /> |
| | 479 | <input type="submit" id="submitForm4" /> |
| | 480 | <input type="submit" id="submitForm4withName" name="form4inputName" /> |
| | 481 | <input type="image" id="form4imageSubmit" name="myImage" src="submit.gif" /> |
| | 482 | </div></form> |
| | 483 | |
| | 484 | <!-- form5 --> |
| | 485 | <form id="form5" action="text.php?test=form" method="get"><div> |
| | 486 | <input name="a" type="text" /> |
| | 487 | <input type="submit" id="submitForm4" /> |
| | 488 | <input type="submit" id="submitForm4withName" name="form4inputName" /> |
| | 489 | <input type="image" id="form4imageSubmit" name="myImage" src="submit.gif" /> |