X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Ftraversing.js;h=46a49bb9788c771f8d0412a580471cc37c093ea2;hb=8356871a5522ad59cac80af2ca4695079059b97e;hp=8abb73751b3d2582a04c1c11c55ff8a83947c50d;hpb=7c04a64016c1f295dfdf4e731f693f8400a987bc;p=jquery.git diff --git a/test/unit/traversing.js b/test/unit/traversing.js index 8abb737..46a49bb 100644 --- a/test/unit/traversing.js +++ b/test/unit/traversing.js @@ -1,3 +1,5 @@ +module("traversing"); + test("end()", function() { expect(3); equals( 'Yahoo', jQuery('#yahoo').parent().end().text(), 'Check for end' ); @@ -49,12 +51,11 @@ test("is(String)", function() { ok( jQuery('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' ); }); -test("filter()", function() { - expect(6); +test("filter(Selector)", function() { + expect(5); isSet( jQuery("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" ); isSet( jQuery("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" ); isSet( jQuery("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" ); - isSet( jQuery("p").filter(function() { return !jQuery("a", this).length }).get(), q("sndp", "first"), "filter(Function)" ); // using contents will get comments regular, text, and comment nodes var j = jQuery("#nonnodes").contents(); @@ -62,8 +63,35 @@ test("filter()", function() { equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" ); }); +test("filter(Function)", function() { + expect(1); + + isSet( jQuery("p").filter(function() { return !jQuery("a", this).length }).get(), q("sndp", "first"), "filter(Function)" ); +}); + +test("filter(Element)", function() { + expect(1); + + var element = document.getElementById("text1"); + isSet( jQuery("#form input").filter(element).get(), q("text1"), "filter(Element)" ); +}); + +test("filter(Array)", function() { + expect(1); + + var elements = [ document.getElementById("text1") ]; + isSet( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" ); +}); + +test("filter(jQuery)", function() { + expect(1); + + var elements = jQuery("#text1"); + isSet( jQuery("#form input").filter(elements).get(), q("text1"), "filter(Element)" ); +}) + test("closest()", function() { - expect(6); + expect(9); isSet( jQuery("body").closest("body").get(), q("body"), "closest(body)" ); isSet( jQuery("body").closest("html").get(), q("html"), "closest(html)" ); isSet( jQuery("body").closest("div").get(), [], "closest(div)" ); @@ -71,26 +99,50 @@ test("closest()", function() { isSet( jQuery("div:eq(1)").closest("div:first").get(), [], "closest(div:first)" ); isSet( jQuery("div").closest("body:first div:last").get(), q("fx-tests"), "closest(body:first div:last)" ); + + // Test .closest() limited by the context + var jq = jQuery("#nothiddendivchild"); + isSet( jq.closest("html", document.body).get(), [], "Context limited." ); + isSet( jq.closest("body", document.body).get(), [], "Context limited." ); + isSet( jq.closest("#nothiddendiv", document.body).get(), q("nothiddendiv"), "Context not reached." ); }); -test("not()", function() { - expect(11); +test("not(Selector)", function() { + expect(7); equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" ); - equals( jQuery("#main > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" ); isSet( jQuery("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" ); isSet( jQuery("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" ); - isSet( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" ); - equals( jQuery("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" ); isSet( jQuery("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d", "option3e" ), "not('complex selector')"); + isSet( jQuery('#ap *').not('code'), q("google", "groups", "anchor1", "mark"), "not('tag selector')" ); + isSet( jQuery('#ap *').not('code, #mark'), q("google", "groups", "anchor1"), "not('tag, ID selector')" ); + isSet( jQuery('#ap *').not('#mark, code'), q("google", "groups", "anchor1"), "not('ID, tag selector')"); +}); + +test("not(Element)", function() { + expect(1); + var selects = jQuery("#form select"); isSet( selects.not( selects[1] ), q("select1", "select3"), "filter out DOM element"); +}); - isSet( jQuery('#ap *').not('code'), q("google", "groups", "anchor1", "mark"), "not('tag selector')" ); - isSet( jQuery('#ap *').not('code, #mark'), q("google", "groups", "anchor1"), "not('tag, ID selector')" ); - isSet( jQuery('#ap *').not('#mark, code'), q("google", "groups", "anchor1"), "not('ID, tag selector')"); +test("not(Function)", function() { + isSet( jQuery("p").not(function() { return jQuery("a", this).length }).get(), q("sndp", "first"), "not(Function)" ); }); +test("not(Array)", function() { + expect(2); + + equals( jQuery("#main > p#ap > a").not(document.getElementById("google")).length, 2, "not(DOMElement)" ); + equals( jQuery("p").not(document.getElementsByTagName("p")).length, 0, "not(Array-like DOM collection)" ); +}); + +test("not(jQuery)", function() { + expect(1); + + isSet( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" ); +}) + test("andSelf()", function() { expect(4); isSet( jQuery("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" ); @@ -151,18 +203,30 @@ test("prev([String])", function() { }); test("slice()", function() { - expect(6); - + expect(7); + var $links = jQuery("#ap a"); - + isSet( $links.slice(1,2), q("groups"), "slice(1,2)" ); isSet( $links.slice(1), q("groups", "anchor1", "mark"), "slice(1)" ); isSet( $links.slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" ); isSet( $links.slice(-1), q("mark"), "slice(-1)" ); isSet( $links.eq(1), q("groups"), "eq(1)" ); - isSet( $links.eq('2'), q("anchor1"), "eq('2')" ); + isSet( $links.eq(-1), q("mark"), "eq(-1)" ); +}); + +test("first()/last()", function() { + expect(4); + + var $links = jQuery("#ap a"), $none = jQuery("asdf"); + + isSet( $links.first(), q("google"), "first()" ); + isSet( $links.last(), q("mark"), "last()" ); + + isSet( $none.first(), [], "first() none" ); + isSet( $none.last(), [], "last() none" ); }); test("map()", function() { @@ -243,4 +307,4 @@ test("contents()", function() { var c = jQuery("#nonnodes").contents().contents(); equals( c.length, 1, "Check node,textnode,comment contents is just one" ); equals( c[0].nodeValue, "hi", "Check node,textnode,comment contents is just the one from span" ); -}); \ No newline at end of file +});