Added equals helper for easier debugging of failed tests, soon to be used more often...
[jquery.git] / src / jquery / coreTest.js
index f736d4d..11cb5fb 100644 (file)
@@ -11,6 +11,14 @@ test("Basic requirements", function() {
        ok( $, "$()" );\r
 });\r
 \r
+test("$()", function() {\r
+       var main = $("#main");\r
+       isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );\r
+       \r
+       // make sure this is handled\r
+       $('<p>\r\n</p>');\r
+});\r
+\r
 test("length", function() {\r
        ok( $("div").length == 2, "Get Number of Elements Found" );\r
 });\r
@@ -29,7 +37,7 @@ test("get(Number)", function() {
 \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
+       isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );\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
@@ -66,7 +74,7 @@ test("index(Object)", function() {
 });\r
 \r
 test("attr(String)", function() {\r
-       expect(12);\r
+       expect(15);\r
        ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );\r
        ok( $('#text1').attr('type') == "text", 'Check for type attribute' );\r
        ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );\r
@@ -79,11 +87,19 @@ test("attr(String)", function() {
        ok( $('#name').attr('name') == "name", 'Check for name attribute' );\r
        ok( $('#text1').attr('name') == "action", 'Check for name attribute' );\r
        ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );\r
+       equals( "#2", $('#anchor2').attr('href'), 'Check for non-absolute href (an anchor)' );\r
+       stop();\r
+       $.get("data/dashboard.xml", function(xml) {\r
+               ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );\r
+               ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );\r
+               start();\r
+       });\r
 });\r
 \r
 test("attr(String, Function)", function() {\r
-       expect(1);\r
+       expect(2);\r
        ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );\r
+       ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");\r
 });\r
 \r
 test("attr(Hash)", function() {\r
@@ -96,7 +112,7 @@ test("attr(Hash)", function() {
 });\r
 \r
 test("attr(String, Object)", function() {\r
-       expect(6);\r
+       expect(7);\r
        var div = $("div");\r
        div.attr("foo", "bar");\r
        var pass = true;\r
@@ -104,6 +120,8 @@ test("attr(String, Object)", function() {
          if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;\r
        }\r
        ok( pass, "Set Attribute" );\r
+\r
+       ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );    \r
        \r
        $("#name").attr('name', 'something');\r
        ok( $("#name").attr('name') == 'something', 'Set name attribute' );\r
@@ -488,10 +506,11 @@ test("filter()", function() {
        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
+test("not()", function() {\r
+       expect(3);\r
        ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );\r
        isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );\r
+       isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );\r
 });\r
 \r
 \r
@@ -656,3 +675,13 @@ test("eq(), gt(), lt(), contains()", function() {
        isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );\r
        isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );\r
 });\r
+\r
+test("click() context", function() {\r
+       $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {\r
+           var close = $('spanx', this); // same with $(this).find('span');\r
+           ok( close.length == 0, "Element does not exist, length must be zero" );\r
+           ok( !close[0], "Element does not exist, direct access to element must return undefined" );\r
+           //console.log( close[0]); // it's the <a> and not a <span> element\r
+           return false;\r
+       }).click();\r
+});
\ No newline at end of file