xonecas.io
142
St

startsInvasion / files

startsInvasion / libs / node_modules / bonzo / tests / tests.html

15.0 KB
1<!DOCTYPE HTML>
2<html lang="en-us">
3  <head>
4    <title>Bonzo Tests</title>
5    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
6    <link rel="stylesheet" href="../build/sink/src/sink.css" type="text/css">
7    <style type="text/css">
8      #fixtures {
9        position: absolute;
10        top: -9999em;
11      }
12      #overflowed {
13        overflow: auto;
14        width: 50px;
15        height: 100px;
16        top: 10000px;
17        position: absolute;
18      }
19      #overflower {
20        height: 1000px;
21        width: 1000px;
22      }
23
24      .show-hide-css {
25        display: none;
26      }
27    </style>
28    <script src="../build/sink/src/sink.js"></script>
29    <script src="../build/qwery/qwery.js"></script>
30    <script type="text/javascript">
31      function bonzo() {
32        return 'success';
33      }
34    </script>
35    <script src="../src/bonzo.js"></script>
36    <script type="text/javascript">
37      var dom = bonzo.noConflict();
38      var Q = qwery.noConflict();
39      dom.setQueryEngine(Q);
40      function $(s) {
41        return dom(Q(s));
42      }
43      dom.aug(dom, $);
44    </script>
45  </head>
46  <body>
47    <div id="fixtures">
48      <div class="prepend-with-engine"></div>
49      <div class="prepend-with-engine"></div>
50
51      <div class="prepend-with-engine-move"></div>
52
53      <div id="class-test" class="existing-class"></div>
54      <div id="toggle-class" class="toggle-me"></div>
55      <div id="append">
56        <div></div>
57        <div></div>
58      </div>
59      <div id="prepend">
60        <div id="prepend-a"></div>
61        <div id="prepend-b"></div>
62      </div>
63      <div id="after">
64        <div class="after-examples"></div>
65        <div class="after-examples"></div>
66      </div>
67      <div id="before">
68        <div class="before-examples"></div>
69        <div class="before-examples"></div>
70      </div>
71      <div id="insert-before">
72        <div class="before-target"></div>
73      </div>
74      <div id="insert-after">
75        <div class="after-target"></div>
76      </div>
77      <div id="insert-after-last"><div class="after-last-target"></div></div>
78      <div id="html"></div>
79      <div id="html-select"></div>
80      <div id="text"></div>
81      <div id="show-hide" style="width:10px"></div>
82      <div id="augment" style="color:orange;"></div>
83      <div id="empty">
84        <p>one</p>
85        <p>two</p>
86        <p>tre</p>
87      </div>
88      <div id="detach">
89        <div id="detach-a"></div>
90        <div id="detach-b"></div>
91      </div>
92      <div id="attrs">
93        <a id="twitter" href="http://twitter.com/">twitter.com</a>
94        <input type="hidden" value="eyo">
95        <input type="checkbox" checked>
96      </div>
97      <div id="first-last">
98        <div id="first"></div>
99        <div id="second"></div>
100        <div id="last"></div>
101      </div>
102      <div id="append-to"></div>
103      <div id="prepend-to">
104        <div id="prepend-first-child"></div>
105      </div>
106      <div id="sibling-tests">
107        <ul>
108          <li>zero</li>
109          <li class="nextr">one</li>
110          <li>two</li>
111          <li class="nextr">three</li>
112          <li>four</li>
113          <li class="nextr">five</li>
114        </ul>
115      </div>
116    </div>
117    <div id="overflowed">
118      <div id="overflower"></div>
119    </div>
120    <div id="styles" style="margin-left:5px;"></div>
121
122    <h1>Bonzo Tests</h1>
123    <ol id="tests"></ol>
124
125    <script>
126      sink('Bonzo', function(test, ok, before, after) {
127        test('should contain a noConflict() method', 2, function() {
128          ok(bonzo() == 'success', 'bonzo() is B()');
129          ok(typeof dom().each == 'function', 'B() is the library');
130        });
131
132        test('add/remove/has classes', 6, function () {
133          var el = Q('#class-test');
134          ok(!dom(el).hasClass('boosh'), 'element does not start off having class "boosh"');
135          ok(dom(el).hasClass('existing-class'), 'element has existing class "existing-class"');
136          dom(el).addClass('boosh');
137          ok(dom(el).hasClass('boosh'), 'element now has class "boosh" after addClass()');
138          ok(dom(el).hasClass('existing-class'), 'element still has pre-existing class "existing-class"');
139          dom(el).removeClass('boosh');
140          ok(!dom(el).hasClass('boosh'), 'element no longer has class "boosh" after removeClass()');
141          ok(dom(el).hasClass('existing-class'), 'element still has pre-existing class "existing-class"');
142        });
143
144        test('toggleClass', 3, function () {
145          ok($('#toggle-class').hasClass('toggle-me'), 'has class toggle-me');
146          $('#toggle-class').toggleClass('toggle-me');
147          ok(!$('#toggle-class').hasClass('toggle-me'), 'has class toggle-me');
148          $('#toggle-class').toggleClass('toggle-me');
149          ok($('#toggle-class').hasClass('toggle-me'), 'has class toggle-me');
150        });
151
152        test('should append nodes', 1, function () {
153          $('#append div').append('<p>hello</p><p>world <a href="#">hunger</a></p>');
154          ok(Q('#append p').length == 4, 'appended two {p} nodes on all elements');
155        });
156
157        test('should prepend nodes', 2, function () {
158          $('#prepend').prepend('<p>hello</p><p>world <a href="#">hunger</a></p>');
159          ok(Q('#prepend p')[0] == Q('#prepend')[0].firstChild, 'prepend two {p} nodes on all elements');
160          ok(Q('#prepend p').length == 2, 'prepended 2 {p} nodes');
161        });
162
163        test('should insert nodes before target', 2, function () {
164          var el = document.createElement('span');
165          el.innerHTML = '<b>some <em>shiza</em></b>';
166          $('.before-examples').before(el);
167          ok(Q('#before span').length == 2, 'inserted all nodes');
168          ok(Q('#before span')[0] == Q('.before-examples')[0].previousSibling, 'target node inserted before collection');
169        });
170
171        test('should insert nodes after target', 2, function () {
172          $('.after-examples').after('<span>some <em>shiza</em></span>');
173          ok(Q('#after em').length == 2, 'inserted all nodes');
174          ok(Q('#after span')[0] == Q('.after-examples')[0].nextSibling, 'inserted node after target');
175        });
176
177        test('should insert target before nodes', 1, function () {
178          $($.create('<p>eyo</p>')).insertBefore($('.before-target'));
179          ok(Q('.before-target')[0].previousSibling.tagName.toLowerCase() == 'p', 'created element was inserted before the target');
180        });
181
182        test('should insert target after nodes', 1, function () {
183          $($.create('<p>eyo</p>')).insertAfter($('.after-target'));
184          ok(Q('.after-target')[0].nextSibling && Q('.after-target')[0].nextSibling.tagName.toLowerCase() == 'p', 'created element was inserted after the target');
185        });
186
187        test('should insert target after last node', 1, function () {
188          $($.create('<p>eyo</p>')).insertAfter($('.after-last-target'));
189          ok(Q('.after-last-target')[0].nextSibling && Q('.after-last-target')[0].nextSibling.tagName.toLowerCase() == 'p', 'created element was inserted after the target (target is last child)');
190        });
191
192        test('get and set html', 2, function () {
193          var fixture = '<p>oh yeeeeeah!</p>';
194          $('#html').html(fixture);
195          ok(Q('#html')[0].innerHTML.toLowerCase() == fixture, 'sets appropriate fixture html');
196          ok($('#html').html().toLowerCase() == fixture, 'gets appropriate fixture html');
197        });
198
199        test('set html on special tags', 2, function () {
200          var fixture = '<option class="bad">i am an option</option>';
201          var fixture2 = '<option>i am an option</option><option>and yet another</option>';
202
203          $('#html-select').append('<select id="select-test-2"/>');
204          $('#select-test-2').html(fixture2);
205          ok($('#select-test-2 option').length == 2, "$('#select-test-2 option').length == 2");
206
207
208          $('#html-select').append('<select id="select-test"/>');
209          $('#select-test').html(fixture);
210          ok($('#select-test option').length == 1, "$('#select-test option').length == 1");
211
212        });
213
214        test('get and set text', 2, function () {
215          var fixture = '<p>boosh</p>';
216          var expected = '&lt;p&gt;boosh&lt;/p&gt;';
217          $('#text').text(fixture);
218          ok(Q('#text')[0].innerHTML.toLowerCase() == expected, 'sets appropriate fixture text');
219          ok($('#text').text().toLowerCase() == fixture, 'gets appropriate fixture text');
220        });
221
222        test('show and hide', 6, function () {
223          ok(Q('#show-hide')[0].offsetWidth > 0, 'element has flow');
224          $('#show-hide').hide();
225          ok(Q('#show-hide')[0].offsetWidth == 0, 'element has no flow');
226          $('#show-hide').show();
227          ok(Q('#show-hide')[0].offsetWidth > 0, 'element has flow');
228          $('#show-hide').hide();
229          ok(Q('#show-hide')[0].offsetWidth == 0, 'element has no flow');
230          $('#show-hide').attr('class','show-hide-css');
231          $('#show-hide').show('block');
232          ok(Q('#show-hide')[0].offsetWidth > 0, 'element has flow');
233          $('#show-hide').hide();
234          $('#show-hide').show('inline');
235          ok($('#show-hide').css('display') == 'inline', 'element has flow');
236        });
237
238        test('offsets', 4, function () {
239          var $el = $(dom.create('<div/>')).css({
240            position: 'absolute',
241            left: '50px',
242            top: -999
243          }).appendTo(document.body);
244          ok($el.offset().left == 50, 'initial offset.left is 50');
245          ok($el.offset().left == 50, 'initial offset.top is -999');
246          $el.offset(100, null);
247          $el.offset(null, -100);
248          ok($el.offset().left == 100, 'after offset(100, null), left == 100');
249          ok($el.offset().top == -100, 'after offset(null, -100), top == -100');
250        });
251
252        test('should augment special methods', 3, function () {
253          ok(!dom('#augment').color, 'bonzo has no "color" method');
254          dom.aug({
255            color: function (color) {
256              ok(this.prototype == dom().prototype, 'calls augmented methods in scope of bonzo prototype');
257              this.css('color', color);
258            }
259          });
260          $('#augment').color('red');
261          ok(!!dom('#augment').color, 'bonzo now has a "color" method');
262        });
263
264        test('viewport width & height', 2, function () {
265          ok(isFinite($.viewport().width), 'has width property');
266          ok(isFinite($.viewport().height), 'has height property');
267        });
268
269        test('document width & height', 2, function () {
270          ok(isFinite($.doc().width), 'has width property');
271          ok(isFinite($.doc().height), 'has height property');
272        });
273
274        test('width & height can be accessed on window and document', 2, function () {
275          ok($(window).css('width') == $(document).css('width'), 'win and doc have same width');
276          ok($(window).css('height') < $(document).css('height'), 'document height is much larger than win height');
277        })
278
279        test('should empty a node without removing node', 2, function () {
280          ok(Q('#empty p').length == 3, 'target has 3 {p} elements');
281          $('#empty').empty();
282          ok(Q('#empty p').length == 0, 'target has 0 {p} elements');
283        });
284
285        test('should detach and return node list', 4, function () {
286          ok(Q('#detach div').length == 2, 'target originally has 2 nodes');
287          var orphans = $('#detach div').detach();
288          ok(Q('#detach div').length == 0, 'target has detached 2 nodes');
289          ok(orphans.length == 2, '2 orphans were returned');
290          ok(!$.isAncestor(document.body, orphans[0]), 'orphans do not exist in document');
291        });
292
293        test('setting & getting attributes', 5, function () {
294          var input = $('#attrs input[type="hidden"]');
295          ok($('#attrs a').attr('href').match(/twitter\.com/), 'retrieves "href" attribute from anchor');
296          ok(input.val() == 'eyo', 'retrieves "value" attribute from {input} element');
297          ok($('#attrs input[type="checkbox"]').attr('checked'), 'retrieves "checked" attribute without value as true');
298          ok(input.attr('value', 'boosh').attr('value') == 'boosh', 'sets value attribute of input');
299          input.val('eyo');
300          ok(input.val() == 'eyo', 'val(val) can set value on input')
301        });
302
303        test('first and last', 2, function () {
304          ok($('#first-last div').first()[0].id == 'first', 'found first() element');
305          ok($('#first-last div').last()[0].id == 'last', 'found last() element');
306        });
307
308        test('appendTo', 1, function () {
309          var node = $.create('<p>i am append-to</p>')[0];
310          var append = Q('#append-to')[0];
311          $(node).appendTo(append);
312          ok($('#append-to p').length == 1, '$("#append-to p").length == 1 - appended to target node');
313        });
314
315        test('prependTo', 3, function () {
316          var node = $.create('<p>world</p>')[0];
317          var node2 = $.create('<p>hello</p>')[0];
318          var prepend = Q('#prepend-to')[0];
319          var prependWithNoFirstChild = Q('#prepend-first-child')[0];
320          $([node, node2]).prependTo(prepend);
321          ok($('#prepend-to p').length == 2, '$("#prepend-to p").length == 2 - prepended to target node');
322          ok($('#prepend-to p').first().html() == 'hello', 'first node is "hello"');
323          ok($('#prepend-to p').last().html() == 'world', 'last node is "world"');
324        });
325
326        test('next', 1, function () {
327          ok($('#sibling-tests li.nextr').next().length == 2, 'els.next().length == 2');
328        });
329
330        test('previous', 1, function () {
331          ok($('#sibling-tests li.nextr').previous().length == 3, 'els.previous().length == 3');
332        });
333
334        test('scrollTop && scrollLeft', 2, function () {
335          // there's gotta be a better way to test this than making the spec page height: 10000px
336          $(window).scrollTop(1);
337          ok($(window).scrollTop() == 1, 'condition1');
338          $('#overflowed').scrollLeft(150);
339          ok($('#overflowed').scrollLeft() == 150, 'condition2');
340        });
341
342        test('setting & getting styles', 3, function() {
343          ok($('#styles').css('margin-left') == '5px', 'margin-left is 5px by default');
344          $('#styles').css('margin-left', 10);
345          ok($('#styles').css('margin-left') == '10px', '10px after update. can also use unitless values');
346          $('#styles').css({marginLeft: '15px'});
347          ok($('#styles').css('margin-left') == '15px', 'blue after setting color with object');
348        });
349
350      });
351
352
353      sink('selector engine', function (test, ok) {
354
355        test('run insert with created nodes', 1, function () {
356          var node = $.create('<p>world</p>')[0];
357          var node2 = $.create('<p>hello</p>')[0];
358          $([node, node2]).prependTo('.prepend-with-engine');
359          ok($('.prepend-with-engine p').length == 4, 'prepends 4 elements total')
360        });
361
362        test('run insert with existing nodes', 2, function () {
363          $('.prepend-with-engine p').prependTo('.prepend-with-engine-move');
364          ok($('.prepend-with-engine p').length == 0, 'prepend now has no elements')
365          ok($('.prepend-with-engine-move p').length == 4, 'elements were moved to target selector')
366        });
367
368      })
369      start();
370    </script>
371
372  </body>
373</html>