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>)", 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 test("prepend(String|Element|Array<Element>)", function() {
\r
193 var defaultText = 'Try them out:'
\r
194 var result = $('#first').prepend('<b>buga</b>');
\r
195 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
\r
196 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
\r
199 expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
\r
200 $('#sap').prepend(document.getElementById('first'));
\r
201 ok( expected == $('#sap').text(), "Check for prepending of element" );
\r
204 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
205 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
\r
206 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
\r
209 test("before(String|Element|Array<Element>)", function() {
\r
211 var expected = 'This is a normal link: bugaYahoo';
\r
212 $('#yahoo').before('<b>buga</b>');
\r
213 ok( expected == $('#en').text(), 'Insert String before' );
\r
216 expected = "This is a normal link: Try them out:Yahoo";
\r
217 $('#yahoo').before(document.getElementById('first'));
\r
218 ok( expected == $('#en').text(), "Insert element before" );
\r
221 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
222 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
\r
223 ok( expected == $('#en').text(), "Insert array of elements before" );
\r
226 test("after(String|Element|Array<Element>)", function() {
\r
228 var expected = 'This is a normal link: Yahoobuga';
\r
229 $('#yahoo').after('<b>buga</b>');
\r
230 ok( expected == $('#en').text(), 'Insert String after' );
\r
233 expected = "This is a normal link: YahooTry them out:";
\r
234 $('#yahoo').after(document.getElementById('first'));
\r
235 ok( expected == $('#en').text(), "Insert element after" );
\r
238 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
239 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
\r
240 ok( expected == $('#en').text(), "Insert array of elements after" );
\r
243 test("end()", function() {
\r
245 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
\r
246 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
\r
249 test("find(String)", function() {
\r
250 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
\r
253 test("clone()", function() {
\r
255 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
\r
256 var clone = $('#yahoo').clone();
\r
257 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
\r
258 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
\r
261 test("filter()", function() {
\r
262 isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
\r
263 isSet( $("p").filter(["#ap", "#sndp"]).get(), q("ap", "sndp"), "filter(Array<String>)" );
\r
264 isSet( $("p").filter(function(el) { return !$("a", el).length }).get(), q("sndp", "first"), "filter(Function)" );
\r
267 test("not(String)", function() {
\r
268 ok($("#main > p#ap > a").not("#google").length == 2, "not('selector')")
\r
269 // TODO: Comma-based selector
\r
270 //isSet( $("p").not("#ap, #sndp").get(), q("firstp", "en", "sap", "first", "result"), "not('selector, selector')" );
\r
273 test("is(String)", function() {
\r
275 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
\r
276 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
\r
277 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
\r
278 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
\r
279 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
\r
280 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
\r
281 ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
\r
282 ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
\r
283 ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
\r
284 ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
\r
285 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
\r
286 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
\r
287 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
\r
288 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
\r
289 ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
\r
290 ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
\r
291 ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
\r
292 ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
\r
293 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
\r
294 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
\r
295 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
\r
296 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
\r
299 test("$.extend(Object, Object)", function() {
\r
301 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
302 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
\r
303 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
\r
304 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
\r
305 jQuery.extend(settings, options);
\r
306 isSet( settings, merged, "Check if extended: settings must be extended" );
\r
307 isSet ( options, optionsCopy, "Check if not modified: options must not be modified" );
\r
310 test("$.extend(Object, Object, Object, Object)", function() {
\r
312 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
313 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
314 options1 = { xnumber2: 1, xstring2: "x" },
\r
315 options1Copy = { xnumber2: 1, xstring2: "x" },
\r
316 options2 = { xstring2: "xx", xxx: "newstringx" },
\r
317 options2Copy = { xstring2: "xx", xxx: "newstringx" },
\r
318 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
\r
319 var settings = jQuery.extend({}, defaults, options1, options2);
\r
320 isSet( settings, merged, "Check if extended: settings must be extended" );
\r
321 isSet ( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
\r
322 isSet ( options1, options1Copy, "Check if not modified: options1 must not be modified" );
\r
323 isSet ( options2, options2Copy, "Check if not modified: options2 must not be modified" );
\r
326 test("val()", function() {
\r
328 ok( $("#text1").val() == "Test", "Check for value of input element" );
\r
329 ok( !$("#text1").val() == "", "Check for value of input element" );
\r
332 test("val(String)", function() {
\r
334 document.getElementById('text1').value = "bla";
\r
335 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
\r
336 $("#text1").val('test');
\r
337 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
\r
340 test("html(String)", function() {
\r
342 var div = $("div");
\r
343 div.html("<b>test</b>");
\r
345 for ( var i = 0; i < div.size(); i++ ) {
\r
346 if ( div.get(i).childNodes.length == 0 ) pass = false;
\r
348 ok( pass, "Set HTML" );
\r
351 test("id()", function() {
\r
353 ok( $(document.getElementById('main')).id() == "main", "Check for id" );
\r
354 ok( $("#foo").id() == "foo", "Check for id" );
\r
355 ok( !$("head").id(), "Check for id" );
\r
358 test("title()", function() {
\r
360 ok( $(document.getElementById('google')).title() == "Google!", "Check for title" );
\r
361 ok( !$("#yahoo").title(), "Check for title" );
\r
364 test("name()", function() {
\r
366 ok( $(document.getElementById('text1')).name() == "action", "Check for name" );
\r
367 ok( $("#hidden1").name() == "hidden", "Check for name" );
\r
368 ok( !$("#area1").name(), "Check for name" );
\r
372 test("siblings([String])", function() {
\r
374 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
\r
375 isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" );
\r
376 isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
\r
379 test("children([String])", function() {
\r
381 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
\r
382 isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
\r
386 test("show()", function() {
\r
388 var pass = true, div = $("div");
\r
389 div.show().each(function(){
\r
390 if ( this.style.display == "none" ) pass = false;
\r
392 ok( pass, "Show" );
\r
395 test("addClass(String)", function() {
\r
396 var div = $("div");
\r
397 div.addClass("test");
\r
399 for ( var i = 0; i < div.size(); i++ ) {
\r
400 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
\r
402 ok( pass, "Add Class" );
\r
405 test("removeClass(String) - simple", function() {
\r
407 var div = $("div").addClass("test").removeClass("test"),
\r
409 for ( var i = 0; i < div.size(); i++ ) {
\r
410 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
\r
412 ok( pass, "Remove Class" );
\r
415 test("removeClass(String) - add three classes and remove again", function() {
\r
417 var div = $("div").addClass("test").addClass("foo").addClass("bar");
\r
418 div.removeClass("test").removeClass("bar").removeClass("foo");
\r
420 for ( var i = 0; i < div.size(); i++ ) {
\r
421 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
\r
423 ok( pass, "Remove multiple classes" );
\r
426 test("removeAttr(String", function() {
\r
427 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
\r