Integration of Mike Alsup's excellent form serialization code. The benefits are as...
[jquery.git] / src / jquery / coreTest.js
index e919284..db47856 100644 (file)
@@ -12,7 +12,7 @@ test("Basic requirements", function() {
 });
 
 test("$()", function() {
-       expect(2);
+       expect(5);
        
        var main = $("#main");
        isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
@@ -29,6 +29,13 @@ test("$()", function() {
                pass = false;
        }
        ok( pass, "$('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
+
+       var code = $("<code/>");
+       equals( code.length, 1, "Correct number of elements generated for code" );
+       var img = $("<img/>");
+       equals( img.length, 1, "Correct number of elements generated for img" );
+       var div = $("<div/><hr/><code/><b/>");
+       equals( div.length, 4, "Correct number of elements generated for div hr code b" );
 });
 
 test("isFunction", function() {
@@ -232,7 +239,7 @@ test("attr(Hash)", function() {
 });
 
 test("attr(String, Object)", function() {
-       expect(8);
+       expect(12);
        var div = $("div");
        div.attr("foo", "bar");
        var pass = true;
@@ -255,6 +262,28 @@ test("attr(String, Object)", function() {
        ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
        $("#name").attr('maxlength', '5');
        ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
+
+       reset();
+
+       var type = $("#check2").attr('type');
+       var thrown = false;
+       try {
+               $("#check2").attr('type','hidden');
+       } catch(e) {
+               thrown = true;
+       }
+       ok( thrown, "Exception thrown when trying to change type property" );
+       equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" );
+
+       var check = document.createElement("input");
+       var thrown = true;
+       try {
+               $(check).attr('type','checkbox');
+       } catch(e) {
+               thrown = false;
+       }
+       ok( thrown, "Exception thrown when trying to change type property" );
+       equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
 });
 
 test("attr(String, Object) - Loaded via XML document", function() {
@@ -348,20 +377,50 @@ test("wrap(String|Element)", function() {
        ok( result.text() == defaultText, 'Check for element wrapping' );
        
        reset();
-       //stop();
        $('#check1').click(function() {         
                var checkbox = this;            
                ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
                $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
                ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
-               // use a fade in to check state after this event handler has finished
-               /*setTimeout(function() {
-                       ok( !checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
-                       start();
-               }, 100);*/
        }).click();
 });
 
+test("wrapAll(String|Element)", function() {
+       expect(8);
+       var prev = $("#first")[0].previousSibling;
+       var p = $("#first")[0].parentNode;
+       var result = $('#first,#firstp').wrapAll('<div class="red"><div id="tmp"></div></div>');
+       equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
+       ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
+       ok( $('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
+       equals( $("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
+       equals( $("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
+
+       reset();
+       var prev = $("#first")[0].previousSibling;
+       var p = $("#first")[0].parentNode;
+       var result = $('#first,#firstp').wrapAll(document.getElementById('empty'));
+       equals( $("#first").parent()[0], $("#firstp").parent()[0], "Same Parent" );
+       equals( $("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
+       equals( $("#first").parent()[0].parentNode, p, "Correct Parent" );
+});
+
+test("wrapInner(String|Element)", function() {
+       expect(6);
+       var num = $("#first").children().length;
+       var result = $('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
+       equals( $("#first").children().length, 1, "Only one child" );
+       ok( $("#first").children().is(".red"), "Verify Right Element" );
+       equals( $("#first").children().children().children().length, num, "Verify Elements Intact" );
+
+       reset();
+       var num = $("#first").children().length;
+       var result = $('#first').wrapInner(document.getElementById('empty'));
+       equals( $("#first").children().length, 1, "Only one child" );
+       ok( $("#first").children().is("#empty"), "Verify Right Element" );
+       equals( $("#first").children().children().length, num, "Verify Elements Intact" );
+});
+
 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
        expect(18);
        var defaultText = 'Try them out:'
@@ -683,28 +742,28 @@ test("is(String)", function() {
        ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
        ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
        ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
-       ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
-       ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
-       ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
-       ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
+       ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
+       ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
+       ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
+       ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
        ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
        ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
        ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
        ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
-       ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
-       ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
-       ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
-       ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
+       ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
+       ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
+       ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
+       ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
        ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
        ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
        ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
        ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
        
        // test is() with comma-seperated expressions
-       ok( $('#en').is('[@lang="en"],[@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
-       ok( $('#en').is('[@lang="de"],[@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
-       ok( $('#en').is('[@lang="en"] , [@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
-       ok( $('#en').is('[@lang="de"] , [@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+       ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+       ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+       ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+       ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
 });
 
 test("$.extend(Object, Object)", function() {
@@ -777,7 +836,7 @@ test("html(String)", function() {
 
 test("filter()", function() {
        expect(4);
-       isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
+       isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
        isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
        isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
        isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
@@ -790,19 +849,27 @@ test("not()", function() {
        isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
 });
 
+test("andSelf()", function() {
+       expect(4);
+       isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" );
+       isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" );
+       isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" );
+       isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" );
+});
+
 test("siblings([String])", function() {
        expect(5);
        isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
-       isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" ); 
-       isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
-       isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "floatTest"), "Check for multiple filters" );
+       isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" ); 
+       isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
+       isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" );
        isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
 });
 
 test("children([String])", function() {
        expect(3);
        isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
-       isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
+       isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" );
        isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
 });
 
@@ -961,14 +1028,6 @@ test("empty()", function() {
        ok( $("#ap").children().length == 4, "Check elements are not removed" );
 });
 
-test("eq(), gt(), lt(), contains()", function() {
-       expect(4);
-       ok( $("#ap a").eq(1)[0].id == "groups", "eq()" );
-       isSet( $("#ap a").gt(0).get(), q("groups", "anchor1", "mark"), "gt()" );
-       isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );
-       isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );
-});
-
 test("slice()", function() {
        expect(4);
        isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" );
@@ -976,3 +1035,31 @@ test("slice()", function() {
        isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" );
        isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" );
 });
+
+test("map()", function() {
+       expect(2);
+
+       isSet(
+               $("#ap").map(function(){
+                       return $(this).find("a").get();
+               }),
+               q("google", "groups", "anchor1", "mark"),
+               "Array Map"
+       );
+
+       isSet(
+               $("#ap > a").map(function(){
+                       return this.parentNode;
+               }),
+               q("ap","ap","ap"),
+               "Single Map"
+       );
+});
+
+test("contents()", function() {
+       expect(2);
+       equals( $("#ap").contents().length, 9, "Check element contents" );
+       ok( $("#iframe").contents()[0], "Check existance of IFrame document" );
+       // Disabled, randomly fails
+       //ok( $("#iframe").contents()[0].body, "Check existance of IFrame body" );
+});