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(String, Function)", function() {
\r
71 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
\r
72 ok( $('#text2').attr('value', "${this.id}")[0].value == "text2", "Set value from id" );
\r
75 test("attr(Hash)", function() {
\r
78 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
\r
79 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
\r
81 ok( pass, "Set Multiple Attributes" );
\r
84 test("attr(String, Object)", function() {
\r
87 div.attr("foo", "bar");
\r
89 for ( var i = 0; i < div.size(); i++ ) {
\r
90 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
\r
92 ok( pass, "Set Attribute" );
\r
94 $("#name").attr('name', 'something');
\r
95 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
\r
96 $("#check2").attr('checked', true);
\r
97 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
\r
98 $("#check2").attr('checked', false);
\r
99 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
\r
100 $("#text1").attr('readonly', true);
\r
101 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
\r
102 $("#text1").attr('readonly', false);
\r
103 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
\r
106 test("attr(String, Object)x", function() {
\r
109 $.get('data/dashboard.xml', function(xml) {
\r
111 $('tab', xml).each(function() {
\r
112 titles.push($(this).attr('title'));
\r
114 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
\r
115 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
\r
120 test("css(String|Hash)", function() {
\r
123 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
\r
125 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
\r
126 $('#foo').css({display: 'none'});
\r
127 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
\r
128 $('#foo').css({display: 'block'});
\r
129 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
\r
131 $('#floatTest').css({styleFloat: 'right'});
\r
132 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
\r
133 $('#floatTest').css({cssFloat: 'left'});
\r
134 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
\r
135 $('#floatTest').css({'float': 'right'});
\r
136 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
\r
137 $('#floatTest').css({'font-size': '30px'});
\r
138 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
\r
141 test("css(String, Object)", function() {
\r
143 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
\r
144 $('#foo').css('display', 'none');
\r
145 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
\r
146 $('#foo').css('display', 'block');
\r
147 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
\r
149 $('#floatTest').css('styleFloat', 'left');
\r
150 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
\r
151 $('#floatTest').css('cssFloat', 'right');
\r
152 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
\r
153 $('#floatTest').css('float', 'left');
\r
154 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
\r
155 $('#floatTest').css('font-size', '20px');
\r
156 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
\r
159 test("text()", function() {
\r
160 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
\r
161 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
\r
164 test("wrap(String|Element)", function() {
\r
166 var defaultText = 'Try them out:'
\r
167 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
\r
168 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
\r
169 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
\r
172 var defaultText = 'Try them out:'
\r
173 var result = $('#first').wrap(document.getElementById('empty')).parent();
\r
174 ok( result.is('ol'), 'Check for element wrapping' );
\r
175 ok( result.text() == defaultText, 'Check for element wrapping' );
\r
178 test("append(String|Element|Array<Element>|jQuery)", function() {
\r
180 var defaultText = 'Try them out:'
\r
181 var result = $('#first').append('<b>buga</b>');
\r
182 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
\r
183 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
\r
186 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
\r
187 $('#sap').append(document.getElementById('first'));
\r
188 ok( expected == $('#sap').text(), "Check for appending of element" );
\r
191 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
\r
192 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
\r
193 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
\r
196 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
\r
197 $('#sap').append($("#first, #yahoo"));
\r
198 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
\r
201 test("prepend(String|Element|Array<Element>|jQuery)", function() {
\r
203 var defaultText = 'Try them out:'
\r
204 var result = $('#first').prepend('<b>buga</b>');
\r
205 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
\r
206 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
\r
209 expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
\r
210 $('#sap').prepend(document.getElementById('first'));
\r
211 ok( expected == $('#sap').text(), "Check for prepending of element" );
\r
214 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
215 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
\r
216 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
\r
219 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
220 $('#sap').prepend($("#first, #yahoo"));
\r
221 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
\r
224 test("before(String|Element|Array<Element>|jQuery)", function() {
\r
226 var expected = 'This is a normal link: bugaYahoo';
\r
227 $('#yahoo').before('<b>buga</b>');
\r
228 ok( expected == $('#en').text(), 'Insert String before' );
\r
231 expected = "This is a normal link: Try them out:Yahoo";
\r
232 $('#yahoo').before(document.getElementById('first'));
\r
233 ok( expected == $('#en').text(), "Insert element before" );
\r
236 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
237 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
\r
238 ok( expected == $('#en').text(), "Insert array of elements before" );
\r
241 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
242 $('#yahoo').before($("#first, #mark"));
\r
243 ok( expected == $('#en').text(), "Insert jQuery before" );
\r
246 test("after(String|Element|Array<Element>|jQuery)", function() {
\r
248 var expected = 'This is a normal link: Yahoobuga';
\r
249 $('#yahoo').after('<b>buga</b>');
\r
250 ok( expected == $('#en').text(), 'Insert String after' );
\r
253 expected = "This is a normal link: YahooTry them out:";
\r
254 $('#yahoo').after(document.getElementById('first'));
\r
255 ok( expected == $('#en').text(), "Insert element after" );
\r
258 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
259 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
\r
260 ok( expected == $('#en').text(), "Insert array of elements after" );
\r
263 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
264 $('#yahoo').after($("#first, #mark"));
\r
265 ok( expected == $('#en').text(), "Insert jQuery after" );
\r
268 test("end()", function() {
\r
270 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
\r
271 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
\r
274 test("find(String)", function() {
\r
275 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
\r
278 test("clone()", function() {
\r
280 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
\r
281 var clone = $('#yahoo').clone();
\r
282 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
\r
283 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
\r
286 test("filter()", function() {
\r
287 isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
\r
288 isSet( $("p").filter(["#ap", "#sndp"]).get(), q("ap", "sndp"), "filter(Array<String>)" );
\r
289 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
\r
292 test("not(String)", function() {
\r
293 ok($("#main > p#ap > a").not("#google").length == 2, "not('selector')")
\r
294 // TODO: Comma-based selector
\r
295 //isSet( $("p").not("#ap, #sndp").get(), q("firstp", "en", "sap", "first", "result"), "not('selector, selector')" );
\r
298 test("is(String)", function() {
\r
300 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
\r
301 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
\r
302 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
\r
303 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
\r
304 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
\r
305 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
\r
306 ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
\r
307 ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
\r
308 ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
\r
309 ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
\r
310 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
\r
311 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
\r
312 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
\r
313 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
\r
314 ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
\r
315 ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
\r
316 ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
\r
317 ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
\r
318 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
\r
319 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
\r
320 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
\r
321 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
\r
324 test("$.extend(Object, Object)", function() {
\r
326 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
327 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
\r
328 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
\r
329 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
\r
330 jQuery.extend(settings, options);
\r
331 isSet( settings, merged, "Check if extended: settings must be extended" );
\r
332 isSet ( options, optionsCopy, "Check if not modified: options must not be modified" );
\r
335 test("$.extend(Object, Object, Object, Object)", function() {
\r
337 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
338 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
339 options1 = { xnumber2: 1, xstring2: "x" },
\r
340 options1Copy = { xnumber2: 1, xstring2: "x" },
\r
341 options2 = { xstring2: "xx", xxx: "newstringx" },
\r
342 options2Copy = { xstring2: "xx", xxx: "newstringx" },
\r
343 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
\r
344 var settings = jQuery.extend({}, defaults, options1, options2);
\r
345 isSet( settings, merged, "Check if extended: settings must be extended" );
\r
346 isSet ( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
\r
347 isSet ( options1, options1Copy, "Check if not modified: options1 must not be modified" );
\r
348 isSet ( options2, options2Copy, "Check if not modified: options2 must not be modified" );
\r
351 test("val()", function() {
\r
353 ok( $("#text1").val() == "Test", "Check for value of input element" );
\r
354 ok( !$("#text1").val() == "", "Check for value of input element" );
\r
357 test("val(String)", function() {
\r
359 document.getElementById('text1').value = "bla";
\r
360 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
\r
361 $("#text1").val('test');
\r
362 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
\r
365 test("html(String)", function() {
\r
367 var div = $("div");
\r
368 div.html("<b>test</b>");
\r
370 for ( var i = 0; i < div.size(); i++ ) {
\r
371 if ( div.get(i).childNodes.length == 0 ) pass = false;
\r
373 ok( pass, "Set HTML" );
\r
376 test("siblings([String])", function() {
\r
378 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
\r
379 isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" );
\r
380 isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
\r
383 test("children([String])", function() {
\r
385 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
\r
386 isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
\r
390 test("show()", function() {
\r
392 var pass = true, div = $("div");
\r
393 div.show().each(function(){
\r
394 if ( this.style.display == "none" ) pass = false;
\r
396 ok( pass, "Show" );
\r
399 test("addClass(String)", function() {
\r
400 var div = $("div");
\r
401 div.addClass("test");
\r
403 for ( var i = 0; i < div.size(); i++ ) {
\r
404 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
\r
406 ok( pass, "Add Class" );
\r
409 test("removeClass(String) - simple", function() {
\r
411 var div = $("div").addClass("test").removeClass("test"),
\r
413 for ( var i = 0; i < div.size(); i++ ) {
\r
414 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
\r
416 ok( pass, "Remove Class" );
\r
419 test("removeClass(String) - add three classes and remove again", function() {
\r
421 var div = $("div").addClass("test").addClass("foo").addClass("bar");
\r
422 div.removeClass("test").removeClass("bar").removeClass("foo");
\r
424 for ( var i = 0; i < div.size(); i++ ) {
\r
425 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
\r
427 ok( pass, "Remove multiple classes" );
\r
430 test("removeAttr(String", function() {
\r
431 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
\r