From 200319ea6b6a59daebce1cfc1d330a9d52c208c0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B6rn=20Zaefferer?= Date: Wed, 10 Jan 2007 11:08:54 +0000 Subject: [PATCH] Added tests for almost everything in jquery/jquery.js; fixed some docs and removed the now useless docs for $(jQuery) --- src/jquery/coreTest.js | 158 +++++++++++++++++++++++++++++++++++++++++++++++- src/jquery/jquery.js | 47 +++++++------- 2 files changed, 179 insertions(+), 26 deletions(-) diff --git a/src/jquery/coreTest.js b/src/jquery/coreTest.js index e587c3a..ed7b52b 100644 --- a/src/jquery/coreTest.js +++ b/src/jquery/coreTest.js @@ -214,6 +214,29 @@ test("append(String|Element|Array<Element>|jQuery)", function() { ok( expected == $('#sap').text(), "Check for appending of jQuery object" ); }); +test("appendTo(String|Element|Array<Element>|jQuery)", function() { + expect(5); + var defaultText = 'Try them out:' + $('buga').appendTo('#first'); + ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' ); + ok( $('').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element'); + + reset(); + expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; + $(document.getElementById('first')).appendTo('#sap'); + ok( expected == $('#sap').text(), "Check for appending of element" ); + + reset(); + expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; + $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap'); + ok( expected == $('#sap').text(), "Check for appending of array of elements" ); + + reset(); + expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; + $("#first, #yahoo").appendTo('#sap'); + ok( expected == $('#sap').text(), "Check for appending of jQuery object" ); +}); + test("prepend(String|Element|Array<Element>|jQuery)", function() { expect(5); var defaultText = 'Try them out:' @@ -237,6 +260,29 @@ test("prepend(String|Element|Array<Element>|jQuery)", function() { ok( expected == $('#sap').text(), "Check for prepending of jQuery object" ); }); +test("prependTo(String|Element|Array<Element>|jQuery)", function() { + expect(5); + var defaultText = 'Try them out:' + $('buga').prependTo('#first'); + ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' ); + ok( $('').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element'); + + reset(); + expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; + $(document.getElementById('first')).prependTo('#sap'); + ok( expected == $('#sap').text(), "Check for prepending of element" ); + + reset(); + expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; + $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap'); + ok( expected == $('#sap').text(), "Check for prepending of array of elements" ); + + reset(); + expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; + $("#yahoo, #first").prependTo('#sap'); + ok( expected == $('#sap').text(), "Check for prepending of jQuery object" ); +}); + test("before(String|Element|Array<Element>|jQuery)", function() { expect(4); var expected = 'This is a normal link: bugaYahoo'; @@ -259,6 +305,28 @@ test("before(String|Element|Array<Element>|jQuery)", function() { ok( expected == $('#en').text(), "Insert jQuery before" ); }); +test("insertBefore(String|Element|Array<Element>|jQuery)", function() { + expect(4); + var expected = 'This is a normal link: bugaYahoo'; + $('buga').insertBefore('#yahoo'); + ok( expected == $('#en').text(), 'Insert String before' ); + + reset(); + expected = "This is a normal link: Try them out:Yahoo"; + $(document.getElementById('first')).insertBefore('#yahoo'); + ok( expected == $('#en').text(), "Insert element before" ); + + reset(); + expected = "This is a normal link: Try them out:diveintomarkYahoo"; + $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo'); + ok( expected == $('#en').text(), "Insert array of elements before" ); + + reset(); + expected = "This is a normal link: Try them out:diveintomarkYahoo"; + $("#first, #mark").insertBefore('#yahoo'); + ok( expected == $('#en').text(), "Insert jQuery before" ); +}); + test("after(String|Element|Array<Element>|jQuery)", function() { expect(4); var expected = 'This is a normal link: Yahoobuga'; @@ -281,10 +349,36 @@ test("after(String|Element|Array<Element>|jQuery)", function() { ok( expected == $('#en').text(), "Insert jQuery after" ); }); +test("insertAfter(String|Element|Array<Element>|jQuery)", function() { + expect(4); + var expected = 'This is a normal link: Yahoobuga'; + $('buga').insertAfter('#yahoo'); + ok( expected == $('#en').text(), 'Insert String after' ); + + reset(); + expected = "This is a normal link: YahooTry them out:"; + $(document.getElementById('first')).insertAfter('#yahoo'); + ok( expected == $('#en').text(), "Insert element after" ); + + reset(); + expected = "This is a normal link: YahooTry them out:diveintomark"; + $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo'); + ok( expected == $('#en').text(), "Insert array of elements after" ); + + reset(); + expected = "This is a normal link: YahooTry them out:diveintomark"; + $("#mark, #first").insertAfter('#yahoo'); + ok( expected == $('#en').text(), "Insert jQuery after" ); +}); + test("end()", function() { - expect(2); + expect(3); ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' ); ok( $('#yahoo').end(), 'Check for end with nothing to end' ); + + var x = $('#yahoo'); + x.parent(); + ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' ); }); test("find(String)", function() { @@ -492,3 +586,65 @@ test("text(String)", function() { expect(1); ok( $("#foo").text("
Hello cruel world!
")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" ); }); + +test("$.each(Object,Function)", function() { + expect(8); + $.each( [0,1,2], function(i, n){ + ok( i == n, "Check array iteration" ); + }); + + $.each( [5,6,7], function(i, n){ + ok( i == n - 5, "Check array iteration" ); + }); + + $.each( { name: "name", lang: "lang" }, function(i, n){ + ok( i == n, "Check object iteration" ); + }); +}); + +test("$.prop", function() { + expect(2); + var handle = function() { return this.id }; + ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" ); + ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" ); +}); + +test("$.className", function() { + expect(6); + var x = $("

Hi

")[0]; + var c = $.className; + c.add(x, "hi"); + ok( x.className == "hi", "Check single added class" ); + c.add(x, "foo bar"); + ok( x.className == "hi foo bar", "Check more added classes" ); + c.remove(x); + ok( x.className == "", "Remove all classes" ); + c.add(x, "hi foo bar"); + c.remove(x, "foo"); + ok( x.className == "hi bar", "Check removal of one class" ); + ok( c.has(x, "hi"), "Check has1" ); + ok( c.has(x, "bar"), "Check has2" ); +}); + +test("remove()", function() { + $("#ap").children().remove(); + ok( $("#ap").text().length > 10, "Check text is not removed" ); + ok( $("#ap").children().length == 0, "Check remove" ); + + reset(); + $("#ap").children().remove("a"); + ok( $("#ap").text().length > 10, "Check text is not removed" ); + ok( $("#ap").children().length == 1, "Check filtered remove" ); +}); + +test("empty()", function() { + ok( $("#ap").children().empty().text().length == 0, "Check text is removed" ); + ok( $("#ap").children().length == 4, "Check elements are not removed" ); +}); + +test("eq(), gt(), lt(), contains()", function() { + ok( $("#ap a").eq(1)[0].id == "groups", "eq()" ); + ok( $("#ap a").gt(1).get(), q("groups", "anchor1", "mark"), "gt()" ); + ok( $("#ap a").lt(2).get(), q("google", "groups", "anchor1"), "lt()" ); + ok( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" ); +}); \ No newline at end of file diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 0f712af..6e71268 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -160,21 +160,6 @@ var $ = jQuery; * @type jQuery */ -/** - * A means of creating a cloned copy of a jQuery object. This function - * copies the set of matched elements from one jQuery object and creates - * another, new, jQuery object containing the same elements. - * - * @example var div = $("div"); - * $( div ).find("p"); - * @desc Locates all p elements with all div elements, without disrupting the original jQuery object contained in 'div' (as would normally be the case if a simple div.find("p") was done). - * - * @name $ - * @param jQuery obj The jQuery object to be cloned. - * @cat Core - * @type jQuery - */ - jQuery.fn = jQuery.prototype = { /** * The current version of jQuery. @@ -447,7 +432,7 @@ jQuery.fn = jQuery.prototype = { for ( var prop in obj ) jQuery.attr( type ? this.style : this, - prop, jQuery.prop(this, prop, obj[prop], type) + prop, jQuery.prop(this, obj[prop]) ); }); }, @@ -1233,7 +1218,7 @@ jQuery.extend({ return obj; }, - prop: function(elem, key, value){ + prop: function(elem, value){ // Handle executable functions return value.constructor == Function && value.call( elem ) || value; @@ -1859,8 +1844,9 @@ jQuery.each({ * * @name appendTo * @type jQuery - * @param String expr A jQuery expression of elements to match. + * @param content Content to append to the selected element to. * @cat DOM/Manipulation + * @see append() */ /** @@ -1876,8 +1862,9 @@ jQuery.each({ * * @name prependTo * @type jQuery - * @param String expr A jQuery expression of elements to match. + * @param content Content to prepend to the selected element to. * @cat DOM/Manipulation + * @see prepend() */ /** @@ -1893,8 +1880,9 @@ jQuery.each({ * * @name insertBefore * @type jQuery - * @param String expr A jQuery expression of elements to match. + * @param content Content to insert the selected element before. * @cat DOM/Manipulation + * @see before() */ /** @@ -1910,8 +1898,9 @@ jQuery.each({ * * @name insertAfter * @type jQuery - * @param String expr A jQuery expression of elements to match. + * @param content Content to insert the selected element after. * @cat DOM/Manipulation + * @see after() */ jQuery.each({ @@ -1943,21 +1932,25 @@ jQuery.each({ */ /** - * Adds the specified class to each of the set of matched elements. + * Adds the specified class(es) to each of the set of matched elements. * * @example $("p").addClass("selected") * @before

Hello

* @result [

Hello

] * + * @example $("p").addClass("selected highlight") + * @before

Hello

+ * @result [

Hello

] + * * @name addClass * @type jQuery - * @param String class A CSS class to add to the elements + * @param String class One or more CSS classes to add to the elements * @cat DOM/Attributes * @see removeClass(String) */ /** - * Removes all or the specified class from the set of matched elements. + * Removes all or the specified class(es) from the set of matched elements. * * @example $("p").removeClass() * @before

Hello

@@ -1967,9 +1960,13 @@ jQuery.each({ * @before

Hello

* @result [

Hello

] * + * @example $("p").removeClass("selected highlight") + * @before

Hello

+ * @result [

Hello

] + * * @name removeClass * @type jQuery - * @param String class (optional) A CSS class to remove from the elements + * @param String class (optional) One or more CSS classes to remove from the elements * @cat DOM/Attributes * @see addClass(String) */ -- 1.7.10.4