" );
21 |
22 | // These should be "auto" (or some better value)
23 | // temporarily provide "0px" for backwards compat
24 | test.equals( div.css("width"), "0px", "Width on disconnected node." );
25 | test.equals( div.css("height"), "0px", "Height on disconnected node." );
26 |
27 | div.css({ width: 4, height: 4 });
28 |
29 | test.equals( div.css("width"), "4px", "Width on disconnected node." );
30 | test.equals( div.css("height"), "4px", "Height on disconnected node." );
31 |
32 | var div2 = jQuery( "
").appendTo("body");
33 |
34 | test.equals( div2.find("input").css("height"), "20px", "Height on hidden input." );
35 | test.equals( div2.find("textarea").css("height"), "20px", "Height on hidden textarea." );
36 | test.equals( div2.find("div").css("height"), "20px", "Height on hidden textarea." );
37 |
38 | div2.remove();
39 |
40 | // handle negative numbers by ignoring #1599, #4216
41 | jQuery('#nothiddendiv').css({ 'width': 1, 'height': 1 });
42 |
43 | // var width = parseFloat(jQuery('#nothiddendiv').css('width'), 10);
44 | // var height = parseFloat(jQuery('#nothiddendiv').css('height'), 10);
45 | jQuery('#nothiddendiv').css({ width: -1, height: -1 });
46 | //test.equals( parseFloat(jQuery('#nothiddendiv').css('width')), width, 'Test negative width ignored')
47 | //test.equals( parseFloat(jQuery('#nothiddendiv').css('height')), height, 'Test negative height ignored')
48 |
49 | test.equals( jQuery('
').css('display'), 'none', 'Styles on disconnected nodes');
50 |
51 | //jQuery('#floatTest').css('float', 'right');
52 | //test.equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');
53 | //jQuery('#floatTest').css({'font-size': '30px'});
54 | //test.equals( jQuery('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');
55 |
56 | /*jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
57 | jQuery('#foo').css({opacity: n});
58 | test.equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
59 | jQuery('#foo').css({opacity: parseFloat(n)});
60 | test.equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
61 | });*/
62 | jQuery('#foo').css({opacity: ''});
63 | test.equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
64 |
65 | //test.equals( jQuery('#empty').css('opacity'), '0', "Assert opacity is accessible via filter property set in stylesheet in IE" );
66 | //jQuery('#empty').css({ opacity: '1' });
67 | //test.equals( jQuery('#empty').css('opacity'), '1', "Assert opacity is taken from style attribute when set vs stylesheet in IE with filters" );
68 |
69 | div = jQuery('#nothiddendiv');
70 | var child = jQuery('#nothiddendivchild');
71 |
72 | //test.equals( parseInt(div.css("fontSize")), 16, "Verify fontSize px set." );
73 | //test.equals( parseInt(div.css("font-size")), 16, "Verify fontSize px set." );
74 | //test.equals( parseInt(child.css("fontSize")), 16, "Verify fontSize px set." );
75 | //test.equals( parseInt(child.css("font-size")), 16, "Verify fontSize px set." );
76 |
77 | child.css("height", "100%");
78 | test.equals( child[0].style.height, "100%", "Make sure the height is being set correctly." );
79 |
80 | child.attr("class", "em");
81 | //test.equals( parseInt(child.css("fontSize")), 32, "Verify fontSize em set." );
82 |
83 | // Have to verify this as the result depends upon the browser's CSS
84 | // support for font-size percentages
85 | child.attr("class", "prct");
86 | var prctval = parseInt(child.css("fontSize"), 10), checkval = 0;
87 | if ( prctval === 16 || prctval === 24 ) {
88 | checkval = prctval;
89 | }
90 |
91 | //test.equals( prctval, checkval, "Verify fontSize % set." );
92 |
93 | test.equals( typeof child.css("width"), "string", "Make sure that a string width is returned from css('width')." );
94 |
95 | var old = child[0].style.height;
96 |
97 | // Test NaN
98 | child.css("height", parseFloat("zoo"));
99 | test.equals( child[0].style.height, old, "Make sure height isn't changed on NaN." );
100 |
101 | // Test null
102 | child.css("height", null);
103 | test.equals( child[0].style.height, old, "Make sure height isn't changed on null." );
104 |
105 | old = child[0].style.fontSize;
106 |
107 | // Test NaN
108 | child.css("font-size", parseFloat("zoo"));
109 | test.equals( child[0].style.fontSize, old, "Make sure font-size isn't changed on NaN." );
110 |
111 | // Test null
112 | child.css("font-size", null);
113 | test.equals( child[0].style.fontSize, old, "Make sure font-size isn't changed on null." );
114 | test.done();
115 | }
116 | };
117 | };
118 |
--------------------------------------------------------------------------------
/test/dom.js:
--------------------------------------------------------------------------------
1 | module.exports = function(ctx) {
2 |
3 | 'use strict';
4 |
5 | var jsdom = ctx.jsdom;
6 |
7 | return {
8 | "jQuery('html')": function(test) {
9 |
10 | var jQuery = ctx.$;
11 |
12 | test.expect(15);
13 |
14 | jQuery.foo = false;
15 | var s = jQuery("")[0];
16 | test.ok( s, "Creating a script" );
17 | test.ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
18 | jQuery("body").append("");
19 | test.ok( jQuery.foo, "Executing a scripts contents in the right context" );
20 |
21 | // Test multi-line HTML
22 | var div = jQuery("
\r\nsome text\n
some p
\nmore text\r\n
")[0];
23 | test.equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
24 | test.equals( div.firstChild.nodeType, 3, "Text node." );
25 | test.equals( div.lastChild.nodeType, 3, "Text node." );
26 | test.equals( div.childNodes[1].nodeType, 1, "Paragraph." );
27 | test.equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
28 |
29 | test.ok( jQuery("
")[0], "Creating a link" );
30 |
31 | test.ok( !jQuery("")[0].parentNode, "Create a script" );
32 |
33 | test.ok( jQuery("
").attr("type", "hidden"), "Create an input and set the type." );
34 |
35 | var j = jQuery("
hi there ");
36 | test.ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
37 |
38 | test.ok( !jQuery("
test ")[0].selected, "Make sure that options are auto-selected #2050" );
39 |
40 | test.ok( jQuery("
")[0], "Create a div with closing tag." );
41 | test.ok( jQuery("
")[0], "Create a table with closing tag." );
42 |
43 | test.done();
44 | },
45 |
46 | "create script tag": function (test) {
47 |
48 | var jQuery = ctx.$;
49 |
50 | var src = null, dom;
51 | test.expect(1);
52 | dom = jsdom('');
53 | src = jQuery('script', dom).attr('src');
54 | test.equals(src, 'none.js', 'script should return proper src attribute');
55 | test.done();
56 | },
57 |
58 | "jQuery('html', context)": function(test) {
59 |
60 | var jQuery = ctx.$;
61 |
62 | test.expect(1);
63 |
64 | var $div = jQuery("
")[0];
65 | var $span = jQuery("
", $div);
66 | test.equals($span.length, 1, "Verify a span created with a div context works, #1763");
67 | test.done();
68 | }
69 | };
70 | };
--------------------------------------------------------------------------------
/test/fixtures/core.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
jQuery Test Suite
4 |
5 |
6 |
7 |
See this blog entry for more information.
8 |
9 | Here are some links in a normal paragraph: Google ,
10 | Google Groups (Link) .
11 | This link has class="blog" :
12 | diveintomark
13 |
14 |
15 |
21 |
Try them out:
22 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/test/fixtures/css.css:
--------------------------------------------------------------------------------
1 | #nothiddendiv { font-size: 16px; }
2 | #nothiddendivchild.em { font-size: 2em; }
3 | #nothiddendivchild.prct { font-size: 150%; }
4 |
--------------------------------------------------------------------------------
/test/fixtures/css.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
jQuery Test Suite
5 |
10 |
11 |
12 |
13 |
14 |
17 |
18 |
19 |
See this blog entry for more information.
20 |
21 | Here are some links in a normal paragraph: Google ,
22 | Google Groups (Link) .
23 | This link has class="blog" :
24 | diveintomark
25 |
26 |
27 |
33 |
Try them out:
34 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/test/fn.js:
--------------------------------------------------------------------------------
1 | module.exports = function(ctx) {
2 |
3 | 'use strict';
4 |
5 | var q = ctx.q;
6 | var jsdom = ctx.jsdom;
7 |
8 | return {
9 | "text()": function(test) {
10 |
11 | var jQuery = ctx.$;
12 |
13 | test.expect(2);
14 | test.equals("Yahoo", jQuery("#yahoo").text(), "check for text in anchor");
15 | test.equals("Everything inside the red border is inside a div with id=\"foo\".", jQuery("#sndp").text(), "check for text in complex paragraph");
16 | test.done();
17 | },
18 |
19 | "end()": function(test) {
20 |
21 | var jQuery = ctx.$;
22 |
23 | test.expect(3);
24 | test.equals( 'Yahoo', jQuery('#yahoo').parent().end().text(), 'Check for end' );
25 | test.ok( jQuery('#yahoo').end(), 'Check for end with nothing to end' );
26 |
27 | var x = jQuery('#yahoo');
28 | x.parent();
29 | test.equals( 'Yahoo', jQuery('#yahoo').text(), 'Check for non-destructive behaviour' );
30 | test.done();
31 | },
32 |
33 | "length": function(test) {
34 |
35 | var jQuery = ctx.$;
36 |
37 | test.expect(1);
38 | test.equals( jQuery("#main p").length, 6, "Get Number of Elements Found" );
39 | test.done();
40 | },
41 |
42 | "size()": function(test) {
43 |
44 | var jQuery = ctx.$;
45 |
46 | test.expect(1);
47 | test.equals( jQuery("#main p").size(), 6, "Get Number of Elements Found" );
48 | test.done();
49 | },
50 |
51 | "get()": function(test) {
52 |
53 | var jQuery = ctx.$;
54 |
55 | test.expect(1);
56 | test.same( jQuery("#main p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
57 | test.done();
58 | },
59 |
60 | "toArray()": function(test) {
61 |
62 | var jQuery = ctx.$;
63 |
64 | test.expect(1);
65 | test.same( jQuery("#main p").toArray(),
66 | q("firstp","ap","sndp","en","sap","first"),
67 | "Convert jQuery object to an Array" );
68 | test.done();
69 | },
70 |
71 | "get(Number)": function(test) {
72 |
73 | var jQuery = ctx.$;
74 | var document = ctx.document;
75 |
76 | test.expect(2);
77 | test.equals( jQuery("#main p").get(0), document.getElementById("firstp"), "Get A Single Element" );
78 | test.strictEqual( jQuery("#firstp").get(1), undefined, "Try get with index larger elements count" );
79 | test.done();
80 | },
81 |
82 | "get(-Number)": function(test) {
83 |
84 | var jQuery = ctx.$;
85 | var document = ctx.document;
86 |
87 | test.expect(2);
88 | test.equals( jQuery("p").get(-1), document.getElementById("first"), "Get a single element with negative index" );
89 | test.strictEqual( jQuery("#firstp").get(-2), undefined, "Try get with index negative index larger then elements count" );
90 | test.done();
91 | },
92 |
93 | "attr()": function(test) {
94 |
95 | var jQuery = ctx.$;
96 |
97 | var e = null;
98 | test.expect(4);
99 | test.equals( jQuery('#input1').attr('name'), 'PWD', "Get form element name attribute" );
100 | test.equals( jQuery('#input2').attr('name'), 'T1', "Get form element name attribute" );
101 | test.equals( jQuery('item').attr('name'), 'test val', "Get name attribute from element" );
102 | e = jsdom('
content ');
103 | test.equals( jQuery('element', e).attr('name'), 'dude', "Get name attribute from element" );
104 | test.done();
105 | }
106 | };
107 | };
108 |
--------------------------------------------------------------------------------
/test/index.js:
--------------------------------------------------------------------------------
1 | var testCase = require('nodeunit').testCase;
2 | var jsdom = require('jsdom').jsdom;
3 | var jquery = require(__dirname + '/../lib/node-jquery');
4 | var fs = require('fs');
5 |
6 | // Execution context object
7 | var context = {
8 | 'jsdom': jsdom
9 | };
10 |
11 | /**
12 | * Returns an array of elements with the given IDs, eg.
13 | * @example q("main", "foo", "bar")
14 | * @result [
,
, ]
15 | */
16 | context.q = function query_ids() {
17 |
18 | var r = [];
19 |
20 | for ( var i = 0; i < arguments.length; i++ ) {
21 | r.push( context.document.getElementById( arguments[i] ) );
22 | }
23 |
24 | return r;
25 | };
26 |
27 | function recreate_doc(fixture) {
28 | var markup = fs.readFileSync('test/fixtures/' + (fixture||'core') + '.html', 'utf8');
29 | context.document = jsdom(markup);
30 | context.window = context.document.createWindow();
31 | context.$ = jquery.create(context.window, '1.8');
32 | }
33 |
34 | var testSuite = {
35 | setUp: function (callback) {
36 | recreate_doc();
37 | callback();
38 | }
39 | };
40 |
41 | var tests, defaults = ['collections', 'core', 'dom', 'fn', 'objects', 'selector', 'sub', 'type', 'utils'];
42 | if(process.env.TEST) {
43 | tests = [process.env.TEST];
44 | } else {
45 | tests = defaults;
46 | }
47 |
48 | tests.forEach(function(name) {
49 | var test = require('./' + name)(context);
50 | // Copy over the tests into the suite
51 | Object.keys(test).forEach(function(key) {
52 | testSuite[key] = test[key];
53 | });
54 | });
55 |
56 | if(tests === defaults || tests.indexOf('css') > -1) {
57 | testSuite.css = require('./css')(context);
58 | testSuite.css.setUp = function (callback) {
59 | recreate_doc('css');
60 | callback();
61 | };
62 | }
63 |
64 | module.exports = testCase(testSuite);
65 |
--------------------------------------------------------------------------------
/test/objects.js:
--------------------------------------------------------------------------------
1 | module.exports = function(ctx) {
2 |
3 | 'use strict';
4 |
5 | return {
6 | "jQuery.extend(Object, Object)": function(test) {
7 |
8 | var jQuery = ctx.$;
9 | var document = ctx.document;
10 |
11 | test.expect(28);
12 |
13 | var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
14 | options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
15 | optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
16 | merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
17 | deep1 = { foo: { bar: true } },
18 | // deep1copy = { foo: { bar: true } },
19 | deep2 = { foo: { baz: true }, foo2: document },
20 | deep2copy = { foo: { baz: true }, foo2: document },
21 | deepmerged = { foo: { bar: true, baz: true }, foo2: document },
22 | arr = [1, 2, 3],
23 | nestedarray = { arr: arr };
24 |
25 | jQuery.extend(settings, options);
26 | test.same( settings, merged, "Check if extended: settings must be extended" );
27 | test.same( options, optionsCopy, "Check if not modified: options must not be modified" );
28 |
29 | jQuery.extend(settings, null, options);
30 | test.same( settings, merged, "Check if extended: settings must be extended" );
31 | test.same( options, optionsCopy, "Check if not modified: options must not be modified" );
32 |
33 | jQuery.extend(true, deep1, deep2);
34 | test.same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
35 | test.same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
36 | test.equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
37 |
38 | test.ok( jQuery.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" );
39 |
40 | // #5991
41 | test.ok( jQuery.isArray( jQuery.extend(true, { arr: {} }, nestedarray).arr ), "Cloned array heve to be an Array" );
42 | test.ok( jQuery.isPlainObject( jQuery.extend(true, { arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" );
43 |
44 | var empty = {};
45 | var optionsWithLength = { foo: { length: -1 } };
46 | jQuery.extend(true, empty, optionsWithLength);
47 | test.same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
48 |
49 | empty = {};
50 | var optionsWithDate = { foo: { date: new Date() } };
51 | jQuery.extend(true, empty, optionsWithDate);
52 | test.same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
53 |
54 | var MyKlass = function() {};
55 | var customObject = new MyKlass();
56 | var optionsWithCustomObject = { foo: { date: customObject } };
57 | empty = {};
58 | jQuery.extend(true, empty, optionsWithCustomObject);
59 | test.ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
60 |
61 | // Makes the class a little more realistic
62 | MyKlass.prototype = { someMethod: function(){} };
63 | empty = {};
64 | jQuery.extend(true, empty, optionsWithCustomObject);
65 | test.ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
66 |
67 | var ret = jQuery.extend(true, { foo: 4 }, { foo: 5 } );
68 | test.ok( ret.foo === 5, "Wrapped numbers copy correctly" );
69 |
70 | var nullUndef;
71 | nullUndef = jQuery.extend({}, options, { xnumber2: null });
72 | test.ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
73 |
74 | nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
75 | test.ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
76 |
77 | nullUndef = jQuery.extend({}, options, { xnumber0: null });
78 | test.ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
79 |
80 | var target = {};
81 | var recursive = { foo:target, bar:5 };
82 | jQuery.extend(true, target, recursive);
83 | test.same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
84 |
85 | ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
86 | test.equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
87 |
88 | ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
89 | test.ok( typeof ret.foo !== "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
90 |
91 | ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
92 | test.ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
93 |
94 | var obj = { foo:null };
95 | jQuery.extend(true, obj, { foo:"notnull" } );
96 | test.equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
97 |
98 | function func() {}
99 | jQuery.extend(func, { key: "value" } );
100 | test.equals( func.key, "value", "Verify a function can be extended" );
101 |
102 | var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
103 | defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
104 | options1 = { xnumber2: 1, xstring2: "x" },
105 | options1Copy = { xnumber2: 1, xstring2: "x" },
106 | options2 = { xstring2: "xx", xxx: "newstringx" },
107 | options2Copy = { xstring2: "xx", xxx: "newstringx" },
108 | merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
109 |
110 | settings = jQuery.extend({}, defaults, options1, options2);
111 | test.same( settings, merged2, "Check if extended: settings must be extended" );
112 | test.same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
113 | test.same( options1, options1Copy, "Check if not modified: options1 must not be modified" );
114 | test.same( options2, options2Copy, "Check if not modified: options2 must not be modified" );
115 | test.done();
116 | },
117 | "jQuery.isEmptyObject": function(test) {
118 |
119 | var jQuery = ctx.$;
120 |
121 | test.expect(2);
122 |
123 | test.equals(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
124 | test.equals(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
125 |
126 | // What about this ?
127 | // test.equals(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
128 | test.done();
129 | }
130 | };
131 | };
--------------------------------------------------------------------------------
/test/selector.js:
--------------------------------------------------------------------------------
1 | module.exports = function(ctx) {
2 |
3 | 'use strict';
4 |
5 | return {
6 | "selector state": function(test) {
7 |
8 | var jQuery = ctx.$;
9 | var document = ctx.document;
10 |
11 | test.expect(31);
12 |
13 | var elem;
14 |
15 | elem = jQuery(undefined);
16 | test.equals( elem.selector, "", "Empty jQuery Selector" );
17 | test.equals( elem.context, undefined, "Empty jQuery Context" );
18 |
19 | elem = jQuery(document);
20 | test.equals( elem.selector, "", "Document Selector" );
21 | test.equals( elem.context, document, "Document Context" );
22 |
23 | elem = jQuery(document.body);
24 | test.equals( elem.selector, "", "Body Selector" );
25 | test.equals( elem.context, document.body, "Body Context" );
26 |
27 | elem = jQuery("#main");
28 | test.equals( elem.selector, "#main", "#main Selector" );
29 | test.equals( elem.context, document, "#main Context" );
30 |
31 | elem = jQuery("#notfoundnono");
32 | test.equals( elem.selector, "#notfoundnono", "#notfoundnono Selector" );
33 | test.equals( elem.context, document, "#notfoundnono Context" );
34 |
35 | elem = jQuery("#main", document);
36 | test.equals( elem.selector, "#main", "#main Selector" );
37 | test.equals( elem.context, document, "#main Context" );
38 |
39 | elem = jQuery("#main", document.body);
40 | test.equals( elem.selector, "#main", "#main Selector" );
41 | test.equals( elem.context, document.body, "#main Context" );
42 |
43 | // Test cloning
44 | elem = jQuery(elem);
45 | test.equals( elem.selector, "#main", "#main Selector" );
46 | test.equals( elem.context, document.body, "#main Context" );
47 |
48 | elem = jQuery(document.body).find("#main");
49 | test.equals( elem.selector, "#main", "#main find Selector" );
50 | test.equals( elem.context, document.body, "#main find Context" );
51 |
52 | elem = jQuery("#main").filter("div");
53 | test.equals( elem.selector, "#main.filter(div)", "#main filter Selector" );
54 | test.equals( elem.context, document, "#main filter Context" );
55 |
56 | elem = jQuery("#main").not("div");
57 | test.equals( elem.selector, "#main.not(div)", "#main not Selector" );
58 | test.equals( elem.context, document, "#main not Context" );
59 |
60 | elem = jQuery("#main").filter("div").not("div");
61 | test.equals( elem.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
62 | test.equals( elem.context, document, "#main filter, not Context" );
63 |
64 | elem = jQuery("#main").filter("div").not("div").end();
65 | test.equals( elem.selector, "#main.filter(div)", "#main filter, not, end Selector" );
66 | test.equals( elem.context, document, "#main filter, not, end Context" );
67 |
68 | elem = jQuery("#main").parent("body");
69 | test.equals( elem.selector, "#main.parent(body)", "#main parent Selector" );
70 | test.equals( elem.context, document, "#main parent Context" );
71 |
72 | elem = jQuery("#main").eq(0);
73 | test.equals( elem.selector, "#main.slice(0,1)", "#main eq Selector" );
74 | test.equals( elem.context, document, "#main eq Context" );
75 |
76 | var d = "
";
77 | test.equals(
78 | jQuery(d).appendTo(jQuery(d)).selector,
79 | jQuery(d).appendTo(d).selector,
80 | "manipulation methods make same selector for jQuery objects"
81 | );
82 | test.done();
83 | }
84 | };
85 | };
--------------------------------------------------------------------------------
/test/sub.js:
--------------------------------------------------------------------------------
1 | module.exports = function(ctx) {
2 |
3 | 'use strict';
4 |
5 | return {
6 | "jQuery.sub() - Static Methods": function(test) {
7 |
8 | var jQuery = ctx.$;
9 | var document = ctx.document;
10 |
11 | if(jQuery.fn.jquery > '1.8.2') {
12 | test.done();
13 | return;
14 | }
15 |
16 | test.expect(18);
17 |
18 | var subclass = ctx.jQuery.sub();
19 |
20 | subclass.extend({
21 | 'topLevelMethod': function() {
22 | return this.debug;
23 | },
24 | 'debug': false,
25 | 'config': {
26 | 'locale': 'en_US'
27 | },
28 | 'setup': function(config) {
29 | this.extend(true, this.config, config);
30 | }
31 | });
32 |
33 | subclass.fn.extend({
34 | 'subClassMethod': function() {
35 | return this;
36 | }
37 | });
38 |
39 | //Test Simple Subclass
40 | test.ok(subclass.topLevelMethod() === false, 'Subclass.topLevelMethod thought debug was true');
41 | test.ok(subclass.config.locale === 'en_US', subclass.config.locale + ' is wrong!');
42 | test.same(subclass.config.test, undefined, 'Subclass.config.test is set incorrectly');
43 | test.equal(jQuery.ajax, subclass.ajax, 'The subclass failed to get all top level methods');
44 |
45 | //Create a SubSubclass
46 | var subSubclass = subclass.sub();
47 |
48 | //Make Sure the SubSubclass inherited properly
49 | test.ok(subSubclass.topLevelMethod() === false, 'SubSubclass.topLevelMethod thought debug was true');
50 | test.ok(subSubclass.config.locale === 'en_US', subSubclass.config.locale + ' is wrong!');
51 | test.same(subSubclass.config.test, undefined, 'SubSubclass.config.test is set incorrectly');
52 | test.equal(jQuery.ajax, subSubclass.ajax, 'The subsubclass failed to get all top level methods');
53 |
54 | //Modify The Subclass and test the Modifications
55 | subSubclass.fn.extend({subSubClassMethod: function() { return this;}});
56 | subSubclass.setup({locale: 'es_MX', test: 'worked'});
57 | subSubclass.debug = true;
58 | subSubclass.ajax = function() {return false;};
59 | test.ok(subSubclass.topLevelMethod(), 'SubSubclass.topLevelMethod thought debug was false');
60 | test.same(subSubclass(document).subClassMethod, subclass.fn.subClassMethod, 'Methods Differ!');
61 | test.ok(subSubclass.config.locale === 'es_MX', subSubclass.config.locale + ' is wrong!');
62 | test.ok(subSubclass.config.test === 'worked', 'SubSubclass.config.test is set incorrectly');
63 | test.notEqual(jQuery.ajax, subSubclass.ajax, 'The subsubclass failed to get all top level methods');
64 |
65 | //This shows that the modifications to the SubSubClass did not bubble back up to it's superclass
66 | test.ok(subclass.topLevelMethod() === false, 'Subclass.topLevelMethod thought debug was true');
67 | test.ok(subclass.config.locale === 'en_US', subclass.config.locale + ' is wrong!');
68 | test.same(subclass.config.test, undefined, 'Subclass.config.test is set incorrectly');
69 | test.same(subclass(document).subSubClassMethod, undefined, 'subSubClassMethod set incorrectly');
70 | test.equal(jQuery.ajax, subclass.ajax, 'The subclass failed to get all top level methods');
71 | test.done();
72 | },
73 |
74 | "jQuery.sub() - .fn Methods": function(test) {
75 |
76 | var jQuery = ctx.$;
77 | var document = ctx.document;
78 |
79 | if(jQuery.fn.jquery > '1.8.2') {
80 | test.done();
81 | return;
82 | }
83 |
84 | test.expect(378);
85 |
86 | var subclass = jQuery.sub(),
87 | subclassSubclass = subclass.sub(),
88 | jQueryDocument = jQuery(document),
89 | selectors, contexts, methods, method, arg, description;
90 |
91 | jQueryDocument.toString = function(){ return 'jQueryDocument'; };
92 |
93 | subclass.fn.subclassMethod = function(){};
94 | subclassSubclass.fn.subclassSubclassMethod = function(){};
95 |
96 | selectors = [
97 | 'body',
98 | 'html, body',
99 | '
'
100 | ];
101 |
102 | methods = [ // all methods that return a new jQuery instance
103 | ['eq', 1],
104 | ['add', document],
105 | ['end'],
106 | ['has'],
107 | ['closest', 'div'],
108 | ['filter', document],
109 | ['find', 'div']
110 | ];
111 |
112 | contexts = [undefined, document, jQueryDocument];
113 |
114 | jQuery.each(selectors, function(i, selector){
115 |
116 | jQuery.each(methods, function(){
117 | method = this[0];
118 | arg = this[1];
119 |
120 | jQuery.each(contexts, function(i, context){
121 |
122 | description = '("'+selector+'", '+context+').'+method+'('+(arg||'')+')';
123 |
124 | test.same(
125 | jQuery(selector, context)[method](arg).subclassMethod, undefined,
126 | 'jQuery'+description+' doesnt have Subclass methods'
127 | );
128 | test.same(
129 | jQuery(selector, context)[method](arg).subclassSubclassMethod, undefined,
130 | 'jQuery'+description+' doesnt have SubclassSubclass methods'
131 | );
132 | test.same(
133 | subclass(selector, context)[method](arg).subclassMethod, subclass.fn.subclassMethod,
134 | 'Subclass'+description+' has Subclass methods'
135 | );
136 | test.same(
137 | subclass(selector, context)[method](arg).subclassSubclassMethod, undefined,
138 | 'Subclass'+description+' doesnt have SubclassSubclass methods'
139 | );
140 | test.same(
141 | subclassSubclass(selector, context)[method](arg).subclassMethod, subclass.fn.subclassMethod,
142 | 'SubclassSubclass'+description+' has Subclass methods'
143 | );
144 | test.same(
145 | subclassSubclass(selector, context)[method](arg).subclassSubclassMethod, subclassSubclass.fn.subclassSubclassMethod,
146 | 'SubclassSubclass'+description+' has SubclassSubclass methods'
147 | );
148 |
149 | });
150 | });
151 | });
152 | test.done();
153 | }
154 | };
155 | };
--------------------------------------------------------------------------------
/test/type.js:
--------------------------------------------------------------------------------
1 | module.exports = function(ctx) {
2 |
3 | 'use strict';
4 |
5 | return {
6 | "type": function(test) {
7 |
8 | var jQuery = ctx.$;
9 | var document = ctx.document;
10 | var window = ctx.window;
11 |
12 | test.expect(22);
13 |
14 | test.equals( jQuery.type(null), "null", "null" );
15 | test.equals( jQuery.type(undefined), "undefined", "undefined" );
16 | test.equals( jQuery.type(true), "boolean", "Boolean" );
17 | test.equals( jQuery.type(false), "boolean", "Boolean" );
18 | test.equals( jQuery.type(Boolean(true)), "boolean", "Boolean" );
19 | test.equals( jQuery.type(0), "number", "Number" );
20 | test.equals( jQuery.type(1), "number", "Number" );
21 | test.equals( jQuery.type(Number(1)), "number", "Number" );
22 | test.equals( jQuery.type(""), "string", "String" );
23 | test.equals( jQuery.type("a"), "string", "String" );
24 | test.equals( jQuery.type(String("a")), "string", "String" );
25 | test.equals( jQuery.type({}), "object", "Object" );
26 | test.equals( jQuery.type(/foo/), "regexp", "RegExp" );
27 | test.equals( jQuery.type(new RegExp("asdf")), "regexp", "RegExp" );
28 | test.equals( jQuery.type([1]), "array", "Array" );
29 | test.equals( jQuery.type(new Date()), "date", "Date" );
30 | // test.equals( jQuery.type(new Function("return;")), "function", "Function" );
31 | test.equals( jQuery.type(function(){}), "function", "Function" );
32 | test.equals( jQuery.type(window), "object", "Window" );
33 | test.equals( jQuery.type(document), "object", "Document" );
34 | test.equals( jQuery.type(document.body), "object", "Element" );
35 | test.equals( jQuery.type(document.createTextNode("foo")), "object", "TextNode" );
36 | test.equals( jQuery.type(document.getElementsByTagName("*")), "object", "NodeList" );
37 | test.done();
38 | },
39 |
40 | "isPlainObject": function(test) {
41 |
42 | var jQuery = ctx.$;
43 | var document = ctx.document;
44 | var window = ctx.window;
45 |
46 | test.expect(13);
47 |
48 | // The use case that we want to match
49 | test.ok(jQuery.isPlainObject({}), "{}");
50 |
51 | // Not objects shouldn't be matched
52 | test.ok(!jQuery.isPlainObject(""), "string");
53 | test.ok(!jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number");
54 | test.ok(!jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean");
55 | test.ok(!jQuery.isPlainObject(null), "null");
56 | test.ok(!jQuery.isPlainObject(undefined), "undefined");
57 |
58 | // Arrays shouldn't be matched
59 | test.ok(!jQuery.isPlainObject([]), "array");
60 |
61 | // Instantiated objects shouldn't be matched
62 | test.ok(!jQuery.isPlainObject(new Date()), "new Date");
63 |
64 | var Fn = function(){};
65 |
66 | // Functions shouldn't be matched
67 | test.ok(!jQuery.isPlainObject(Fn), "fn");
68 |
69 | // Again, instantiated objects shouldn't be matched
70 | test.ok(!jQuery.isPlainObject(new Fn()), "new fn (no methods)");
71 |
72 | // Makes the function a little more realistic
73 | // (and harder to detect, incidentally)
74 | Fn.prototype = {someMethod: function(){}};
75 |
76 | // Again, instantiated objects shouldn't be matched
77 | test.ok(!jQuery.isPlainObject(new Fn()), "new fn");
78 |
79 | // DOM Element
80 | test.ok(!jQuery.isPlainObject(document.createElement("div")), "DOM Element");
81 |
82 | // Window
83 | test.ok(!jQuery.isPlainObject(window), "window");
84 |
85 | /* XXX removed iframe test */
86 | test.done();
87 | },
88 |
89 | "isFunction": function(test) {
90 |
91 | var jQuery = ctx.$;
92 | var document = ctx.document;
93 |
94 | test.expect(19);
95 |
96 | // Make sure that false values return false
97 | test.ok( !jQuery.isFunction(), "No Value" );
98 | test.ok( !jQuery.isFunction( null ), "null Value" );
99 | test.ok( !jQuery.isFunction( undefined ), "undefined Value" );
100 | test.ok( !jQuery.isFunction( "" ), "Empty String Value" );
101 | test.ok( !jQuery.isFunction( 0 ), "0 Value" );
102 |
103 | // Check built-ins
104 | // Safari uses "(Internal Function)"
105 | test.ok( jQuery.isFunction(String), "String Function("+String+")" );
106 | test.ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
107 | test.ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
108 | test.ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
109 |
110 | // When stringified, this could be misinterpreted
111 | var mystr = "function";
112 | test.ok( !jQuery.isFunction(mystr), "Function String" );
113 |
114 | // When stringified, this could be misinterpreted
115 | var myarr = [ "function" ];
116 | test.ok( !jQuery.isFunction(myarr), "Function Array" );
117 |
118 | // When stringified, this could be misinterpreted
119 | var myfunction = { "function": "test" };
120 | test.ok( !jQuery.isFunction(myfunction), "Function Object" );
121 |
122 | // Make sure normal functions still work
123 | var fn = function(){};
124 | test.ok( jQuery.isFunction(fn), "Normal Function" );
125 |
126 | var obj = document.createElement("object");
127 |
128 | // Firefox says this is a function
129 | test.ok( !jQuery.isFunction(obj), "Object Element" );
130 |
131 | // IE says this is an object
132 | // Since 1.3, this isn't supported (#2968)
133 | //test.ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
134 |
135 | var nodes = document.body.childNodes;
136 |
137 | // Safari says this is a function
138 | test.ok( !jQuery.isFunction(nodes), "childNodes Property" );
139 |
140 | var first = document.body.firstChild;
141 |
142 | // Normal elements are reported test.ok everywhere
143 | test.ok( !jQuery.isFunction(first), "A normal DOM Element" );
144 |
145 | var input = document.createElement("input");
146 | input.type = "text";
147 | document.body.appendChild( input );
148 |
149 | // IE says this is an object
150 | // Since 1.3, this isn't supported (#2968)
151 | //test.ok( jQuery.isFunction(input.focus), "A default function property" );
152 |
153 | document.body.removeChild( input );
154 |
155 | var a = document.createElement("a");
156 | a.href = "some-function";
157 | document.body.appendChild( a );
158 |
159 | // This serializes with the word 'function' in it
160 | test.ok( !jQuery.isFunction(a), "Anchor Element" );
161 |
162 | document.body.removeChild( a );
163 |
164 | // Recursive function calls have lengths and array-like properties
165 | function callme(callback){
166 | function fn(response){
167 | callback(response);
168 | }
169 |
170 | test.ok( jQuery.isFunction(fn), "Recursive Function Call" );
171 |
172 | fn({ some: "data" });
173 | }
174 |
175 | callme(function(){
176 | callme(function(){});
177 | });
178 | test.done();
179 | },
180 |
181 | "isXMLDoc - HTML": function(test) {
182 |
183 | var jQuery = ctx.$;
184 | var document = ctx.document;
185 |
186 | test.expect(4);
187 |
188 | test.ok( !jQuery.isXMLDoc( document ), "HTML document" );
189 | test.ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
190 | test.ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
191 |
192 | var iframe = document.createElement("iframe");
193 | document.body.appendChild( iframe );
194 |
195 | try {
196 | var body = jQuery(iframe).contents()[0];
197 |
198 | try {
199 | test.ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
200 | } catch(e) {
201 | test.ok( false, "Iframe body element exception" );
202 | }
203 |
204 | } catch(e) {
205 | test.ok( true, "Iframe body element - iframe not working correctly" );
206 | }
207 |
208 | test.done();
209 | }
210 | };
211 | };
--------------------------------------------------------------------------------
/test/utils.js:
--------------------------------------------------------------------------------
1 | module.exports = function(ctx) {
2 |
3 | 'use strict';
4 |
5 | return {
6 | "jQuery.proxy": function(test){
7 |
8 | var jQuery = ctx.$;
9 |
10 | test.expect(4);
11 |
12 | var testfn = function() {
13 | test.equals( this, thisObject, "Make sure that scope is set properly." );
14 | };
15 | var thisObject = { foo: "bar", method: testfn };
16 |
17 | // Make sure normal works
18 | testfn.call( thisObject );
19 |
20 | // Basic scoping
21 | jQuery.proxy( testfn, thisObject )();
22 |
23 | // Make sure it doesn't freak out
24 | test.equals( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
25 |
26 | // Use the string shortcut
27 | jQuery.proxy( thisObject, "method" )();
28 | test.done();
29 | },
30 |
31 | "trim": function(test) {
32 |
33 | var jQuery = ctx.$;
34 |
35 | test.expect(9);
36 |
37 | var nbsp = String.fromCharCode(160);
38 |
39 | test.equals( jQuery.trim("hello "), "hello", "trailing space" );
40 | test.equals( jQuery.trim(" hello"), "hello", "leading space" );
41 | test.equals( jQuery.trim(" hello "), "hello", "space on both sides" );
42 | test.equals( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", " " );
43 |
44 | test.equals( jQuery.trim(), "", "Nothing in." );
45 | test.equals( jQuery.trim( undefined ), "", "Undefined" );
46 | test.equals( jQuery.trim( null ), "", "Null" );
47 | test.equals( jQuery.trim( 5 ), "5", "Number" );
48 | test.equals( jQuery.trim( false ), "false", "Boolean" );
49 | test.done();
50 | },
51 |
52 | "jQuery.parseJSON": function(test){
53 |
54 | var jQuery = ctx.$;
55 |
56 | test.expect(8);
57 |
58 | test.equals( jQuery.parseJSON(), null, "Nothing in, null out." );
59 | test.equals( jQuery.parseJSON( null ), null, "Nothing in, null out." );
60 | test.equals( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
61 |
62 | test.same( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
63 | test.same( jQuery.parseJSON('{"test":1}'), {"test":1}, "Plain object parsing." );
64 |
65 | test.same( jQuery.parseJSON('\n{"test":1}'), {"test":1}, "Make sure leading whitespaces are handled." );
66 |
67 | try {
68 | jQuery.parseJSON("{a:1}");
69 | test.ok( false, "Test malformed JSON string." );
70 | } catch( e ) {
71 | test.ok( true, "Test malformed JSON string." );
72 | }
73 |
74 | try {
75 | jQuery.parseJSON("{'a':1}");
76 | test.ok( false, "Test malformed JSON string." );
77 | } catch( e ) {
78 | test.ok( true, "Test malformed JSON string." );
79 | }
80 | test.done();
81 | }
82 | };
83 | };
--------------------------------------------------------------------------------