Added tests for almost everything in jquery/jquery.js; fixed some docs and removed the now useless docs for $(jQuery)

This commit is contained in:
Jörn Zaefferer 2007-01-10 11:08:54 +00:00
parent caad7f814e
commit 200319ea6b
2 changed files with 179 additions and 26 deletions

158
src/jquery/coreTest.js vendored
View file

@ -214,6 +214,29 @@ test("append(String|Element|Array<Element>|jQuery)", function() {
ok( expected == $('#sap').text(), "Check for appending of jQuery object" ); 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:'
$('<b>buga</b>').appendTo('#first');
ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
ok( $('<option value="appendTest">Append Test</option>').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&lt;Element&gt;|jQuery)", function() { test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
expect(5); expect(5);
var defaultText = 'Try them out:' var defaultText = 'Try them out:'
@ -237,6 +260,29 @@ test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
ok( expected == $('#sap').text(), "Check for prepending of jQuery object" ); ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
}); });
test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
expect(5);
var defaultText = 'Try them out:'
$('<b>buga</b>').prependTo('#first');
ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
ok( $('<option value="prependTest">Prepend Test</option>').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&lt;Element&gt;|jQuery)", function() { test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
expect(4); expect(4);
var expected = 'This is a normal link: bugaYahoo'; var expected = 'This is a normal link: bugaYahoo';
@ -259,6 +305,28 @@ test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
ok( expected == $('#en').text(), "Insert jQuery before" ); ok( expected == $('#en').text(), "Insert jQuery before" );
}); });
test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
expect(4);
var expected = 'This is a normal link: bugaYahoo';
$('<b>buga</b>').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&lt;Element&gt;|jQuery)", function() { test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
expect(4); expect(4);
var expected = 'This is a normal link: Yahoobuga'; var expected = 'This is a normal link: Yahoobuga';
@ -281,10 +349,36 @@ test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
ok( expected == $('#en').text(), "Insert jQuery after" ); ok( expected == $('#en').text(), "Insert jQuery after" );
}); });
test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
expect(4);
var expected = 'This is a normal link: Yahoobuga';
$('<b>buga</b>').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() { test("end()", function() {
expect(2); expect(3);
ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' ); ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
ok( $('#yahoo').end(), 'Check for end with nothing to 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() { test("find(String)", function() {
@ -492,3 +586,65 @@ test("text(String)", function() {
expect(1); expect(1);
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" ); 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" );
}); });
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 = $("<p>Hi</p>")[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()" );
});

47
src/jquery/jquery.js vendored
View file

@ -160,21 +160,6 @@ var $ = jQuery;
* @type 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 = { jQuery.fn = jQuery.prototype = {
/** /**
* The current version of jQuery. * The current version of jQuery.
@ -447,7 +432,7 @@ jQuery.fn = jQuery.prototype = {
for ( var prop in obj ) for ( var prop in obj )
jQuery.attr( jQuery.attr(
type ? this.style : this, 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; return obj;
}, },
prop: function(elem, key, value){ prop: function(elem, value){
// Handle executable functions // Handle executable functions
return value.constructor == Function && return value.constructor == Function &&
value.call( elem ) || value; value.call( elem ) || value;
@ -1859,8 +1844,9 @@ jQuery.each({
* *
* @name appendTo * @name appendTo
* @type jQuery * @type jQuery
* @param String expr A jQuery expression of elements to match. * @param <Content> content Content to append to the selected element to.
* @cat DOM/Manipulation * @cat DOM/Manipulation
* @see append(<Content>)
*/ */
/** /**
@ -1876,8 +1862,9 @@ jQuery.each({
* *
* @name prependTo * @name prependTo
* @type jQuery * @type jQuery
* @param String expr A jQuery expression of elements to match. * @param <Content> content Content to prepend to the selected element to.
* @cat DOM/Manipulation * @cat DOM/Manipulation
* @see prepend(<Content>)
*/ */
/** /**
@ -1893,8 +1880,9 @@ jQuery.each({
* *
* @name insertBefore * @name insertBefore
* @type jQuery * @type jQuery
* @param String expr A jQuery expression of elements to match. * @param <Content> content Content to insert the selected element before.
* @cat DOM/Manipulation * @cat DOM/Manipulation
* @see before(<Content>)
*/ */
/** /**
@ -1910,8 +1898,9 @@ jQuery.each({
* *
* @name insertAfter * @name insertAfter
* @type jQuery * @type jQuery
* @param String expr A jQuery expression of elements to match. * @param <Content> content Content to insert the selected element after.
* @cat DOM/Manipulation * @cat DOM/Manipulation
* @see after(<Content>)
*/ */
jQuery.each({ 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") * @example $("p").addClass("selected")
* @before <p>Hello</p> * @before <p>Hello</p>
* @result [ <p class="selected">Hello</p> ] * @result [ <p class="selected">Hello</p> ]
* *
* @example $("p").addClass("selected highlight")
* @before <p>Hello</p>
* @result [ <p class="selected highlight">Hello</p> ]
*
* @name addClass * @name addClass
* @type jQuery * @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 * @cat DOM/Attributes
* @see removeClass(String) * @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() * @example $("p").removeClass()
* @before <p class="selected">Hello</p> * @before <p class="selected">Hello</p>
@ -1967,9 +1960,13 @@ jQuery.each({
* @before <p class="selected first">Hello</p> * @before <p class="selected first">Hello</p>
* @result [ <p class="first">Hello</p> ] * @result [ <p class="first">Hello</p> ]
* *
* @example $("p").removeClass("selected highlight")
* @before <p class="highlight selected first">Hello</p>
* @result [ <p class="first">Hello</p> ]
*
* @name removeClass * @name removeClass
* @type jQuery * @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 * @cat DOM/Attributes
* @see addClass(String) * @see addClass(String)
*/ */