comparison default/node_modules/shoestring/test/unit/core.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 /*
2 shoestring unit tests - using qUnit
3 */
4 (function(undefined){
5
6 module( 'Core' );
7 test( 'API Properties: shoestring is defined', function() {
8 ok( shoestring !== undefined );
9 });
10
11 test( 'API Properties: shoestring.fn is defined', function() {
12 ok( shoestring.fn !== undefined );
13 });
14
15 test( 'API Properties: shoestring.extend is defined', function() {
16 ok( shoestring.extend !== undefined );
17 });
18
19 test( 'API Properties: shoestring.inArray is defined', function() {
20 ok( shoestring.inArray !== undefined );
21 });
22
23 test( 'API Properties: shoestring.ready is defined', function() {
24 ok( shoestring.ready !== undefined );
25 });
26
27 module( 'Types' );
28 test( 'API Properties: shoestring is a function', function() {
29 ok( typeof(shoestring) === "function" );
30 });
31
32 test( 'API Properties: shoestring.fn is an object', function() {
33 ok( shoestring.fn === shoestring.Shoestring.prototype );
34 });
35
36 test( 'API Properties: shoestring.extend is a function', function() {
37 ok( typeof(shoestring.extend) === "function" );
38 });
39
40 test( 'API Properties: shoestring.inArray is a function', function() {
41 ok( typeof(shoestring.inArray) === "function" );
42 });
43
44 test( 'API Properties: shoestring.ready is a function', function() {
45 ok( typeof(shoestring.ready) === "function" );
46 });
47
48 module( 'Core fn Methods' );
49 test( 'API Properties: shoestring.fn.each, is defined', function() {
50 ok( shoestring.fn.each !== undefined );
51 });
52
53 test( 'API Properties: shoestring.fn.children, is defined', function() {
54 ok( shoestring.fn.children !== undefined );
55 });
56
57 test( 'API Properties: shoestring.fn.find, is defined', function() {
58 ok( shoestring.fn.find !== undefined );
59 });
60
61 module( 'fn Types' );
62
63 test( 'API Properties: shoestring.fn.each, is a function', function() {
64 ok( typeof(shoestring.fn.each) === "function" );
65 });
66
67 test( 'API Properties: shoestring.fn.children, is a function', function() {
68 ok( typeof(shoestring.fn.children) === "function" );
69 });
70
71 test( 'API Properties: shoestring.fn.find, is a function', function() {
72 ok( typeof(shoestring.fn.find) === "function" );
73 });
74
75
76
77 module( 'Functionality' );
78 test( 'shoestring with no arguments returns a shoestring object', function() {
79 ok( shoestring().constructor === shoestring.Shoestring );
80 });
81
82 test( 'shoestring with no arguments returns an empty array', function() {
83 ok( shoestring().length === 0 );
84 });
85
86 test( 'shoestring with a string argument returns a shoestring objecty', function() {
87 ok( shoestring( "body" ).constructor === shoestring.Shoestring );
88 });
89
90 test( 'shoestring with a string argument returns a collection of dom nodes from qsa', function() {
91 ok( shoestring( "body" )[ 0 ] === document.querySelectorAll( "body" )[ 0 ] );
92 });
93
94 test( 'shoestring with a string argument starting with "<" returns a shoestring object', function() {
95 ok( shoestring( "<div></div>" ).constructor === shoestring.Shoestring );
96 });
97
98 test( 'shoestring with a string argument starting with "<" returns a generated array of dom nodes', function() {
99 ok( shoestring( "<div></div>" )[0].constructor === HTMLDivElement );
100 });
101
102 test( 'shoestring with a string argument starting with "<" returns a generated array of dom nodes', function() {
103 ok( shoestring( "<div></div><h2></h2>" )[1].constructor === HTMLHeadingElement );
104 });
105
106 test( 'shoestring with a function argument returns array', function() {
107 ok( shoestring( function(){} ).constructor === Array );
108 });
109
110 test( 'shoestring with a function argument returns array with document child', function() {
111 ok( shoestring( function(){} )[ 0 ] === document );
112 });
113
114 test( 'shoestring with a Node passed in returns an array of that node', function(){
115 var el = document.querySelectorAll( ".constructor-selector" )[0];
116 ok( shoestring( el ).constructor === shoestring.Shoestring );
117 ok( shoestring( el )[0] === el );
118 });
119
120 test( 'shoestring with a Node passed in returns a singleton array', function(){
121 var el = document.querySelectorAll( ".constructor-selector" )[0];
122 ok( shoestring( el )[0].constructor === HTMLDivElement );
123 });
124
125 test( 'shoestring with a NodeList passed in returns an array of those nodes', function(){
126 var els = document.querySelectorAll( ".constructor-selector" );
127 equal( shoestring( els )[0], els[0] );
128 equal( shoestring( els ).length, 1 );
129 });
130
131 test( 'passing a string argument to shoestring with a second argument returns result scoped to second arg', function() {
132 ok( shoestring( "body" )[ 0 ] === shoestring( "body", "html" )[ 0 ] );
133 ok( shoestring( "body" )[ 0 ] !== shoestring( "body", "body" )[ 0 ] );
134 ok( shoestring( ".testel-2" ).length === 2 );
135 ok( shoestring( ".testel-2", ".testel" ).length === 1 );
136 });
137
138 test ('shoestring.each value matches array at index', function(){
139 expect( 5 );
140 var arr = [1,2,3,4,5];
141 $.each( arr, function( i, v ){
142 equal( v, arr[i], "Array at specified index " + i + " should have value of " + arr[i] + " but has " + v );
143 });
144 });
145
146 test ('shoestring.each value matches array at index for selected items', function(){
147 expect( 2 );
148 var div1 = $( "<div id='div1'></div>" );
149 var div2 = $( "<div id='div2'></div>" );
150 var divs = [ div1, div2 ];
151 $.each( divs, function( i, v ){
152 equal( v.id, divs[i].id, "Item at specified index " + i + " should have id of " + divs[i] + " but has " + v );
153 });
154 });
155
156 test ('shoestring.each only runs as many times as are items in array', function(){
157 var testels = $( ".testel-2" );
158 var divout = [];
159 $.each( testels, function( i, v ){
160 if( i > 0 ){
161 return false;
162 }
163 divout.push(v);
164 });
165
166 equal( divout.length, 1, "Both arrays should have equal length" );
167 });
168
169 test ('shoestring.each exits early if "false" is returned', function(){
170 var testels = $( ".testel-2" );
171 var divout = [];
172 $.each( testels, function( i, v ){
173 divout.push(v);
174 });
175
176 equal( divout.length, testels.length, "Both arrays should have equal length" );
177 });
178
179 test( 'shoestring with an array of Nodes passed returns an array of those nodes', function(){
180 var els = [ document.createElement( "div" ), document.createElement( "div" ), document.createElement( "div" ) ];
181 ok( shoestring( els ).constructor === shoestring.Shoestring );
182 ok( shoestring( els )[0].constructor === HTMLDivElement );
183 ok( shoestring( els ).length === 3 );
184 equal( Object.prototype.toString.call( shoestring( els ).map ).indexOf( '[native code]' ), -1, undefined );
185 });
186
187 test( "find can select multiple comma-separated children", function() {
188 equal( shoestring( ".find" ).find( ".child1, .child2" ).length, 2 );
189 });
190
191 })();