From 0e63c789e3270e68c96c36a8ebad8d352fbfc76f Mon Sep 17 00:00:00 2001 From: Ariel Flesler Date: Sat, 24 May 2008 18:11:55 +0000 Subject: [PATCH] test runner: adding more tests for attr(). Related to [5574] and [5683]. --- test/unit/core.js | 235 +++++++++++++++++++++++++--------------------- 1 file changed, 128 insertions(+), 107 deletions(-) diff --git a/test/unit/core.js b/test/unit/core.js index bfaceca3..6d2ff6b9 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -13,10 +13,10 @@ test("Basic requirements", function() { test("$()", function() { expect(8); - + var main = $("#main"); isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" ); - + /* // disabled since this test was doing nothing. i tried to fix it but i'm not sure // what the expected behavior should even be. FF returns "\n" for the text node @@ -25,7 +25,7 @@ test("$()", function() { var x = crlfContainer.contents().get(0).nodeValue; equals( x, what???, "Check for \\r and \\n in jQuery()" ); */ - + /* // Disabled until we add this functionality in var pass = true; try { @@ -41,14 +41,14 @@ test("$()", function() { equals( img.length, 1, "Correct number of elements generated for img" ); var div = $("

"); equals( div.length, 4, "Correct number of elements generated for div hr code b" ); - + // can actually yield more than one, when iframes are included, the window is an array as well equals( $(window).length, 1, "Correct number of elements generated for window" ); - + equals( $(document).length, 1, "Correct number of elements generated for document" ); - + equals( $([1,2,3]).get(1), 2, "Test passing an array to the factory" ); - + equals( $(document.body).get(0), $('body').get(0), "Test passing an html node to the factory" ); }); @@ -90,7 +90,7 @@ test("browser", function() { test("noConflict", function() { expect(6); - + var old = jQuery; var newjQuery = jQuery.noConflict(); @@ -156,7 +156,7 @@ test("isFunction", function() { ok( !jQuery.isFunction(nodes), "childNodes Property" ); var first = document.body.firstChild; - + // Normal elements are reported ok everywhere ok( !jQuery.isFunction(first), "A normal DOM Element" ); @@ -206,10 +206,10 @@ test("$('html')", function() { ok( !foo, "Make sure the script wasn't executed prematurely" ); $("body").append(s); ok( foo, "Executing a scripts contents in the right context" ); - + reset(); ok( $("")[0], "Creating a link" ); - + reset(); var j = $("hi there "); @@ -230,7 +230,7 @@ if ( !isLocal ) { test("$(selector, xml).text(str) - Loaded via XML document", function() { expect(2); stop(); - $.get('data/dashboard.xml', function(xml) { + $.get('data/dashboard.xml', function(xml) { // tests for #1419 where IE was a problem equals( $("tab:first", xml).text(), "blabla", "Verify initial text correct" ); $("tab:first", xml).text("newtext"); @@ -269,18 +269,18 @@ test("add(String|Element|Array|undefined)", function() { // For the time being, we're discontinuing support for $(form.elements) since it's ambiguous in IE // use $([]).add(form.elements) instead. //equals( $([]).add($("#form")[0].elements).length, $($("#form")[0].elements).length, "Array in constructor must equals array in add()" ); - + var x = $([]).add($("

xxx

")).add($("

xxx

")); equals( x[0].id, "x1", "Check on-the-fly element1" ); equals( x[1].id, "x2", "Check on-the-fly element2" ); - + var x = $([]).add("

xxx

").add("

xxx

"); equals( x[0].id, "x1", "Check on-the-fly element1" ); equals( x[1].id, "x2", "Check on-the-fly element2" ); - + var notDefined; equals( $([]).add(notDefined).length, 0, "Check that undefined adds nothing" ); - + // Added after #2811 equals( $([]).add([window,document,document.body,document]).length, 3, "Pass an array" ); equals( $(document).add(document).length, 1, "Check duplicated elements" ); @@ -301,10 +301,10 @@ test("each(Function)", function() { test("index(Object)", function() { expect(10); - + var elements = $([window, document]), inputElements = $('#radio1,#radio2,#check1,#check2'); - + equals( elements.index(window), 0, "Check for index of elements" ); equals( elements.index(document), 1, "Check for index of elements" ); equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" ); @@ -313,14 +313,14 @@ test("index(Object)", function() { equals( inputElements.index(document.getElementById('check2')), 3, "Check for index of elements" ); equals( inputElements.index(window), -1, "Check for not found index" ); equals( inputElements.index(document), -1, "Check for not found index" ); - + // enabled since [5500] equals( elements.index( elements ), 0, "Pass in a jQuery object" ); equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" ); }); test("attr(String)", function() { - expect(20); + expect(26); equals( $('#text1').attr('value'), "Test", 'Check for value attribute' ); equals( $('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' ); equals( $('#text1').attr('type'), "text", 'Check for type attribute' ); @@ -340,9 +340,30 @@ test("attr(String)", function() { equals( $('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' ); equals( $('#foo').attr('nodeName'), 'DIV', 'Check for nodeName attribute' ); equals( $('#foo').attr('tagName'), 'DIV', 'Check for tagName attribute' ); - + $('').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path equals( $('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' ); + + + // Related to [5574] and [5683] + var body = document.body, $body = $(body); + + ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' ); + ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' ); + + body.setAttribute('foo', 'baz'); + equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' ); + + body.foo = 'bar'; + equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' ); + + $body.attr('foo','cool'); + equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' ); + + body.foo = undefined; + ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' ); + + body.removeAttribute('foo'); // Cleanup }); if ( !isLocal ) { @@ -384,8 +405,8 @@ test("attr(String, Object)", function() { } equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" ); - ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" ); - + ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" ); + $("#name").attr('name', 'something'); equals( $("#name").attr('name'), 'something', 'Set name attribute' ); $("#check2").attr('checked', true); @@ -443,7 +464,7 @@ if ( !isLocal ) { test("attr(String, Object) - Loaded via XML document", function() { expect(2); stop(); - $.get('data/dashboard.xml', function(xml) { + $.get('data/dashboard.xml', function(xml) { var titles = []; $('tab', xml).each(function() { titles.push($(this).attr('title')); @@ -457,15 +478,15 @@ if ( !isLocal ) { test("css(String|Hash)", function() { expect(19); - + equals( $('#main').css("display"), 'none', 'Check for css property "display"'); - + ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); $('#foo').css({display: 'none'}); ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); $('#foo').css({display: 'block'}); ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); - + $('#floatTest').css({styleFloat: 'right'}); equals( $('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right'); $('#floatTest').css({cssFloat: 'left'}); @@ -474,13 +495,13 @@ test("css(String|Hash)", function() { equals( $('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right'); $('#floatTest').css({'font-size': '30px'}); equals( $('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px'); - + $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) { $('#foo').css({opacity: n}); equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" ); $('#foo').css({opacity: parseFloat(n)}); equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" ); - }); + }); $('#foo').css({opacity: ''}); equals( $('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" ); }); @@ -492,7 +513,7 @@ test("css(String, Object)", function() { ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); $('#foo').css('display', 'block'); ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); - + $('#floatTest').css('styleFloat', 'left'); equals( $('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left'); $('#floatTest').css('cssFloat', 'right'); @@ -501,7 +522,7 @@ test("css(String, Object)", function() { equals( $('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left'); $('#floatTest').css('font-size', '20px'); equals( $('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px'); - + $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) { $('#foo').css('opacity', n); equals( $('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" ); @@ -558,9 +579,9 @@ test("width()", function() { equals($div.width(), 30, "Test padding specified with percent"); $div.hide(); equals($div.width(), 30, "Test hidden div"); - + $div.css({ display: "", border: "", padding: "" }); - + $("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" }); equals($("#nothiddendivchild").width(), 20, "Test child width with border and padding"); $("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" }); @@ -586,7 +607,7 @@ test("height()", function() { equals($div.height(), 30, "Test padding specified with percent"); $div.hide(); equals($div.height(), 30, "Test hidden div"); - + $div.css({ display: "", border: "", padding: "", height: "1px" }); }); @@ -608,10 +629,10 @@ test("wrap(String|Element)", function() { var result = $('#first').wrap(document.getElementById('empty')).parent(); ok( result.is('ol'), 'Check for element wrapping' ); equals( result.text(), defaultText, 'Check for element wrapping' ); - + reset(); - $('#check1').click(function() { - var checkbox = this; + $('#check1').click(function() { + var checkbox = this; ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); $(checkbox).wrap( '' ); ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); @@ -666,17 +687,17 @@ test("append(String|Element|Array<Element>|jQuery)", function() { var result = $('#first').append('buga'); equals( result.text(), defaultText + 'buga', 'Check if text appending works' ); equals( $('#select3').append('').find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element'); - + reset(); var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; $('#sap').append(document.getElementById('first')); equals( expected, $('#sap').text(), "Check for appending of element" ); - + reset(); expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]); equals( expected, $('#sap').text(), "Check for appending of array of elements" ); - + reset(); expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; $('#sap').append($("#first, #yahoo")); @@ -694,7 +715,7 @@ test("append(String|Element|Array<Element>|jQuery)", function() { ok( $("#sap").append([]), "Check for appending an empty array." ); ok( $("#sap").append(""), "Check for appending an empty string." ); ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." ); - + reset(); $("#sap").append(document.getElementById('form')); equals( $("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910 @@ -708,21 +729,21 @@ test("append(String|Element|Array<Element>|jQuery)", function() { } ok( pass, "Test for appending a DOM node to the contents of an IFrame" ); - + reset(); $('
').appendTo('#form').append('test'); t( 'Append legend', '#legend', ['legend'] ); - + reset(); $('#select1').append(''); equals( $('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" ); - + $('#table').append(''); ok( $('#table colgroup').length, "Append colgroup" ); - + $('#table colgroup').append(''); ok( $('#table colgroup col').length, "Append col" ); - + reset(); $('#table').append(''); ok( $('#table caption').length, "Append caption" ); @@ -731,7 +752,7 @@ test("append(String|Element|Array<Element>|jQuery)", function() { $('form:last') .append('') .append(''); - + t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] ); // using contents will get comments regular, text, and comment nodes @@ -750,22 +771,22 @@ test("appendTo(String|Element|Array<Element>|jQuery)", function() { $('buga').appendTo('#first'); equals( $("#first").text(), defaultText + 'buga', 'Check if text appending works' ); equals( $('').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element'); - + reset(); var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; $(document.getElementById('first')).appendTo('#sap'); equals( 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'); equals( 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'); equals( expected, $('#sap').text(), "Check for appending of jQuery object" ); - + reset(); $('#select1').appendTo('#foo'); t( 'Append select', '#foo select', ['select1'] ); @@ -777,7 +798,7 @@ test("prepend(String|Element|Array<Element>|jQuery)", function() { var result = $('#first').prepend('buga'); equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' ); equals( $('#select3').prepend('').find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element'); - + reset(); var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; $('#sap').prepend(document.getElementById('first')); @@ -787,7 +808,7 @@ test("prepend(String|Element|Array<Element>|jQuery)", function() { expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]); equals( expected, $('#sap').text(), "Check for prepending of array of elements" ); - + reset(); expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; $('#sap').prepend($("#first, #yahoo")); @@ -800,7 +821,7 @@ test("prependTo(String|Element|Array<Element>|jQuery)", function() { $('buga').prependTo('#first'); equals( $('#first').text(), 'buga' + defaultText, 'Check if text prepending works' ); equals( $('').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element'); - + reset(); var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; $(document.getElementById('first')).prependTo('#sap'); @@ -810,16 +831,16 @@ test("prependTo(String|Element|Array<Element>|jQuery)", function() { expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap'); equals( 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'); equals( expected, $('#sap').text(), "Check for prepending of jQuery object" ); - + reset(); $('').prependTo('form:last'); $('').prependTo('form:last'); - + t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] ); }); @@ -828,17 +849,17 @@ test("before(String|Element|Array<Element>|jQuery)", function() { var expected = 'This is a normal link: bugaYahoo'; $('#yahoo').before('buga'); equals( expected, $('#en').text(), 'Insert String before' ); - + reset(); expected = "This is a normal link: Try them out:Yahoo"; $('#yahoo').before(document.getElementById('first')); equals( expected, $('#en').text(), "Insert element before" ); - + reset(); expected = "This is a normal link: Try them out:diveintomarkYahoo"; $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]); equals( expected, $('#en').text(), "Insert array of elements before" ); - + reset(); expected = "This is a normal link: Try them out:diveintomarkYahoo"; $('#yahoo').before($("#first, #mark")); @@ -850,17 +871,17 @@ test("insertBefore(String|Element|Array<Element>|jQuery)", function() { var expected = 'This is a normal link: bugaYahoo'; $('buga').insertBefore('#yahoo'); equals( expected, $('#en').text(), 'Insert String before' ); - + reset(); expected = "This is a normal link: Try them out:Yahoo"; $(document.getElementById('first')).insertBefore('#yahoo'); equals( 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'); equals( expected, $('#en').text(), "Insert array of elements before" ); - + reset(); expected = "This is a normal link: Try them out:diveintomarkYahoo"; $("#first, #mark").insertBefore('#yahoo'); @@ -872,7 +893,7 @@ test("after(String|Element|Array<Element>|jQuery)", function() { var expected = 'This is a normal link: Yahoobuga'; $('#yahoo').after('buga'); equals( expected, $('#en').text(), 'Insert String after' ); - + reset(); expected = "This is a normal link: YahooTry them out:"; $('#yahoo').after(document.getElementById('first')); @@ -882,7 +903,7 @@ test("after(String|Element|Array<Element>|jQuery)", function() { expected = "This is a normal link: YahooTry them out:diveintomark"; $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]); equals( expected, $('#en').text(), "Insert array of elements after" ); - + reset(); expected = "This is a normal link: YahooTry them out:diveintomark"; $('#yahoo').after($("#first, #mark")); @@ -894,7 +915,7 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() { var expected = 'This is a normal link: Yahoobuga'; $('buga').insertAfter('#yahoo'); equals( expected, $('#en').text(), 'Insert String after' ); - + reset(); expected = "This is a normal link: YahooTry them out:"; $(document.getElementById('first')).insertAfter('#yahoo'); @@ -904,7 +925,7 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() { expected = "This is a normal link: YahooTry them out:diveintomark"; $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo'); equals( expected, $('#en').text(), "Insert array of elements after" ); - + reset(); expected = "This is a normal link: YahooTry them out:diveintomark"; $("#mark, #first").insertAfter('#yahoo'); @@ -916,7 +937,7 @@ test("replaceWith(String|Element|Array<Element>|jQuery)", function() { $('#yahoo').replaceWith('buga'); ok( $("#replace")[0], 'Replace element with string' ); ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' ); - + reset(); $('#yahoo').replaceWith(document.getElementById('first')); ok( $("#first")[0], 'Replace element with element' ); @@ -927,7 +948,7 @@ test("replaceWith(String|Element|Array<Element>|jQuery)", function() { ok( $("#first")[0], 'Replace element with array of elements' ); ok( $("#mark")[0], 'Replace element with array of elements' ); ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' ); - + reset(); $('#yahoo').replaceWith($("#first, #mark")); ok( $("#first")[0], 'Replace element with set of elements' ); @@ -940,7 +961,7 @@ test("replaceAll(String|Element|Array<Element>|jQuery)", function() { $('buga').replaceAll("#yahoo"); ok( $("#replace")[0], 'Replace element with string' ); ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' ); - + reset(); $(document.getElementById('first')).replaceAll("#yahoo"); ok( $("#first")[0], 'Replace element with element' ); @@ -951,7 +972,7 @@ test("replaceAll(String|Element|Array<Element>|jQuery)", function() { ok( $("#first")[0], 'Replace element with array of elements' ); ok( $("#mark")[0], 'Replace element with array of elements' ); ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' ); - + reset(); $("#first, #mark").replaceAll("#yahoo"); ok( $("#first")[0], 'Replace element with set of elements' ); @@ -963,7 +984,7 @@ test("end()", function() { expect(3); equals( 'Yahoo', $('#yahoo').parent().end().text(), 'Check for end' ); ok( $('#yahoo').end(), 'Check for end with nothing to end' ); - + var x = $('#yahoo'); x.parent(); equals( 'Yahoo', $('#yahoo').text(), 'Check for non-destructive behaviour' ); @@ -985,8 +1006,8 @@ test("clone()", function() { equals( 'Try them out:Yahoo', $('#first').append(clone).text(), 'Check for clone' ); equals( 'This is a normal link: Yahoo', $('#en').text(), 'Reassert text for #en' ); - var cloneTags = [ - "", "", "
", "
", + var cloneTags = [ + "", "", "
", "
", "