3 test("Basic requirements", function() {
\r
5 ok( Array.prototype.push, "Array.push()" );
\r
6 ok( Function.prototype.apply, "Function.apply()" );
\r
7 ok( document.getElementById, "getElementById" );
\r
8 ok( document.getElementsByTagName, "getElementsByTagName" );
\r
9 ok( RegExp, "RegExp" );
\r
10 ok( jQuery, "jQuery" );
\r
14 test("length", function() {
\r
15 ok( $("div").length == 2, "Get Number of Elements Found" );
\r
18 test("size()", function() {
\r
19 ok( $("div").size() == 2, "Get Number of Elements Found" );
\r
22 test("get()", function() {
\r
23 isSet( $("div").get(), q("main","foo"), "Get All Elements" );
\r
26 test("get(Number)", function() {
\r
27 ok( $("div").get(0) == document.getElementById("main"), "Get A Single Element" );
\r
30 test("each(Function)", function() {
\r
33 div.each(function(){this.foo = 'zoo';});
\r
35 for ( var i = 0; i < div.size(); i++ ) {
\r
36 if ( div.get(i).foo != "zoo" ) pass = false;
\r
38 ok( pass, "Execute a function, Relative" );
\r
41 test("index(Object)", function() {
\r
43 ok( $([window, document]).index(window) == 0, "Check for index of elements" );
\r
44 ok( $([window, document]).index(document) == 1, "Check for index of elements" );
\r
45 var inputElements = $('#radio1,#radio2,#check1,#check2');
\r
46 ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
\r
47 ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
\r
48 ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
\r
49 ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
\r
50 ok( inputElements.index(window) == -1, "Check for not found index" );
\r
51 ok( inputElements.index(document) == -1, "Check for not found index" );
\r
54 test("attr(String)", function() {
\r
56 ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
\r
57 ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
\r
58 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
\r
59 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
\r
60 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
\r
61 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
\r
62 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
\r
63 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
\r
64 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
\r
65 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
\r
66 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
\r
67 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
\r
70 test("attr(Hash)", function() {
\r
73 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
\r
74 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
\r
76 ok( pass, "Set Multiple Attributes" );
\r
79 test("attr(String, Object)", function() {
\r
82 div.attr("foo", "bar");
\r
84 for ( var i = 0; i < div.size(); i++ ) {
\r
85 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
\r
87 ok( pass, "Set Attribute" );
\r
89 $("#name").attr('name', 'something');
\r
90 ok( $("#name").name() == 'something', 'Set name attribute' );
\r
91 $("#check2").attr('checked', true);
\r
92 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
\r
93 $("#check2").attr('checked', false);
\r
94 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
\r
95 $("#text1").attr('readonly', true);
\r
96 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
\r
97 $("#text1").attr('readonly', false);
\r
98 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
\r
101 test("attr(String, Object)x", function() {
\r
104 $.get('data/dashboard.xml', function(xml) {
\r
106 $('tab', xml).each(function() {
\r
107 titles.push($(this).attr('title'));
\r
109 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
\r
110 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
\r
115 test("css(String|Hash)", function() {
\r
118 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
\r
120 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
\r
121 $('#foo').css({display: 'none'});
\r
122 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
\r
123 $('#foo').css({display: 'block'});
\r
124 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
\r
126 $('#floatTest').css({styleFloat: 'right'});
\r
127 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
\r
128 $('#floatTest').css({cssFloat: 'left'});
\r
129 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
\r
130 $('#floatTest').css({'float': 'right'});
\r
131 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
\r
132 $('#floatTest').css({'font-size': '30px'});
\r
133 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
\r
136 test("css(String, Object)", function() {
\r
138 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
\r
139 $('#foo').css('display', 'none');
\r
140 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
\r
141 $('#foo').css('display', 'block');
\r
142 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
\r
144 $('#floatTest').css('styleFloat', 'left');
\r
145 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
\r
146 $('#floatTest').css('cssFloat', 'right');
\r
147 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
\r
148 $('#floatTest').css('float', 'left');
\r
149 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
\r
150 $('#floatTest').css('font-size', '20px');
\r
151 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
\r
154 test("text()", function() {
\r
155 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
\r
156 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
\r
159 test("wrap(String|Element)", function() {
\r
161 var defaultText = 'Try them out:'
\r
162 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
\r
163 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
\r
164 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
\r
167 var defaultText = 'Try them out:'
\r
168 var result = $('#first').wrap(document.getElementById('empty')).parent();
\r
169 ok( result.is('ol'), 'Check for element wrapping' );
\r
170 ok( result.text() == defaultText, 'Check for element wrapping' );
\r
173 test("append(String|Element|Array<Element>|jQuery)", function() {
\r
175 var defaultText = 'Try them out:'
\r
176 var result = $('#first').append('<b>buga</b>');
\r
177 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
\r
178 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
\r
181 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
\r
182 $('#sap').append(document.getElementById('first'));
\r
183 ok( expected == $('#sap').text(), "Check for appending of element" );
\r
186 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
\r
187 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
\r
188 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
\r
191 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
\r
192 $('#sap').append($("#first, #yahoo"));
\r
193 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
\r
196 test("prepend(String|Element|Array<Element>|jQuery)", function() {
\r
198 var defaultText = 'Try them out:'
\r
199 var result = $('#first').prepend('<b>buga</b>');
\r
200 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
\r
201 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
\r
204 expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
\r
205 $('#sap').prepend(document.getElementById('first'));
\r
206 ok( expected == $('#sap').text(), "Check for prepending of element" );
\r
209 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
210 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
\r
211 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
\r
214 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
215 $('#sap').prepend($("#first, #yahoo"));
\r
216 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
\r
219 test("before(String|Element|Array<Element>|jQuery)", function() {
\r
221 var expected = 'This is a normal link: bugaYahoo';
\r
222 $('#yahoo').before('<b>buga</b>');
\r
223 ok( expected == $('#en').text(), 'Insert String before' );
\r
226 expected = "This is a normal link: Try them out:Yahoo";
\r
227 $('#yahoo').before(document.getElementById('first'));
\r
228 ok( expected == $('#en').text(), "Insert element before" );
\r
231 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
232 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
\r
233 ok( expected == $('#en').text(), "Insert array of elements before" );
\r
236 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
237 $('#yahoo').before($("#first, #mark"));
\r
238 ok( expected == $('#en').text(), "Insert jQuery before" );
\r
241 test("after(String|Element|Array<Element>|jQuery)", function() {
\r
243 var expected = 'This is a normal link: Yahoobuga';
\r
244 $('#yahoo').after('<b>buga</b>');
\r
245 ok( expected == $('#en').text(), 'Insert String after' );
\r
248 expected = "This is a normal link: YahooTry them out:";
\r
249 $('#yahoo').after(document.getElementById('first'));
\r
250 ok( expected == $('#en').text(), "Insert element after" );
\r
253 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
254 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
\r
255 ok( expected == $('#en').text(), "Insert array of elements after" );
\r
258 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
259 $('#yahoo').after($("#first, #mark"));
\r
260 ok( expected == $('#en').text(), "Insert jQuery after" );
\r
263 test("end()", function() {
\r
265 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
\r
266 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
\r
269 test("find(String)", function() {
\r
270 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
\r
273 test("clone()", function() {
\r
275 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
\r
276 var clone = $('#yahoo').clone();
\r
277 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
\r
278 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
\r
281 test("filter()", function() {
\r
282 isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
\r
283 isSet( $("p").filter(["#ap", "#sndp"]).get(), q("ap", "sndp"), "filter(Array<String>)" );
\r
284 isSet( $("p").filter(function(el) { return !$("a", el).length }).get(), q("sndp", "first"), "filter(Function)" );
\r
287 test("not(String)", function() {
\r
288 ok($("#main > p#ap > a").not("#google").length == 2, "not('selector')")
\r
289 // TODO: Comma-based selector
\r
290 //isSet( $("p").not("#ap, #sndp").get(), q("firstp", "en", "sap", "first", "result"), "not('selector, selector')" );
\r
293 test("is(String)", function() {
\r
295 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
\r
296 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
\r
297 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
\r
298 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
\r
299 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
\r
300 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
\r
301 ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
\r
302 ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
\r
303 ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
\r
304 ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
\r
305 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
\r
306 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
\r
307 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
\r
308 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
\r
309 ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
\r
310 ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
\r
311 ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
\r
312 ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
\r
313 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
\r
314 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
\r
315 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
\r
316 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
\r
319 test("$.extend(Object, Object)", function() {
\r
321 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
322 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
\r
323 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
\r
324 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
\r
325 jQuery.extend(settings, options);
\r
326 isSet( settings, merged, "Check if extended: settings must be extended" );
\r
327 isSet ( options, optionsCopy, "Check if not modified: options must not be modified" );
\r
330 test("$.extend(Object, Object, Object, Object)", function() {
\r
332 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
333 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
334 options1 = { xnumber2: 1, xstring2: "x" },
\r
335 options1Copy = { xnumber2: 1, xstring2: "x" },
\r
336 options2 = { xstring2: "xx", xxx: "newstringx" },
\r
337 options2Copy = { xstring2: "xx", xxx: "newstringx" },
\r
338 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
\r
339 var settings = jQuery.extend({}, defaults, options1, options2);
\r
340 isSet( settings, merged, "Check if extended: settings must be extended" );
\r
341 isSet ( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
\r
342 isSet ( options1, options1Copy, "Check if not modified: options1 must not be modified" );
\r
343 isSet ( options2, options2Copy, "Check if not modified: options2 must not be modified" );
\r
346 test("val()", function() {
\r
348 ok( $("#text1").val() == "Test", "Check for value of input element" );
\r
349 ok( !$("#text1").val() == "", "Check for value of input element" );
\r
352 test("val(String)", function() {
\r
354 document.getElementById('text1').value = "bla";
\r
355 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
\r
356 $("#text1").val('test');
\r
357 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
\r
360 test("html(String)", function() {
\r
362 var div = $("div");
\r
363 div.html("<b>test</b>");
\r
365 for ( var i = 0; i < div.size(); i++ ) {
\r
366 if ( div.get(i).childNodes.length == 0 ) pass = false;
\r
368 ok( pass, "Set HTML" );
\r
371 test("id()", function() {
\r
373 ok( $(document.getElementById('main')).id() == "main", "Check for id" );
\r
374 ok( $("#foo").id() == "foo", "Check for id" );
\r
375 ok( !$("head").id(), "Check for id" );
\r
378 test("title()", function() {
\r
380 ok( $(document.getElementById('google')).title() == "Google!", "Check for title" );
\r
381 ok( !$("#yahoo").title(), "Check for title" );
\r
384 test("name()", function() {
\r
386 ok( $(document.getElementById('text1')).name() == "action", "Check for name" );
\r
387 ok( $("#hidden1").name() == "hidden", "Check for name" );
\r
388 ok( !$("#area1").name(), "Check for name" );
\r
392 test("siblings([String])", function() {
\r
394 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
\r
395 isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" );
\r
396 isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
\r
399 test("children([String])", function() {
\r
401 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
\r
402 isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
\r
406 test("show()", function() {
\r
408 var pass = true, div = $("div");
\r
409 div.show().each(function(){
\r
410 if ( this.style.display == "none" ) pass = false;
\r
412 ok( pass, "Show" );
\r
415 test("addClass(String)", function() {
\r
416 var div = $("div");
\r
417 div.addClass("test");
\r
419 for ( var i = 0; i < div.size(); i++ ) {
\r
420 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
\r
422 ok( pass, "Add Class" );
\r
425 test("removeClass(String) - simple", function() {
\r
427 var div = $("div").addClass("test").removeClass("test"),
\r
429 for ( var i = 0; i < div.size(); i++ ) {
\r
430 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
\r
432 ok( pass, "Remove Class" );
\r
435 test("removeClass(String) - add three classes and remove again", function() {
\r
437 var div = $("div").addClass("test").addClass("foo").addClass("bar");
\r
438 div.removeClass("test").removeClass("bar").removeClass("foo");
\r
440 for ( var i = 0; i < div.size(); i++ ) {
\r
441 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
\r
443 ok( pass, "Remove multiple classes" );
\r
446 test("removeAttr(String", function() {
\r
447 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
\r