rob@77
|
1 var testCase = require('nodeunit').testCase,
|
rob@77
|
2 jsdom = require('jsdom').jsdom,
|
rob@77
|
3 static_document = require('fs').readFileSync('test/fixtures/core.html', 'utf8');
|
rob@77
|
4
|
rob@77
|
5 // need to be global as helpers access these variables
|
rob@77
|
6 window = document = jQuery = $ = null;
|
rob@77
|
7
|
rob@77
|
8 var helpers = require('./helpers/helper'),
|
rob@77
|
9 q = helpers.query_ids;
|
rob@77
|
10
|
rob@77
|
11 module.exports = testCase({
|
rob@77
|
12 setUp: function (callback) {
|
rob@77
|
13 jQuery = $ = helpers.recreate_doc(static_document);
|
rob@77
|
14 callback();
|
rob@77
|
15 },
|
rob@77
|
16 tearDown: function (callback) {
|
rob@77
|
17 // clean up
|
rob@77
|
18 callback();
|
rob@77
|
19 },
|
rob@77
|
20 "Basic requirements": function(test) {
|
rob@77
|
21 test.expect(7);
|
rob@77
|
22 test.ok( Array.prototype.push, "Array.push()" );
|
rob@77
|
23 test.ok( Function.prototype.apply, "Function.apply()" );
|
rob@77
|
24 test.ok( document.getElementById, "getElementById" );
|
rob@77
|
25 test.ok( document.getElementsByTagName, "getElementsByTagName" );
|
rob@77
|
26 test.ok( RegExp, "RegExp" );
|
rob@77
|
27 test.ok( jQuery, "jQuery" );
|
rob@77
|
28 test.ok( $, "$" );
|
rob@77
|
29 test.done();
|
rob@77
|
30 },
|
rob@77
|
31 "jQuery()": function(test) {
|
rob@77
|
32 test.expect(24);
|
rob@77
|
33 // Basic constructor's behavior
|
rob@77
|
34
|
rob@77
|
35 test.equals( jQuery().length, 0, "jQuery() === jQuery([])" );
|
rob@77
|
36 test.equals( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
|
rob@77
|
37 test.equals( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
|
rob@77
|
38 test.equals( jQuery("").length, 0, "jQuery('') === jQuery([])" );
|
rob@77
|
39
|
rob@77
|
40 var obj = jQuery("div");
|
rob@77
|
41 test.equals( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
|
rob@77
|
42
|
rob@77
|
43 // can actually yield more than one, when iframes are included, the window is an array as well
|
rob@77
|
44 test.equals( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
|
rob@77
|
45
|
rob@77
|
46
|
rob@77
|
47 var main = jQuery("#main");
|
rob@77
|
48 test.same( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
|
rob@77
|
49
|
rob@77
|
50 /*
|
rob@77
|
51 // disabled since this test was doing nothing. i tried to fix it but i'm not sure
|
rob@77
|
52 // what the expected behavior should even be. FF returns "\n" for the text node
|
rob@77
|
53 // make sure this is handled
|
rob@77
|
54 var crlfContainer = jQuery('<p>\r\n</p>');
|
rob@77
|
55 var x = crlfContainer.contents().get(0).nodeValue;
|
rob@77
|
56 equals( x, what???, "Check for \\r and \\n in jQuery()" );
|
rob@77
|
57 */
|
rob@77
|
58
|
rob@77
|
59 /* // Disabled until we add this functionality in
|
rob@77
|
60 var pass = true;
|
rob@77
|
61 try {
|
rob@77
|
62 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
|
rob@77
|
63 } catch(e){
|
rob@77
|
64 pass = false;
|
rob@77
|
65 }
|
rob@77
|
66 ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
|
rob@77
|
67
|
rob@77
|
68 var code = jQuery("<code/>");
|
rob@77
|
69 test.equals( code.length, 1, "Correct number of elements generated for code" );
|
rob@77
|
70 test.equals( code.parent().length, 0, "Make sure that the generated HTML has no parent." );
|
rob@77
|
71 var img = jQuery("<img/>");
|
rob@77
|
72 test.equals( img.length, 1, "Correct number of elements generated for img" );
|
rob@77
|
73 test.equals( img.parent().length, 0, "Make sure that the generated HTML has no parent." );
|
rob@77
|
74 var div = jQuery("<div/><hr/><code/><b/>");
|
rob@77
|
75 test.equals( div.length, 4, "Correct number of elements generated for div hr code b" );
|
rob@77
|
76 test.equals( div.parent().length, 0, "Make sure that the generated HTML has no parent." );
|
rob@77
|
77
|
rob@77
|
78 test.equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
|
rob@77
|
79
|
rob@77
|
80 test.equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
|
rob@77
|
81
|
rob@77
|
82 var exec = false;
|
rob@77
|
83
|
rob@77
|
84 var elem = jQuery("<div/>", {
|
rob@77
|
85 width: 10,
|
rob@77
|
86 css: { paddingLeft:1, paddingRight:1 },
|
rob@77
|
87 click: function(){ test.ok(exec, "Click executed."); },
|
rob@77
|
88 text: "test",
|
rob@77
|
89 "class": "test2",
|
rob@77
|
90 id: "test3"
|
rob@77
|
91 });
|
rob@77
|
92
|
rob@77
|
93 test.equals( elem[0].style.width, '10px', 'jQuery() quick setter width');
|
rob@77
|
94 test.equals( elem[0].style.paddingLeft, '1px', 'jQuery quick setter css');
|
rob@77
|
95 test.equals( elem[0].style.paddingRight, '1px', 'jQuery quick setter css');
|
rob@77
|
96 test.equals( elem[0].childNodes.length, 1, 'jQuery quick setter text');
|
rob@77
|
97 test.equals( elem[0].firstChild.nodeValue, "test", 'jQuery quick setter text');
|
rob@77
|
98 test.equals( elem[0].className, "test2", 'jQuery() quick setter class');
|
rob@77
|
99 test.equals( elem[0].id, "test3", 'jQuery() quick setter id');
|
rob@77
|
100
|
rob@77
|
101 exec = true;
|
rob@77
|
102 elem.click();
|
rob@77
|
103
|
rob@77
|
104 // manually clean up detached elements
|
rob@77
|
105 elem.remove();
|
rob@77
|
106
|
rob@77
|
107 for ( var i = 0; i < 3; ++i ) {
|
rob@77
|
108 elem = jQuery("<input type='text' value='TEST' />");
|
rob@77
|
109 }
|
rob@77
|
110 test.equals( elem[0].defaultValue, "TEST", "Ensure cached nodes are cloned properly (Bug #6655)" );
|
rob@77
|
111
|
rob@77
|
112 // manually clean up detached elements
|
rob@77
|
113 elem.remove();
|
rob@77
|
114 test.done();
|
rob@77
|
115 },
|
rob@77
|
116 "selector state": function(test) {
|
rob@77
|
117 test.expect(31);
|
rob@77
|
118
|
rob@77
|
119 var elem;
|
rob@77
|
120
|
rob@77
|
121 elem = jQuery(undefined);
|
rob@77
|
122 test.equals( elem.selector, "", "Empty jQuery Selector" );
|
rob@77
|
123 test.equals( elem.context, undefined, "Empty jQuery Context" );
|
rob@77
|
124
|
rob@77
|
125 elem = jQuery(document);
|
rob@77
|
126 test.equals( elem.selector, "", "Document Selector" );
|
rob@77
|
127 test.equals( elem.context, document, "Document Context" );
|
rob@77
|
128
|
rob@77
|
129 elem = jQuery(document.body);
|
rob@77
|
130 test.equals( elem.selector, "", "Body Selector" );
|
rob@77
|
131 test.equals( elem.context, document.body, "Body Context" );
|
rob@77
|
132
|
rob@77
|
133 elem = jQuery("#main");
|
rob@77
|
134 test.equals( elem.selector, "#main", "#main Selector" );
|
rob@77
|
135 test.equals( elem.context, document, "#main Context" );
|
rob@77
|
136
|
rob@77
|
137 elem = jQuery("#notfoundnono");
|
rob@77
|
138 test.equals( elem.selector, "#notfoundnono", "#notfoundnono Selector" );
|
rob@77
|
139 test.equals( elem.context, document, "#notfoundnono Context" );
|
rob@77
|
140
|
rob@77
|
141 elem = jQuery("#main", document);
|
rob@77
|
142 test.equals( elem.selector, "#main", "#main Selector" );
|
rob@77
|
143 test.equals( elem.context, document, "#main Context" );
|
rob@77
|
144
|
rob@77
|
145 elem = jQuery("#main", document.body);
|
rob@77
|
146 test.equals( elem.selector, "#main", "#main Selector" );
|
rob@77
|
147 test.equals( elem.context, document.body, "#main Context" );
|
rob@77
|
148
|
rob@77
|
149 // Test cloning
|
rob@77
|
150 elem = jQuery(elem);
|
rob@77
|
151 test.equals( elem.selector, "#main", "#main Selector" );
|
rob@77
|
152 test.equals( elem.context, document.body, "#main Context" );
|
rob@77
|
153
|
rob@77
|
154 elem = jQuery(document.body).find("#main");
|
rob@77
|
155 test.equals( elem.selector, "#main", "#main find Selector" );
|
rob@77
|
156 test.equals( elem.context, document.body, "#main find Context" );
|
rob@77
|
157
|
rob@77
|
158 elem = jQuery("#main").filter("div");
|
rob@77
|
159 test.equals( elem.selector, "#main.filter(div)", "#main filter Selector" );
|
rob@77
|
160 test.equals( elem.context, document, "#main filter Context" );
|
rob@77
|
161
|
rob@77
|
162 elem = jQuery("#main").not("div");
|
rob@77
|
163 test.equals( elem.selector, "#main.not(div)", "#main not Selector" );
|
rob@77
|
164 test.equals( elem.context, document, "#main not Context" );
|
rob@77
|
165
|
rob@77
|
166 elem = jQuery("#main").filter("div").not("div");
|
rob@77
|
167 test.equals( elem.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
|
rob@77
|
168 test.equals( elem.context, document, "#main filter, not Context" );
|
rob@77
|
169
|
rob@77
|
170 elem = jQuery("#main").filter("div").not("div").end();
|
rob@77
|
171 test.equals( elem.selector, "#main.filter(div)", "#main filter, not, end Selector" );
|
rob@77
|
172 test.equals( elem.context, document, "#main filter, not, end Context" );
|
rob@77
|
173
|
rob@77
|
174 elem = jQuery("#main").parent("body");
|
rob@77
|
175 test.equals( elem.selector, "#main.parent(body)", "#main parent Selector" );
|
rob@77
|
176 test.equals( elem.context, document, "#main parent Context" );
|
rob@77
|
177
|
rob@77
|
178 elem = jQuery("#main").eq(0);
|
rob@77
|
179 test.equals( elem.selector, "#main.slice(0,1)", "#main eq Selector" );
|
rob@77
|
180 test.equals( elem.context, document, "#main eq Context" );
|
rob@77
|
181
|
rob@77
|
182 var d = "<div />";
|
rob@77
|
183 test.equals(
|
rob@77
|
184 jQuery(d).appendTo(jQuery(d)).selector,
|
rob@77
|
185 jQuery(d).appendTo(d).selector,
|
rob@77
|
186 "manipulation methods make same selector for jQuery objects"
|
rob@77
|
187 );
|
rob@77
|
188 test.done();
|
rob@77
|
189 },
|
rob@77
|
190 "noConflict": function(test) {
|
rob@77
|
191 test.expect(7);
|
rob@77
|
192
|
rob@77
|
193 var originaljQuery = jQuery,
|
rob@77
|
194 original$ = $,
|
rob@77
|
195 $$ = jQuery;
|
rob@77
|
196
|
rob@77
|
197 test.equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
|
rob@77
|
198 test.equals( jQuery, $$, "Make sure jQuery wasn't touched." );
|
rob@77
|
199 test.equals( $, original$, "Make sure $ was reverted." );
|
rob@77
|
200
|
rob@77
|
201 jQuery = $ = $$;
|
rob@77
|
202
|
rob@77
|
203 test.equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );
|
rob@77
|
204 test.equals( jQuery, originaljQuery, "Make sure jQuery was reverted." );
|
rob@77
|
205 test.equals( $, original$, "Make sure $ was reverted." );
|
rob@77
|
206 test.ok( $$("#main").html("test"), "Make sure that jQuery still works." );
|
rob@77
|
207
|
rob@77
|
208 jQuery = $$;
|
rob@77
|
209 test.done();
|
rob@77
|
210 },
|
rob@77
|
211
|
rob@77
|
212 "trim": function(test) {
|
rob@77
|
213 test.expect(9);
|
rob@77
|
214
|
rob@77
|
215 var nbsp = String.fromCharCode(160);
|
rob@77
|
216
|
rob@77
|
217 test.equals( jQuery.trim("hello "), "hello", "trailing space" );
|
rob@77
|
218 test.equals( jQuery.trim(" hello"), "hello", "leading space" );
|
rob@77
|
219 test.equals( jQuery.trim(" hello "), "hello", "space on both sides" );
|
rob@77
|
220 test.equals( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", " " );
|
rob@77
|
221
|
rob@77
|
222 test.equals( jQuery.trim(), "", "Nothing in." );
|
rob@77
|
223 test.equals( jQuery.trim( undefined ), "", "Undefined" );
|
rob@77
|
224 test.equals( jQuery.trim( null ), "", "Null" );
|
rob@77
|
225 test.equals( jQuery.trim( 5 ), "5", "Number" );
|
rob@77
|
226 test.equals( jQuery.trim( false ), "false", "Boolean" );
|
rob@77
|
227 test.done();
|
rob@77
|
228 },
|
rob@77
|
229 "type": function(test) {
|
rob@77
|
230 test.expect(23);
|
rob@77
|
231
|
rob@77
|
232 test.equals( jQuery.type(null), "null", "null" );
|
rob@77
|
233 test.equals( jQuery.type(undefined), "undefined", "undefined" );
|
rob@77
|
234 test.equals( jQuery.type(true), "boolean", "Boolean" );
|
rob@77
|
235 test.equals( jQuery.type(false), "boolean", "Boolean" );
|
rob@77
|
236 test.equals( jQuery.type(Boolean(true)), "boolean", "Boolean" );
|
rob@77
|
237 test.equals( jQuery.type(0), "number", "Number" );
|
rob@77
|
238 test.equals( jQuery.type(1), "number", "Number" );
|
rob@77
|
239 test.equals( jQuery.type(Number(1)), "number", "Number" );
|
rob@77
|
240 test.equals( jQuery.type(""), "string", "String" );
|
rob@77
|
241 test.equals( jQuery.type("a"), "string", "String" );
|
rob@77
|
242 test.equals( jQuery.type(String("a")), "string", "String" );
|
rob@77
|
243 test.equals( jQuery.type({}), "object", "Object" );
|
rob@77
|
244 test.equals( jQuery.type(/foo/), "regexp", "RegExp" );
|
rob@77
|
245 test.equals( jQuery.type(new RegExp("asdf")), "regexp", "RegExp" );
|
rob@77
|
246 test.equals( jQuery.type([1]), "array", "Array" );
|
rob@77
|
247 test.equals( jQuery.type(new Date()), "date", "Date" );
|
rob@77
|
248 test.equals( jQuery.type(new Function("return;")), "function", "Function" );
|
rob@77
|
249 test.equals( jQuery.type(function(){}), "function", "Function" );
|
rob@77
|
250 test.equals( jQuery.type(window), "object", "Window" );
|
rob@77
|
251 test.equals( jQuery.type(document), "object", "Document" );
|
rob@77
|
252 test.equals( jQuery.type(document.body), "object", "Element" );
|
rob@77
|
253 test.equals( jQuery.type(document.createTextNode("foo")), "object", "TextNode" );
|
rob@77
|
254 test.equals( jQuery.type(document.getElementsByTagName("*")), "object", "NodeList" );
|
rob@77
|
255 test.done();
|
rob@77
|
256 },
|
rob@77
|
257 "isPlainObject": function(test) {
|
rob@77
|
258 test.expect(13);
|
rob@77
|
259
|
rob@77
|
260 // The use case that we want to match
|
rob@77
|
261 test.ok(jQuery.isPlainObject({}), "{}");
|
rob@77
|
262
|
rob@77
|
263 // Not objects shouldn't be matched
|
rob@77
|
264 test.ok(!jQuery.isPlainObject(""), "string");
|
rob@77
|
265 test.ok(!jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number");
|
rob@77
|
266 test.ok(!jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean");
|
rob@77
|
267 test.ok(!jQuery.isPlainObject(null), "null");
|
rob@77
|
268 test.ok(!jQuery.isPlainObject(undefined), "undefined");
|
rob@77
|
269
|
rob@77
|
270 // Arrays shouldn't be matched
|
rob@77
|
271 test.ok(!jQuery.isPlainObject([]), "array");
|
rob@77
|
272
|
rob@77
|
273 // Instantiated objects shouldn't be matched
|
rob@77
|
274 test.ok(!jQuery.isPlainObject(new Date), "new Date");
|
rob@77
|
275
|
rob@77
|
276 var fn = function(){};
|
rob@77
|
277
|
rob@77
|
278 // Functions shouldn't be matched
|
rob@77
|
279 test.ok(!jQuery.isPlainObject(fn), "fn");
|
rob@77
|
280
|
rob@77
|
281 // Again, instantiated objects shouldn't be matched
|
rob@77
|
282 test.ok(!jQuery.isPlainObject(new fn), "new fn (no methods)");
|
rob@77
|
283
|
rob@77
|
284 // Makes the function a little more realistic
|
rob@77
|
285 // (and harder to detect, incidentally)
|
rob@77
|
286 fn.prototype = {someMethod: function(){}};
|
rob@77
|
287
|
rob@77
|
288 // Again, instantiated objects shouldn't be matched
|
rob@77
|
289 test.ok(!jQuery.isPlainObject(new fn), "new fn");
|
rob@77
|
290
|
rob@77
|
291 // DOM Element
|
rob@77
|
292 test.ok(!jQuery.isPlainObject(document.createElement("div")), "DOM Element");
|
rob@77
|
293
|
rob@77
|
294 // Window
|
rob@77
|
295 test.ok(!jQuery.isPlainObject(window), "window");
|
rob@77
|
296
|
rob@77
|
297 /* XXX removed iframe test */
|
rob@77
|
298 test.done();
|
rob@77
|
299 },
|
rob@77
|
300 "isFunction": function(test) {
|
rob@77
|
301 test.expect(19);
|
rob@77
|
302
|
rob@77
|
303 // Make sure that false values return false
|
rob@77
|
304 test.ok( !jQuery.isFunction(), "No Value" );
|
rob@77
|
305 test.ok( !jQuery.isFunction( null ), "null Value" );
|
rob@77
|
306 test.ok( !jQuery.isFunction( undefined ), "undefined Value" );
|
rob@77
|
307 test.ok( !jQuery.isFunction( "" ), "Empty String Value" );
|
rob@77
|
308 test.ok( !jQuery.isFunction( 0 ), "0 Value" );
|
rob@77
|
309
|
rob@77
|
310 // Check built-ins
|
rob@77
|
311 // Safari uses "(Internal Function)"
|
rob@77
|
312 test.ok( jQuery.isFunction(String), "String Function("+String+")" );
|
rob@77
|
313 test.ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
|
rob@77
|
314 test.ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
|
rob@77
|
315 test.ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
|
rob@77
|
316
|
rob@77
|
317 // When stringified, this could be misinterpreted
|
rob@77
|
318 var mystr = "function";
|
rob@77
|
319 test.ok( !jQuery.isFunction(mystr), "Function String" );
|
rob@77
|
320
|
rob@77
|
321 // When stringified, this could be misinterpreted
|
rob@77
|
322 var myarr = [ "function" ];
|
rob@77
|
323 test.ok( !jQuery.isFunction(myarr), "Function Array" );
|
rob@77
|
324
|
rob@77
|
325 // When stringified, this could be misinterpreted
|
rob@77
|
326 var myfunction = { "function": "test" };
|
rob@77
|
327 test.ok( !jQuery.isFunction(myfunction), "Function Object" );
|
rob@77
|
328
|
rob@77
|
329 // Make sure normal functions still work
|
rob@77
|
330 var fn = function(){};
|
rob@77
|
331 test.ok( jQuery.isFunction(fn), "Normal Function" );
|
rob@77
|
332
|
rob@77
|
333 var obj = document.createElement("object");
|
rob@77
|
334
|
rob@77
|
335 // Firefox says this is a function
|
rob@77
|
336 test.ok( !jQuery.isFunction(obj), "Object Element" );
|
rob@77
|
337
|
rob@77
|
338 // IE says this is an object
|
rob@77
|
339 // Since 1.3, this isn't supported (#2968)
|
rob@77
|
340 //test.ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
|
rob@77
|
341
|
rob@77
|
342 var nodes = document.body.childNodes;
|
rob@77
|
343
|
rob@77
|
344 // Safari says this is a function
|
rob@77
|
345 test.ok( !jQuery.isFunction(nodes), "childNodes Property" );
|
rob@77
|
346
|
rob@77
|
347 var first = document.body.firstChild;
|
rob@77
|
348
|
rob@77
|
349 // Normal elements are reported test.ok everywhere
|
rob@77
|
350 test.ok( !jQuery.isFunction(first), "A normal DOM Element" );
|
rob@77
|
351
|
rob@77
|
352 var input = document.createElement("input");
|
rob@77
|
353 input.type = "text";
|
rob@77
|
354 document.body.appendChild( input );
|
rob@77
|
355
|
rob@77
|
356 // IE says this is an object
|
rob@77
|
357 // Since 1.3, this isn't supported (#2968)
|
rob@77
|
358 //test.ok( jQuery.isFunction(input.focus), "A default function property" );
|
rob@77
|
359
|
rob@77
|
360 document.body.removeChild( input );
|
rob@77
|
361
|
rob@77
|
362 var a = document.createElement("a");
|
rob@77
|
363 a.href = "some-function";
|
rob@77
|
364 document.body.appendChild( a );
|
rob@77
|
365
|
rob@77
|
366 // This serializes with the word 'function' in it
|
rob@77
|
367 test.ok( !jQuery.isFunction(a), "Anchor Element" );
|
rob@77
|
368
|
rob@77
|
369 document.body.removeChild( a );
|
rob@77
|
370
|
rob@77
|
371 // Recursive function calls have lengths and array-like properties
|
rob@77
|
372 function callme(callback){
|
rob@77
|
373 function fn(response){
|
rob@77
|
374 callback(response);
|
rob@77
|
375 }
|
rob@77
|
376
|
rob@77
|
377 test.ok( jQuery.isFunction(fn), "Recursive Function Call" );
|
rob@77
|
378
|
rob@77
|
379 fn({ some: "data" });
|
rob@77
|
380 };
|
rob@77
|
381
|
rob@77
|
382 callme(function(){
|
rob@77
|
383 callme(function(){});
|
rob@77
|
384 });
|
rob@77
|
385 test.done();
|
rob@77
|
386 },
|
rob@77
|
387 "isXMLDoc - HTML": function(test) {
|
rob@77
|
388 test.expect(4);
|
rob@77
|
389
|
rob@77
|
390 test.ok( !jQuery.isXMLDoc( document ), "HTML document" );
|
rob@77
|
391 test.ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
|
rob@77
|
392 test.ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
|
rob@77
|
393
|
rob@77
|
394 var iframe = document.createElement("iframe");
|
rob@77
|
395 document.body.appendChild( iframe );
|
rob@77
|
396
|
rob@77
|
397 try {
|
rob@77
|
398 var body = jQuery(iframe).contents()[0];
|
rob@77
|
399
|
rob@77
|
400 try {
|
rob@77
|
401 test.ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
|
rob@77
|
402 } catch(e) {
|
rob@77
|
403 test.ok( false, "Iframe body element exception" );
|
rob@77
|
404 }
|
rob@77
|
405
|
rob@77
|
406 } catch(e) {
|
rob@77
|
407 test.ok( true, "Iframe body element - iframe not working correctly" );
|
rob@77
|
408 }
|
rob@77
|
409
|
rob@77
|
410 test.done();
|
rob@77
|
411 },
|
rob@77
|
412 "jQuery('html')": function(test) {
|
rob@77
|
413 test.expect(15);
|
rob@77
|
414
|
rob@77
|
415 jQuery.foo = false;
|
rob@77
|
416 var s = jQuery("<script>jQuery.foo='test';</script>")[0];
|
rob@77
|
417 test.ok( s, "Creating a script" );
|
rob@77
|
418 test.ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
|
rob@77
|
419 jQuery("body").append("<script>jQuery.foo='test';</script>");
|
rob@77
|
420 test.ok( jQuery.foo, "Executing a scripts contents in the right context" );
|
rob@77
|
421
|
rob@77
|
422 // Test multi-line HTML
|
rob@77
|
423 var div = jQuery("<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>")[0];
|
rob@77
|
424 test.equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
|
rob@77
|
425 test.equals( div.firstChild.nodeType, 3, "Text node." );
|
rob@77
|
426 test.equals( div.lastChild.nodeType, 3, "Text node." );
|
rob@77
|
427 test.equals( div.childNodes[1].nodeType, 1, "Paragraph." );
|
rob@77
|
428 test.equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
|
rob@77
|
429
|
rob@77
|
430 test.ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
|
rob@77
|
431
|
rob@77
|
432 test.ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
|
rob@77
|
433
|
rob@77
|
434 test.ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
|
rob@77
|
435
|
rob@77
|
436 var j = jQuery("<span>hi</span> there <!-- mon ami -->");
|
rob@77
|
437 test.ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
|
rob@77
|
438
|
rob@77
|
439 test.ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
|
rob@77
|
440
|
rob@77
|
441 test.ok( jQuery("<div></div>")[0], "Create a div with closing tag." );
|
rob@77
|
442 test.ok( jQuery("<table></table>")[0], "Create a table with closing tag." );
|
rob@77
|
443
|
rob@77
|
444 test.done();
|
rob@77
|
445 },
|
rob@77
|
446 "create script tag": function (test) {
|
rob@77
|
447 var src = null, dom;
|
rob@77
|
448 test.expect(1);
|
rob@77
|
449 dom = jsdom('<script src="none.js" type="text/javascript"></script>');
|
rob@77
|
450 src = jQuery('script', dom).attr('src');
|
rob@77
|
451 test.equals(src, 'none.js', 'script should return proper src attribute');
|
rob@77
|
452 test.done();
|
rob@77
|
453 },
|
rob@77
|
454 "jQuery('html', context)": function(test) {
|
rob@77
|
455 test.expect(1);
|
rob@77
|
456
|
rob@77
|
457 var $div = jQuery("<div/>")[0];
|
rob@77
|
458 var $span = jQuery("<span/>", $div);
|
rob@77
|
459 test.equals($span.length, 1, "Verify a span created with a div context works, #1763");
|
rob@77
|
460 test.done();
|
rob@77
|
461 },
|
rob@77
|
462 "text()": function(test) {
|
rob@77
|
463 test.expect(2);
|
rob@77
|
464 test.equals("Yahoo", jQuery("#yahoo").text(), "check for text in anchor");
|
rob@77
|
465 test.equals("Everything inside the red border is inside a div with id=\"foo\".", jQuery("#sndp").text(), "check for text in complex paragraph");
|
rob@77
|
466 test.done();
|
rob@77
|
467 },
|
rob@77
|
468 "end()": function(test) {
|
rob@77
|
469 test.expect(3);
|
rob@77
|
470 test.equals( 'Yahoo', jQuery('#yahoo').parent().end().text(), 'Check for end' );
|
rob@77
|
471 test.ok( jQuery('#yahoo').end(), 'Check for end with nothing to end' );
|
rob@77
|
472
|
rob@77
|
473 var x = jQuery('#yahoo');
|
rob@77
|
474 x.parent();
|
rob@77
|
475 test.equals( 'Yahoo', jQuery('#yahoo').text(), 'Check for non-destructive behaviour' );
|
rob@77
|
476 test.done();
|
rob@77
|
477 },
|
rob@77
|
478
|
rob@77
|
479 "length": function(test) {
|
rob@77
|
480 test.expect(1);
|
rob@77
|
481 test.equals( jQuery("#main p").length, 6, "Get Number of Elements Found" );
|
rob@77
|
482 test.done();
|
rob@77
|
483 },
|
rob@77
|
484
|
rob@77
|
485 "size()": function(test) {
|
rob@77
|
486 test.expect(1);
|
rob@77
|
487 test.equals( jQuery("#main p").size(), 6, "Get Number of Elements Found" );
|
rob@77
|
488 test.done();
|
rob@77
|
489 },
|
rob@77
|
490
|
rob@77
|
491 "get()": function(test) {
|
rob@77
|
492 test.expect(1);
|
rob@77
|
493 test.same( jQuery("#main p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
|
rob@77
|
494 test.done();
|
rob@77
|
495 },
|
rob@77
|
496
|
rob@77
|
497 "toArray()": function(test) {
|
rob@77
|
498 test.expect(1);
|
rob@77
|
499 test.same( jQuery("#main p").toArray(),
|
rob@77
|
500 q("firstp","ap","sndp","en","sap","first"),
|
rob@77
|
501 "Convert jQuery object to an Array" )
|
rob@77
|
502 test.done();
|
rob@77
|
503 },
|
rob@77
|
504
|
rob@77
|
505 "get(Number)": function(test) {
|
rob@77
|
506 test.expect(2);
|
rob@77
|
507 test.equals( jQuery("#main p").get(0), document.getElementById("firstp"), "Get A Single Element" );
|
rob@77
|
508 test.strictEqual( jQuery("#firstp").get(1), undefined, "Try get with index larger elements count" );
|
rob@77
|
509 test.done();
|
rob@77
|
510 },
|
rob@77
|
511
|
rob@77
|
512 "get(-Number)": function(test) {
|
rob@77
|
513 test.expect(2);
|
rob@77
|
514 test.equals( jQuery("p").get(-1), document.getElementById("first"), "Get a single element with negative index" );
|
rob@77
|
515 test.strictEqual( jQuery("#firstp").get(-2), undefined, "Try get with index negative index larger then elements count" );
|
rob@77
|
516 test.done();
|
rob@77
|
517 },
|
rob@77
|
518
|
rob@77
|
519 "attr()": function(test) {
|
rob@77
|
520 var e = null;
|
rob@77
|
521 test.expect(4);
|
rob@77
|
522 test.equals( jQuery('#input1').attr('name'), 'PWD', "Get form element name attribute" );
|
rob@77
|
523 test.equals( jQuery('#input2').attr('name'), 'T1', "Get form element name attribute" );
|
rob@77
|
524 test.equals( jQuery('item').attr('name'), 'test val', "Get name attribute from element" );
|
rob@77
|
525 e = jsdom('<element name="dude" age="25">content</element>');
|
rob@77
|
526 test.equals( jQuery('element', e).attr('name'), 'dude', "Get name attribute from element" );
|
rob@77
|
527 test.done();
|
rob@77
|
528 },
|
rob@77
|
529
|
rob@77
|
530 "each(Function)": function(test) {
|
rob@77
|
531 test.expect(1);
|
rob@77
|
532 var div = jQuery("div");
|
rob@77
|
533 div.each(function(){this.foo = 'zoo';});
|
rob@77
|
534 var pass = true;
|
rob@77
|
535 for ( var i = 0; i < div.size(); i++ ) {
|
rob@77
|
536 if ( div.get(i).foo != "zoo" ) pass = false;
|
rob@77
|
537 }
|
rob@77
|
538 test.ok( pass, "Execute a function, Relative" );
|
rob@77
|
539 test.done();
|
rob@77
|
540 },
|
rob@77
|
541
|
rob@77
|
542 "slice()": function(test) {
|
rob@77
|
543 test.expect(7);
|
rob@77
|
544
|
rob@77
|
545 var $links = jQuery("#ap a");
|
rob@77
|
546
|
rob@77
|
547 test.same( $links.slice(1,2).get(), q("groups"), "slice(1,2)" );
|
rob@77
|
548 test.same( $links.slice(1).get(), q("groups", "anchor1", "mark"), "slice(1)" );
|
rob@77
|
549 test.same( $links.slice(0,3).get(), q("google", "groups", "anchor1"), "slice(0,3)" );
|
rob@77
|
550 test.same( $links.slice(-1).get(), q("mark"), "slice(-1)" );
|
rob@77
|
551
|
rob@77
|
552 test.same( $links.eq(1).get(), q("groups"), "eq(1)" );
|
rob@77
|
553 test.same( $links.eq('2').get(), q("anchor1"), "eq('2')" );
|
rob@77
|
554 test.same( $links.eq(-1).get(), q("mark"), "eq(-1)" );
|
rob@77
|
555 test.done();
|
rob@77
|
556 },
|
rob@77
|
557
|
rob@77
|
558 "first()/last()": function(test) {
|
rob@77
|
559 test.expect(4);
|
rob@77
|
560
|
rob@77
|
561 var $links = jQuery("#ap a"), $none = jQuery("asdf");
|
rob@77
|
562
|
rob@77
|
563 test.same( $links.first().get(), q("google"), "first()" );
|
rob@77
|
564 test.same( $links.last().get(), q("mark"), "last()" );
|
rob@77
|
565
|
rob@77
|
566 test.same( $none.first().get(), [], "first() none" );
|
rob@77
|
567 test.same( $none.last().get(), [], "last() none" );
|
rob@77
|
568 test.done();
|
rob@77
|
569 },
|
rob@77
|
570
|
rob@77
|
571 "map()": function(test) {
|
rob@77
|
572 test.expect(2);//test.expect(6);
|
rob@77
|
573
|
rob@77
|
574 test.same(
|
rob@77
|
575 jQuery("#ap").map(function(){
|
rob@77
|
576 return jQuery(this).find("a").get();
|
rob@77
|
577 }).get(),
|
rob@77
|
578 q("google", "groups", "anchor1", "mark"),
|
rob@77
|
579 "Array Map"
|
rob@77
|
580 );
|
rob@77
|
581
|
rob@77
|
582 test.same(
|
rob@77
|
583 jQuery("#ap > a").map(function(){
|
rob@77
|
584 return this.parentNode;
|
rob@77
|
585 }).get(),
|
rob@77
|
586 q("ap","ap","ap"),
|
rob@77
|
587 "Single Map"
|
rob@77
|
588 );
|
rob@77
|
589 test.done();
|
rob@77
|
590
|
rob@77
|
591 return;//these haven't been accepted yet
|
rob@77
|
592
|
rob@77
|
593 //for #2616
|
rob@77
|
594 var keys = jQuery.map( {a:1,b:2}, function( v, k ){
|
rob@77
|
595 return k;
|
rob@77
|
596 }, [ ] );
|
rob@77
|
597
|
rob@77
|
598 test.equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
|
rob@77
|
599
|
rob@77
|
600 var values = jQuery.map( {a:1,b:2}, function( v, k ){
|
rob@77
|
601 return v;
|
rob@77
|
602 }, [ ] );
|
rob@77
|
603
|
rob@77
|
604 test.equals( values.join(""), "12", "Map the values from a hash to an array" );
|
rob@77
|
605
|
rob@77
|
606 var scripts = document.getElementsByTagName("script");
|
rob@77
|
607 var mapped = jQuery.map( scripts, function( v, k ){
|
rob@77
|
608 return v;
|
rob@77
|
609 }, {length:0} );
|
rob@77
|
610
|
rob@77
|
611 test.equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
|
rob@77
|
612
|
rob@77
|
613 var flat = jQuery.map( Array(4), function( v, k ){
|
rob@77
|
614 return k % 2 ? k : [k,k,k];//try mixing array and regular returns
|
rob@77
|
615 });
|
rob@77
|
616
|
rob@77
|
617 test.equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
|
rob@77
|
618 },
|
rob@77
|
619
|
rob@77
|
620 "jQuery.merge()": function(test) {
|
rob@77
|
621 test.expect(8);
|
rob@77
|
622
|
rob@77
|
623 var parse = jQuery.merge;
|
rob@77
|
624
|
rob@77
|
625 test.same( parse([],[]), [], "Empty arrays" );
|
rob@77
|
626
|
rob@77
|
627 test.same( parse([1],[2]), [1,2], "Basic" );
|
rob@77
|
628 test.same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
|
rob@77
|
629
|
rob@77
|
630 test.same( parse([1,2],[]), [1,2], "Second empty" );
|
rob@77
|
631 test.same( parse([],[1,2]), [1,2], "First empty" );
|
rob@77
|
632
|
rob@77
|
633 // Fixed at [5998], #3641
|
rob@77
|
634 test.same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
|
rob@77
|
635
|
rob@77
|
636 // After fixing #5527
|
rob@77
|
637 test.same( parse([], [null, undefined]), [null, undefined], "Second array including null and undefined values");
|
rob@77
|
638 test.same( parse({length:0}, [1,2]), {length:2, 0:1, 1:2}, "First array like");
|
rob@77
|
639 test.done();
|
rob@77
|
640 },
|
rob@77
|
641
|
rob@77
|
642 "jQuery.extend(Object, Object)": function(test) {
|
rob@77
|
643 test.expect(28);
|
rob@77
|
644
|
rob@77
|
645 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
|
rob@77
|
646 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
|
rob@77
|
647 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
|
rob@77
|
648 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
|
rob@77
|
649 deep1 = { foo: { bar: true } },
|
rob@77
|
650 deep1copy = { foo: { bar: true } },
|
rob@77
|
651 deep2 = { foo: { baz: true }, foo2: document },
|
rob@77
|
652 deep2copy = { foo: { baz: true }, foo2: document },
|
rob@77
|
653 deepmerged = { foo: { bar: true, baz: true }, foo2: document },
|
rob@77
|
654 arr = [1, 2, 3],
|
rob@77
|
655 nestedarray = { arr: arr };
|
rob@77
|
656
|
rob@77
|
657 jQuery.extend(settings, options);
|
rob@77
|
658 test.same( settings, merged, "Check if extended: settings must be extended" );
|
rob@77
|
659 test.same( options, optionsCopy, "Check if not modified: options must not be modified" );
|
rob@77
|
660
|
rob@77
|
661 jQuery.extend(settings, null, options);
|
rob@77
|
662 test.same( settings, merged, "Check if extended: settings must be extended" );
|
rob@77
|
663 test.same( options, optionsCopy, "Check if not modified: options must not be modified" );
|
rob@77
|
664
|
rob@77
|
665 jQuery.extend(true, deep1, deep2);
|
rob@77
|
666 test.same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
|
rob@77
|
667 test.same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
|
rob@77
|
668 test.equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
|
rob@77
|
669
|
rob@77
|
670 test.ok( jQuery.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" );
|
rob@77
|
671
|
rob@77
|
672 // #5991
|
rob@77
|
673 test.ok( jQuery.isArray( jQuery.extend(true, { arr: {} }, nestedarray).arr ), "Cloned array heve to be an Array" );
|
rob@77
|
674 test.ok( jQuery.isPlainObject( jQuery.extend(true, { arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" );
|
rob@77
|
675
|
rob@77
|
676 var empty = {};
|
rob@77
|
677 var optionsWithLength = { foo: { length: -1 } };
|
rob@77
|
678 jQuery.extend(true, empty, optionsWithLength);
|
rob@77
|
679 test.same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
|
rob@77
|
680
|
rob@77
|
681 empty = {};
|
rob@77
|
682 var optionsWithDate = { foo: { date: new Date } };
|
rob@77
|
683 jQuery.extend(true, empty, optionsWithDate);
|
rob@77
|
684 test.same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
|
rob@77
|
685
|
rob@77
|
686 var myKlass = function() {};
|
rob@77
|
687 var customObject = new myKlass();
|
rob@77
|
688 var optionsWithCustomObject = { foo: { date: customObject } };
|
rob@77
|
689 empty = {};
|
rob@77
|
690 jQuery.extend(true, empty, optionsWithCustomObject);
|
rob@77
|
691 test.ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
|
rob@77
|
692
|
rob@77
|
693 // Makes the class a little more realistic
|
rob@77
|
694 myKlass.prototype = { someMethod: function(){} };
|
rob@77
|
695 empty = {};
|
rob@77
|
696 jQuery.extend(true, empty, optionsWithCustomObject);
|
rob@77
|
697 test.ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
|
rob@77
|
698
|
rob@77
|
699 var ret = jQuery.extend(true, { foo: 4 }, { foo: new Number(5) } );
|
rob@77
|
700 test.ok( ret.foo == 5, "Wrapped numbers copy correctly" );
|
rob@77
|
701
|
rob@77
|
702 var nullUndef;
|
rob@77
|
703 nullUndef = jQuery.extend({}, options, { xnumber2: null });
|
rob@77
|
704 test.ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
|
rob@77
|
705
|
rob@77
|
706 nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
|
rob@77
|
707 test.ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
|
rob@77
|
708
|
rob@77
|
709 nullUndef = jQuery.extend({}, options, { xnumber0: null });
|
rob@77
|
710 test.ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
|
rob@77
|
711
|
rob@77
|
712 var target = {};
|
rob@77
|
713 var recursive = { foo:target, bar:5 };
|
rob@77
|
714 jQuery.extend(true, target, recursive);
|
rob@77
|
715 test.same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
|
rob@77
|
716
|
rob@77
|
717 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
|
rob@77
|
718 test.equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
|
rob@77
|
719
|
rob@77
|
720 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
|
rob@77
|
721 test.ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
|
rob@77
|
722
|
rob@77
|
723 var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
|
rob@77
|
724 test.ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
|
rob@77
|
725
|
rob@77
|
726 var obj = { foo:null };
|
rob@77
|
727 jQuery.extend(true, obj, { foo:"notnull" } );
|
rob@77
|
728 test.equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
|
rob@77
|
729
|
rob@77
|
730 function func() {}
|
rob@77
|
731 jQuery.extend(func, { key: "value" } );
|
rob@77
|
732 test.equals( func.key, "value", "Verify a function can be extended" );
|
rob@77
|
733
|
rob@77
|
734 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
|
rob@77
|
735 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
|
rob@77
|
736 options1 = { xnumber2: 1, xstring2: "x" },
|
rob@77
|
737 options1Copy = { xnumber2: 1, xstring2: "x" },
|
rob@77
|
738 options2 = { xstring2: "xx", xxx: "newstringx" },
|
rob@77
|
739 options2Copy = { xstring2: "xx", xxx: "newstringx" },
|
rob@77
|
740 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
|
rob@77
|
741
|
rob@77
|
742 var settings = jQuery.extend({}, defaults, options1, options2);
|
rob@77
|
743 test.same( settings, merged2, "Check if extended: settings must be extended" );
|
rob@77
|
744 test.same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
|
rob@77
|
745 test.same( options1, options1Copy, "Check if not modified: options1 must not be modified" );
|
rob@77
|
746 test.same( options2, options2Copy, "Check if not modified: options2 must not be modified" );
|
rob@77
|
747 test.done();
|
rob@77
|
748 },
|
rob@77
|
749
|
rob@77
|
750 "jQuery.each(Object,Function)": function(test) {
|
rob@77
|
751 test.expect(13);
|
rob@77
|
752 jQuery.each( [0,1,2], function(i, n){
|
rob@77
|
753 test.equals( i, n, "Check array iteration" );
|
rob@77
|
754 });
|
rob@77
|
755
|
rob@77
|
756 jQuery.each( [5,6,7], function(i, n){
|
rob@77
|
757 test.equals( i, n - 5, "Check array iteration" );
|
rob@77
|
758 });
|
rob@77
|
759
|
rob@77
|
760 jQuery.each( { name: "name", lang: "lang" }, function(i, n){
|
rob@77
|
761 test.equals( i, n, "Check object iteration" );
|
rob@77
|
762 });
|
rob@77
|
763
|
rob@77
|
764 var total = 0;
|
rob@77
|
765 jQuery.each([1,2,3], function(i,v){ total += v; });
|
rob@77
|
766 test.equals( total, 6, "Looping over an array" );
|
rob@77
|
767 total = 0;
|
rob@77
|
768 jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
|
rob@77
|
769 test.equals( total, 3, "Looping over an array, with break" );
|
rob@77
|
770 total = 0;
|
rob@77
|
771 jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
|
rob@77
|
772 test.equals( total, 6, "Looping over an object" );
|
rob@77
|
773 total = 0;
|
rob@77
|
774 jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
|
rob@77
|
775 test.equals( total, 3, "Looping over an object, with break" );
|
rob@77
|
776
|
rob@77
|
777 var f = function(){};
|
rob@77
|
778 f.foo = 'bar';
|
rob@77
|
779 jQuery.each(f, function(i){
|
rob@77
|
780 f[i] = 'baz';
|
rob@77
|
781 });
|
rob@77
|
782 test.equals( "baz", f.foo, "Loop over a function" );
|
rob@77
|
783 test.done();
|
rob@77
|
784 },
|
rob@77
|
785
|
rob@77
|
786 "jQuery.makeArray": function(test){
|
rob@77
|
787 test.expect(17);
|
rob@77
|
788
|
rob@77
|
789 test.equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
|
rob@77
|
790
|
rob@77
|
791 test.equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
|
rob@77
|
792
|
rob@77
|
793 test.equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
|
rob@77
|
794
|
rob@77
|
795 test.equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
|
rob@77
|
796
|
rob@77
|
797 test.equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and test.expect an empty array" );
|
rob@77
|
798
|
rob@77
|
799 test.equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
|
rob@77
|
800
|
rob@77
|
801 test.equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
|
rob@77
|
802
|
rob@77
|
803 test.equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
|
rob@77
|
804
|
rob@77
|
805 test.equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
|
rob@77
|
806
|
rob@77
|
807 test.equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
|
rob@77
|
808
|
rob@77
|
809 test.ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
|
rob@77
|
810
|
rob@77
|
811 // function, is tricky as it has length
|
rob@77
|
812 test.equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
|
rob@77
|
813
|
rob@77
|
814 //window, also has length
|
rob@77
|
815 test.equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
|
rob@77
|
816
|
rob@77
|
817 test.equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
|
rob@77
|
818
|
rob@77
|
819 test.equals( jQuery.makeArray(document.getElementById('form')).length, 2, "Pass makeArray a form (treat as elements)" );
|
rob@77
|
820
|
rob@77
|
821 // For #5610
|
rob@77
|
822 test.same( jQuery.makeArray({'length': '0'}), [], "Make sure object is coerced properly.");
|
rob@77
|
823 test.same( jQuery.makeArray({'length': '5'}), [], "Make sure object is coerced properly.");
|
rob@77
|
824 test.done();
|
rob@77
|
825 },
|
rob@77
|
826
|
rob@77
|
827 "jQuery.isEmptyObject": function(test){
|
rob@77
|
828 test.expect(2);
|
rob@77
|
829
|
rob@77
|
830 test.equals(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
|
rob@77
|
831 test.equals(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
|
rob@77
|
832
|
rob@77
|
833 // What about this ?
|
rob@77
|
834 // test.equals(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
|
rob@77
|
835 test.done();
|
rob@77
|
836 },
|
rob@77
|
837
|
rob@77
|
838 "jQuery.proxy": function(test){
|
rob@77
|
839 test.expect(4);
|
rob@77
|
840
|
rob@77
|
841 var testfn = function(){ test.equals( this, thisObject, "Make sure that scope is set properly." ); };
|
rob@77
|
842 var thisObject = { foo: "bar", method: testfn };
|
rob@77
|
843
|
rob@77
|
844 // Make sure normal works
|
rob@77
|
845 testfn.call( thisObject );
|
rob@77
|
846
|
rob@77
|
847 // Basic scoping
|
rob@77
|
848 jQuery.proxy( testfn, thisObject )();
|
rob@77
|
849
|
rob@77
|
850 // Make sure it doesn't freak out
|
rob@77
|
851 test.equals( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
|
rob@77
|
852
|
rob@77
|
853 // Use the string shortcut
|
rob@77
|
854 jQuery.proxy( thisObject, "method" )();
|
rob@77
|
855 test.done();
|
rob@77
|
856 },
|
rob@77
|
857 "jQuery.parseJSON": function(test){
|
rob@77
|
858 test.expect(8);
|
rob@77
|
859
|
rob@77
|
860 test.equals( jQuery.parseJSON(), null, "Nothing in, null out." );
|
rob@77
|
861 test.equals( jQuery.parseJSON( null ), null, "Nothing in, null out." );
|
rob@77
|
862 test.equals( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
|
rob@77
|
863
|
rob@77
|
864 test.same( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
|
rob@77
|
865 test.same( jQuery.parseJSON('{"test":1}'), {"test":1}, "Plain object parsing." );
|
rob@77
|
866
|
rob@77
|
867 test.same( jQuery.parseJSON('\n{"test":1}'), {"test":1}, "Make sure leading whitespaces are handled." );
|
rob@77
|
868
|
rob@77
|
869 try {
|
rob@77
|
870 jQuery.parseJSON("{a:1}");
|
rob@77
|
871 test.ok( false, "Test malformed JSON string." );
|
rob@77
|
872 } catch( e ) {
|
rob@77
|
873 test.ok( true, "Test malformed JSON string." );
|
rob@77
|
874 }
|
rob@77
|
875
|
rob@77
|
876 try {
|
rob@77
|
877 jQuery.parseJSON("{'a':1}");
|
rob@77
|
878 test.ok( false, "Test malformed JSON string." );
|
rob@77
|
879 } catch( e ) {
|
rob@77
|
880 test.ok( true, "Test malformed JSON string." );
|
rob@77
|
881 }
|
rob@77
|
882 test.done();
|
rob@77
|
883 },
|
rob@77
|
884
|
rob@77
|
885 "jQuery.sub() - Static Methods": function(test){
|
rob@77
|
886 test.expect(18);
|
rob@77
|
887 var Subclass = jQuery.sub();
|
rob@77
|
888 Subclass.extend({
|
rob@77
|
889 topLevelMethod: function() {return this.debug;},
|
rob@77
|
890 debug: false,
|
rob@77
|
891 config: {
|
rob@77
|
892 locale: 'en_US'
|
rob@77
|
893 },
|
rob@77
|
894 setup: function(config) {
|
rob@77
|
895 this.extend(true, this.config, config);
|
rob@77
|
896 }
|
rob@77
|
897 });
|
rob@77
|
898 Subclass.fn.extend({subClassMethod: function() { return this;}});
|
rob@77
|
899
|
rob@77
|
900 //Test Simple Subclass
|
rob@77
|
901 test.ok(Subclass.topLevelMethod() === false, 'Subclass.topLevelMethod thought debug was true');
|
rob@77
|
902 test.ok(Subclass.config.locale == 'en_US', Subclass.config.locale + ' is wrong!');
|
rob@77
|
903 test.same(Subclass.config.test, undefined, 'Subclass.config.test is set incorrectly');
|
rob@77
|
904 test.equal(jQuery.ajax, Subclass.ajax, 'The subclass failed to get all top level methods');
|
rob@77
|
905
|
rob@77
|
906 //Create a SubSubclass
|
rob@77
|
907 var SubSubclass = Subclass.sub();
|
rob@77
|
908
|
rob@77
|
909 //Make Sure the SubSubclass inherited properly
|
rob@77
|
910 test.ok(SubSubclass.topLevelMethod() === false, 'SubSubclass.topLevelMethod thought debug was true');
|
rob@77
|
911 test.ok(SubSubclass.config.locale == 'en_US', SubSubclass.config.locale + ' is wrong!');
|
rob@77
|
912 test.same(SubSubclass.config.test, undefined, 'SubSubclass.config.test is set incorrectly');
|
rob@77
|
913 test.equal(jQuery.ajax, SubSubclass.ajax, 'The subsubclass failed to get all top level methods');
|
rob@77
|
914
|
rob@77
|
915 //Modify The Subclass and test the Modifications
|
rob@77
|
916 SubSubclass.fn.extend({subSubClassMethod: function() { return this;}});
|
rob@77
|
917 SubSubclass.setup({locale: 'es_MX', test: 'worked'});
|
rob@77
|
918 SubSubclass.debug = true;
|
rob@77
|
919 SubSubclass.ajax = function() {return false;};
|
rob@77
|
920 test.ok(SubSubclass.topLevelMethod(), 'SubSubclass.topLevelMethod thought debug was false');
|
rob@77
|
921 test.same(SubSubclass(document).subClassMethod, Subclass.fn.subClassMethod, 'Methods Differ!');
|
rob@77
|
922 test.ok(SubSubclass.config.locale == 'es_MX', SubSubclass.config.locale + ' is wrong!');
|
rob@77
|
923 test.ok(SubSubclass.config.test == 'worked', 'SubSubclass.config.test is set incorrectly');
|
rob@77
|
924 test.notEqual(jQuery.ajax, SubSubclass.ajax, 'The subsubclass failed to get all top level methods');
|
rob@77
|
925
|
rob@77
|
926 //This shows that the modifications to the SubSubClass did not bubble back up to it's superclass
|
rob@77
|
927 test.ok(Subclass.topLevelMethod() === false, 'Subclass.topLevelMethod thought debug was true');
|
rob@77
|
928 test.ok(Subclass.config.locale == 'en_US', Subclass.config.locale + ' is wrong!');
|
rob@77
|
929 test.same(Subclass.config.test, undefined, 'Subclass.config.test is set incorrectly');
|
rob@77
|
930 test.same(Subclass(document).subSubClassMethod, undefined, 'subSubClassMethod set incorrectly');
|
rob@77
|
931 test.equal(jQuery.ajax, Subclass.ajax, 'The subclass failed to get all top level methods');
|
rob@77
|
932 test.done();
|
rob@77
|
933 },
|
rob@77
|
934
|
rob@77
|
935 "jQuery.sub() - .fn Methods": function(test){
|
rob@77
|
936 test.expect(378);
|
rob@77
|
937
|
rob@77
|
938 var Subclass = jQuery.sub(),
|
rob@77
|
939 SubclassSubclass = Subclass.sub(),
|
rob@77
|
940 jQueryDocument = jQuery(document),
|
rob@77
|
941 selectors, contexts, methods, method, arg, description;
|
rob@77
|
942
|
rob@77
|
943 jQueryDocument.toString = function(){ return 'jQueryDocument'; };
|
rob@77
|
944
|
rob@77
|
945 Subclass.fn.subclassMethod = function(){};
|
rob@77
|
946 SubclassSubclass.fn.subclassSubclassMethod = function(){};
|
rob@77
|
947
|
rob@77
|
948 selectors = [
|
rob@77
|
949 'body',
|
rob@77
|
950 'html, body',
|
rob@77
|
951 '<div></div>'
|
rob@77
|
952 ];
|
rob@77
|
953
|
rob@77
|
954 methods = [ // all methods that return a new jQuery instance
|
rob@77
|
955 ['eq', 1],
|
rob@77
|
956 ['add', document],
|
rob@77
|
957 ['end'],
|
rob@77
|
958 ['has'],
|
rob@77
|
959 ['closest', 'div'],
|
rob@77
|
960 ['filter', document],
|
rob@77
|
961 ['find', 'div']
|
rob@77
|
962 ];
|
rob@77
|
963
|
rob@77
|
964 contexts = [undefined, document, jQueryDocument];
|
rob@77
|
965
|
rob@77
|
966 jQuery.each(selectors, function(i, selector){
|
rob@77
|
967
|
rob@77
|
968 jQuery.each(methods, function(){
|
rob@77
|
969 method = this[0];
|
rob@77
|
970 arg = this[1];
|
rob@77
|
971
|
rob@77
|
972 jQuery.each(contexts, function(i, context){
|
rob@77
|
973
|
rob@77
|
974 description = '("'+selector+'", '+context+').'+method+'('+(arg||'')+')';
|
rob@77
|
975
|
rob@77
|
976 test.same(
|
rob@77
|
977 jQuery(selector, context)[method](arg).subclassMethod, undefined,
|
rob@77
|
978 'jQuery'+description+' doesnt have Subclass methods'
|
rob@77
|
979 );
|
rob@77
|
980 test.same(
|
rob@77
|
981 jQuery(selector, context)[method](arg).subclassSubclassMethod, undefined,
|
rob@77
|
982 'jQuery'+description+' doesnt have SubclassSubclass methods'
|
rob@77
|
983 );
|
rob@77
|
984 test.same(
|
rob@77
|
985 Subclass(selector, context)[method](arg).subclassMethod, Subclass.fn.subclassMethod,
|
rob@77
|
986 'Subclass'+description+' has Subclass methods'
|
rob@77
|
987 );
|
rob@77
|
988 test.same(
|
rob@77
|
989 Subclass(selector, context)[method](arg).subclassSubclassMethod, undefined,
|
rob@77
|
990 'Subclass'+description+' doesnt have SubclassSubclass methods'
|
rob@77
|
991 );
|
rob@77
|
992 test.same(
|
rob@77
|
993 SubclassSubclass(selector, context)[method](arg).subclassMethod, Subclass.fn.subclassMethod,
|
rob@77
|
994 'SubclassSubclass'+description+' has Subclass methods'
|
rob@77
|
995 );
|
rob@77
|
996 test.same(
|
rob@77
|
997 SubclassSubclass(selector, context)[method](arg).subclassSubclassMethod, SubclassSubclass.fn.subclassSubclassMethod,
|
rob@77
|
998 'SubclassSubclass'+description+' has SubclassSubclass methods'
|
rob@77
|
999 );
|
rob@77
|
1000
|
rob@77
|
1001 });
|
rob@77
|
1002 });
|
rob@77
|
1003 });
|
rob@77
|
1004 test.done();
|
rob@77
|
1005 }
|
rob@77
|
1006 });
|
rob@77
|
1007
|