Bug Tracker

Changeset 463

Show
Ignore:
Timestamp:
10/23/06 23:30:23 (2 years ago)
Author:
malsup
Message:

Updated test suite to support change to options args.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/form/test/index.html

    r450 r463  
    1818h2 { padding: 10px; background-color: #eee; color: black; margin: 0; font-size: small; font-weight: normal } 
    1919 
    20 .pass { color: green; }  
    21 .fail { color: red; }  
     20.pass { color: green; } 
     21.fail { color: red; } 
    2222#tests ol { display: none; } 
    2323</style> 
     
    2929    $('#userAgent').html(navigator.userAgent); 
    3030 
     31    // test form with inputs named 'action' and 'method' 
     32    test("'action' and 'method' form attributes", function() { 
     33        var f = $("#form1"); 
     34        ok( f.attr('action').match('text.php'), "form 'action'"); 
     35        ok( f.attr('method').match('get'), "form 'method'"); 
     36    }); 
     37 
    3138    // test serialization to array 
    32     test("formToArray()", function() { 
     39    test("formToArray: multi-select", function() { 
    3340        var a = $("#form1").formToArray(); 
    3441        ok( a.constructor == Array, "type check"); 
    3542        ok( a.length == 13, "array length"); 
    3643        ok( arrayCount(a, 'Multiple') == 3, "multi-select"); 
     44    }); 
     45 
     46    // test serialization to array 
     47    test("formToArray: 'action' and 'method' inputs", function() { 
     48        var a = $("#form1").formToArray(); 
     49        ok( a.constructor == Array, "type check"); 
    3750        ok( arrayValue(a, 'action') == 1, "input name=action"); 
    3851        ok( arrayValue(a, 'method') == 2, "input name=method"); 
    3952    }); 
    4053 
    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() { 
    6456        var formData = $("#form2").formToArray(true); 
    6557        var testData = ['a','b','c','d','e','f']; 
     
    6961    }); 
    7062 
     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 
    7177    // test ajaxSubmit target update 
    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 
    81130 
    82131    // test ajaxSubmit pre-submit callback 
    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); 
    92143    }); 
    93144 
    94145    // test ajaxSubmit post-submit callback for response and status text 
    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() { 
    111163        var testData = ['a','b','c','d','e','f']; 
    112164 
    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); 
    124245    }); 
    125246 
    126247    // test ajaxSubmit using pre-submit callback to cancel submit 
    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); 
    137263    }); 
    138264 
    139265    // test submitting a pseudo-form 
    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); 
    153287    }); 
    154288 
    155289    // test eval of json response 
    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); 
    192304    }); 
    193305 
    194306    // test pre and post callbacks for ajaxForm 
    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 
    207324    }); 
    208325 
    209326    // test that the value of the submit button is captured 
    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 
    220342    // test image submit support 
    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(); 
    230357    }); 
    231358 
    232359    // test that the targetDiv gets updated 
    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(); 
    247380    }); 
    248381 
    249382})}); 
    250383 
    251 // helper method    
     384// helper method 
    252385function arrayCount(arr, key) { 
    253386    var count=0; 
    254387    for (var i=0; i < arr.length; i++) { 
    255         if (arr[i].name == key)  
     388        if (arr[i].name == key) 
    256389            count++; 
    257390    } 
     
    259392} 
    260393 
    261 // helper method    
     394// helper method 
    262395function arrayValue(arr, key) { 
    263396    for (var i=0; i < arr.length; i++) { 
     
    271404    <h1 id="banner">jQuery form.js - Test Suite</h1> 
    272405    <h2 id="userAgent"></h2> 
    273      
     406 
    274407    <!-- Test HTML --> 
    275408    <div id="main" style="display: none;"> 
    276409 
    277410        <!-- form1 --> 
    278         <form id="form1" action="text.php" method="post"><div> 
     411        <form id="form1" action="text.php" method="get"><div> 
    279412                <input type="hidden" name="Hidden" value="hiddenValue" /> 
    280413                <input name="Name" type="text" value="MyName1" /> 
     
    326459 
    327460        <!-- form2 --> 
    328         <form id="form2" action="text.php" method="post"><div> 
     461        <form id="form2" action="text.php" method="get"><div> 
    329462            <input    name="a" type="text" /> 
    330463            <textarea name="b" rows="1" cols="1"></textarea> 
     
    337470 
    338471        <!-- form3 --> 
    339         <form id="form3" action="text.php" method="get"><div> 
     472        <form id="form3" action="text.php" method="post"><div> 
    340473            <input name="a" type="text" /> 
    341474        </div></form> 
     
    344477        <form id="form4" action="text.php" method="get"><div> 
    345478            <input name="a" type="text" /> 
    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" /> 
    349490        </div></form> 
    350491 
     
    363504        <div id="targetDiv"></div> 
    364505    </div> 
    365      
     506 
    366507    <ol id="tests"></ol> 
    367508</body>