Added tests for add() and fixed #770
[jquery.git] / src / jquery / coreTest.js
index beb5735..1c81e08 100644 (file)
@@ -27,6 +27,16 @@ test("get(Number)", function() {
        ok( $("div").get(0) == document.getElementById("main"), "Get A Single Element" );\r
 });\r
 \r
+test("add(String|Element|Array)", function() {\r
+       isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );\r
+       \r
+       ok( $([]).add($("#form")[0].elements).length > 13, "Check elements from array" );\r
+       \r
+       var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));\r
+       ok( x[0].id == "x1", "Check on-the-fly element1" );\r
+       ok( x[1].id == "x2", "Check on-the-fly element2" );\r
+});\r
+\r
 test("each(Function)", function() {\r
        expect(1);\r
        var div = $("div");\r
@@ -67,6 +77,11 @@ test("attr(String)", function() {
        ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );\r
 });\r
 \r
+test("attr(String, Function)", function() {\r
+       expect(1);\r
+       ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );\r
+});\r
+\r
 test("attr(Hash)", function() {\r
        expect(1);\r
        var pass = true;\r
@@ -87,7 +102,7 @@ test("attr(String, Object)", function() {
        ok( pass, "Set Attribute" );\r
        \r
        $("#name").attr('name', 'something');\r
-       ok( $("#name").name() == 'something', 'Set name attribute' );\r
+       ok( $("#name").attr('name') == 'something', 'Set name attribute' );\r
        $("#check2").attr('checked', true);\r
        ok( document.getElementById('check2').checked == true, 'Set checked attribute' );\r
        $("#check2").attr('checked', false);\r
@@ -98,19 +113,21 @@ test("attr(String, Object)", function() {
        ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );\r
 });\r
 \r
-test("attr(String, Object)x", function() {\r
-       expect(2);\r
-       stop();\r
-       $.get('data/dashboard.xml', function(xml) { \r
-         var titles = [];\r
-         $('tab', xml).each(function() {\r
-           titles.push($(this).attr('title'));\r
-         });\r
-         ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );\r
-         ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );\r
-         start();\r
+if ( location.protocol != "file:" ) {\r
+       test("attr(String, Object)x", function() {\r
+               expect(2);\r
+               stop();\r
+               $.get('data/dashboard.xml', function(xml) { \r
+               var titles = [];\r
+               $('tab', xml).each(function() {\r
+               titles.push($(this).attr('title'));\r
+               });\r
+               ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );\r
+               ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );\r
+               start();\r
+               });\r
        });\r
-});\r
+}\r
 \r
 test("css(String|Hash)", function() {\r
        expect(8);\r
@@ -278,18 +295,6 @@ test("clone()", function() {
        ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );\r
 });\r
 \r
-test("filter()", function() {\r
-       isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );\r
-       isSet( $("p").filter(["#ap", "#sndp"]).get(), q("ap", "sndp"), "filter(Array&lt;String&gt;)" );\r
-       isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );\r
-});\r
-\r
-test("not(String)", function() {\r
-       ok($("#main > p#ap > a").not("#google").length == 2, "not('selector')")\r
-       // TODO: Comma-based selector\r
-       //isSet( $("p").not("#ap, #sndp").get(), q("firstp", "en", "sap", "first", "result"), "not('selector, selector')" );\r
-});\r
-\r
 test("is(String)", function() {\r
        expect(22);\r
        ok( $('#form').is('form'), 'Check for element: A form must be a form' );\r
@@ -368,19 +373,62 @@ test("html(String)", function() {
        ok( pass, "Set HTML" );\r
 });\r
 \r
+test("filter()", function() {\r
+       expect(5);\r
+       isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );\r
+       isSet( $("p").filter(["#ap", "#sndp"]).get(), q("ap", "sndp"), "filter(Array&lt;String&gt;)" );\r
+       isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );\r
+       isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );\r
+       isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );\r
+});\r
+\r
+test("not(String)", function() {\r
+       expect(2);\r
+       ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );\r
+       isSet( $("p").not("#ap, #sndp").get(), q("firstp", "en", "sap", "first", "result"), "not('selector, selector')" );\r
+});\r
+\r
+\r
 test("siblings([String])", function() {\r
-       expect(3);\r
+       expect(4);\r
        isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );\r
        isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" ); \r
        isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );\r
+       isSet( $("#foo").siblings("form, b").get(), q("form", "floatTest"), "Check for multiple filters" );\r
 });\r
 \r
 test("children([String])", function() {\r
-       expect(2);\r
+       expect(3);\r
        isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );\r
        isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );\r
+       isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );\r
 });\r
 \r
+test("parent[s]([String])", function() {\r
+       expect(8);\r
+       ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );\r
+       ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );\r
+       ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );\r
+       ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );\r
+       \r
+       ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );\r
+       ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );\r
+       ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );\r
+       isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );\r
+});\r
+\r
+test("next/prev([String])", function() {\r
+       expect(8);\r
+       ok( $("#ap").next()[0].id == "foo", "Simple next check" );\r
+       ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );\r
+       ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );\r
+       ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );\r
+       \r
+       ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );\r
+       ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );\r
+       ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );\r
+       ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );\r
+});\r
 \r
 test("show()", function() {\r
        expect(1);\r
@@ -422,7 +470,21 @@ test("removeClass(String) - add three classes and remove again", function() {
        ok( pass, "Remove multiple classes" );\r
 });\r
 \r
+test("toggleClass(String)", function() {\r
+       expect(3);\r
+       var e = $("#firstp");\r
+       ok( !e.is(".test"), "Assert class not present" );\r
+       e.toggleClass("test");\r
+       ok( e.is(".test"), "Assert class present" ); \r
+       e.toggleClass("test");\r
+       ok( !e.is(".test"), "Assert class not present" );\r
+});\r
+\r
 test("removeAttr(String", function() {\r
        ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );\r
 });\r
 \r
+test("text(String)", function() {\r
+       expect(1);\r
+       ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );\r
+});\r