142
St
startsInvasion / files
startsInvasion / libs / node_modules / bean / tests / tests.js
14.3 KB
1//stub this for ie crap 2if (!window.console) { 3 window.console = { log: function () {}} 4} 5 6sink('add', function (test, ok) { 7 8 test('add: should return the element passed in', 1, function () { 9 var el = document.getElementById('input'); 10 var returned = bean.add(el, 'click', function () {}); 11 ok(el == returned, 'returns the element passed in'); 12 bean.remove(el); 13 }); 14 15 test('add: should be able to add single events to elements', 1, function () { 16 var el = document.getElementById('input'); 17 bean.add(el, 'click', function () { 18 bean.remove(el); 19 ok(true, 'adds single events to elements'); 20 }); 21 Syn.click(el); 22 }); 23 24 test('add: should be able to add single events to objects', 1, function () { 25 var obj = {}; 26 bean.add(obj, 'complete', function () { 27 ok(true, 'adds single events to objects'); 28 }); 29 bean.fire(obj, 'complete'); 30 bean.remove(obj); 31 bean.fire(obj, 'complete'); 32 }); 33 34 test('add: scope should be equal to element', 1, function () { 35 var el = document.getElementById('input'); 36 bean.add(el, 'click', function (e) { 37 bean.remove(el); 38 ok(this == el, 'equal to element') 39 }); 40 Syn.click(el); 41 }); 42 43 test('add: should recieve an event method', 1, function () { 44 var el = document.getElementById('input'); 45 bean.add(el, 'click', function (e) { 46 bean.remove(el); 47 ok(e != null, 'recieves an event method') 48 }); 49 Syn.click(el); 50 }); 51 52 test('add: should be able to pass x amount of additional arguments', 4, function () { 53 var el = document.getElementById('input'); 54 handler = function (e, foo, bar, baz) { 55 bean.remove(el); 56 ok(e != null, 'listener was called with event'); 57 ok(foo === 1, 'listener was called with correct argument'); 58 ok(bar === 2, 'listener was called with correct argument'); 59 ok(baz === 3, 'listener was called with correct argument'); 60 }; 61 bean.add(el, 'click', handler, 1, 2, 3); 62 Syn.click(el); 63 }); 64 65 test('add: should be able to add multiple events by space seperating them', 2, function () { 66 var el = document.getElementById('input'); 67 bean.add(el, 'click keypress', function () { 68 ok(true, 'adds multiple events by space seperating them'); 69 }); 70 Syn.click(el).key('j'); 71 }); 72 73 test('add: should add same event only one time', 1, function () { 74 var el = document.getElementById('input'); 75 bean.remove(el); 76 var handler = function () {ok(true, 'adds same event only one time')}; 77 bean.add(el, 'click', handler); 78 bean.add(el, 'click', handler); 79 bean.add(el, 'click', handler); 80 Syn.click(el); 81 }); 82 83 test('add: should be able to add multiple events of the same type', 3, function () { 84 var el = document.getElementById('input'); 85 bean.remove(el); 86 bean.add(el, 'click', function () {ok(true, 'adds multiple events of the same type 1')}); 87 bean.add(el, 'click', function () {ok(true, 'adds multiple events of the same type 2')}); 88 bean.add(el, 'click', function () {ok(true, 'adds multiple events of the same type 3')}); 89 Syn.click(el); 90 }); 91 92 test('add: should be able to add multiple events simultaneously with an object literal', 2, function () { 93 var el = document.getElementById('input'); 94 bean.remove(el); 95 bean.add(el, { 96 click: function () { 97 ok(true, 'adds multiple events simultaneously with an object literal 1'); 98 }, 99 keydown: function () { 100 ok(true, 'adds multiple events simultaneously with an object literal 2'); 101 bean.remove(el); 102 } 103 }); 104 Syn.click(el).key('j'); 105 }); 106 107 test('add: should bubble up dom', 1, function () { 108 var el1 = document.getElementById('foo'); 109 var el2 = document.getElementById('bar'); 110 bean.add(el1, 'click', function () {ok(true, 'bubbles up dom')}); 111 Syn.click(el2); 112 }); 113 114}) 115 116sink('fire', function (test, ok) { 117 118 test('fire: should be able to fire an event', 1, function () { 119 var el = document.getElementById('input'); 120 bean.remove(el); 121 bean.add(el, 'click', function () {ok(true, 'fires an event')}); 122 bean.fire(el, 'click'); 123 }); 124 125 test('fire: should be able to fire multiple events by space seperation', 2, function () { 126 var el = document.getElementById('input'); 127 bean.remove(el); 128 bean.add(el, 'mousedown', function () {ok(true, 'fires multiple events by space seperation 1')}); 129 bean.add(el, 'mouseup', function () {ok(true, 'fires multiple events by space seperation 2')}); 130 bean.fire(el, 'mousedown mouseup'); 131 }); 132 133}) 134 135sink('custom', function (test, ok) { 136 137 test('custom: should be able to add single custom events', 1, function () { 138 var el = document.getElementById('input'); 139 bean.remove(el); 140 bean.add(el, 'partytime', function () { 141 ok(true, 'add single custom events'); 142 }); 143 bean.fire(el, 'partytime'); 144 }); 145 146 test('custom: should bubble up dom like traditional events', 1, function () { 147 if (!window.addEventListener) { 148 //dean edwards onpropertychange hack doesn't bubble unfortunately :( 149 return ok(true, 'internet explorer is not a bubler, turns out.') 150 } 151 var el1 = document.getElementById('foo'); 152 var el2 = document.getElementById('bar'); 153 bean.add(el1, 'partytime', function () {ok(true, 'bubbles up dom like traditional events')}); 154 bean.fire(el2, 'partytime'); 155 }); 156 157}) 158 159sink('event object', function (test, ok) { 160 161 test('event: should have correct target', 1, function () { 162 var el1 = document.getElementById('foo'); 163 var el2 = document.getElementById('bar'); 164 bean.remove(el1, 'click'); 165 bean.add(el1, 'click', function (e) {ok(e.target == el2, 'has correct target')}); 166 Syn.click(el2); 167 }); 168 169 test('event: should have stop propagation method', 1, function () { 170 var el = document.getElementById('foo'); 171 bean.remove(el); 172 bean.add(el, 'click', function (e) {ok(e.stopPropagation != null, 'has stop propagation')}); 173 Syn.click(el); 174 }); 175 176 test('event: should have preventDefault method', 1, function () { 177 var el = document.getElementById('foo'); 178 bean.remove(el); 179 bean.add(el, 'click', function (e) {ok(e.preventDefault != null, 'has prevent default method')}); 180 Syn.click(el); 181 }); 182 183 test('event: should have keyCode', 1, function () { 184 var el = document.getElementById('input'); 185 bean.add(el, 'keypress', function (e) { 186 bean.remove(el); 187 ok(e.keyCode != null, 'has keycode'); 188 }); 189 Syn.key(el, 'f'); 190 }); 191 192}) 193 194sink('remove', function (test, ok) { 195 196 test('remove: should return the element passed in', 1, function () { 197 var el = document.getElementById('foo'); 198 bean.remove(el); 199 var handler = function () {}; 200 bean.add(el, 'click', handler); 201 var returned = bean.remove(el, 'click', handler); 202 ok(el == returned, 'returns the element passed in'); 203 }); 204 205 test('remove: should be able to remove a single event', 1, function () { 206 var el = document.getElementById('foo'); 207 bean.remove(el); 208 var handler = function () { 209 ok(true, 'remove a single event'); 210 bean.remove(el, 'click', handler); 211 Syn.click(el); 212 } 213 bean.add(el, 'click', handler); 214 Syn.click(el) 215 }); 216 217 test('remove: should be able to remove mulitple events with an object literal', 1, function () { 218 var el = document.getElementById('input'), 219 handler1 = function () { 220 ok(true, 'remove mulitple events with an object literal1'); 221 bean.remove(el, { 222 click: handler1, 223 keydown: handler2 224 }); 225 Syn.click(el).key('j'); 226 }, 227 handler2 = function () { 228 ok(true, 'remove mulitple events with an object literal2'); 229 }; 230 bean.add(el, 'click', handler1); 231 bean.add(el, 'keydown', handler2); 232 Syn.click(el); 233 }); 234 235 test('remove: should be able to remove all events of a specific type', 2, function () { 236 var el = document.getElementById('input'); 237 bean.remove(el); 238 var handler1 = function () { 239 ok(true, 'removes all events of a specific type 1'); 240 }, 241 handler2 = function () { 242 ok(true, 'removes all events of a specific type 2'); 243 bean.remove(el, 'click'); 244 Syn.click(el); 245 }; 246 bean.add(el, 'click', handler1); 247 bean.add(el, 'click', handler2); 248 Syn.click(el); 249 }); 250 251 test('remove: should be able to remove all events of a specific type', 2, function () { 252 var el = document.getElementById('input'); 253 bean.remove(el); 254 var handler1 = function () { 255 ok(true, 'removes all events of a specific type 1'); 256 }, 257 handler2 = function () { 258 ok(true, 'remove all events of a specific type 2'); 259 bean.remove(el, 'mousedown mouseup'); 260 Syn.click(el); 261 }; 262 bean.add(el, 'mousedown', handler1); 263 bean.add(el, 'mouseup', handler2); 264 Syn.click(el); 265 }); 266 267 test('remove: should be able to remove all events', 1, function () { 268 var el = document.getElementById('input'), 269 handler1 = function () { 270 ok(true, 'remove all events 1'); 271 bean.remove(el); 272 Syn.click(el).key('j'); 273 }, 274 handler2 = function () { 275 ok(true, 'remove all events 2'); 276 }; 277 bean.add(el, 'click', handler1); 278 bean.add(el, 'keydown', handler2); 279 Syn.click(el); 280 }); 281 282}) 283 284sink('clone', function (test, ok, before) { 285 286 var el1 = document.getElementById('input'); 287 var el2 = document.getElementById('input2'); 288 289 before(function () { 290 bean.remove(el1); 291 bean.remove(el2); 292 }) 293 294 test('clone: should be able to clone events of a specific type from one element to another', 2, function () { 295 bean.add(el2, 'click', function () {ok(true, 'clones events of a specific type from one element to another 1')}); 296 bean.add(el2, 'click', function () { 297 ok(true, 'clone events of a specific type from one element to another 2'); 298 bean.remove(el1); 299 }); 300 bean.add(el2, 'keydown', function () { 301 ok(true, 'clone events of a specific type from one element to another 3'); 302 bean.remove(el1); 303 }); 304 bean.clone(el1, el2, 'click'); 305 Syn.click(el1).key('j'); 306 }); 307 308 test('clone: should be able to clone all events from one element to another', 3, function () { 309 bean.add(el2, 'keypress', function () {ok(true, 'clones all events from one element to another 1');}); 310 bean.add(el2, 'click', function () {ok(true, 'clones all events from one element to another 2');}); 311 bean.add(el2, 'click', function () {ok(true, 'clonesall events from one element to another 3');}); 312 bean.clone(el1, el2); 313 Syn.click(el1).key('j'); 314 }); 315 316 test('clone: should fire cloned event in scope of new element', 1, function () { 317 bean.add(el1, 'click', function () { 318 ok(this == el2, 'scope of "this" is the element that cloned the event') 319 }) 320 bean.clone(el2, el1) 321 Syn.click(el2) 322 }); 323 324}) 325 326sink('delegation', function (test, ok) { 327 328 test('delegate: should be able to delegate on selectors', 4, function () { 329 var el1 = document.getElementById('foo'); 330 var el2 = document.getElementById('bar'); 331 var el3 = document.getElementById('baz'); 332 var el4 = document.getElementById('bang'); 333 bean.remove(el1); 334 bean.remove(el2); 335 bean.remove(el3); 336 bean.add(el1, '.bar', 'click', function () { 337 ok(true, 'delegation on selectors 1'); 338 ok(this == el2, 'delegation on selectors, context was set to delegated element 2'); 339 }, qwery); 340 Syn.click(el2); 341 Syn.click(el3); 342 Syn.click(el4); 343 }); 344 345 test('delegate: should be able to delegate on arary', 4, function () { 346 var el1 = document.getElementById('foo'); 347 var el2 = document.getElementById('bar'); 348 var el3 = document.getElementById('baz'); 349 var el4 = document.getElementById('bang'); 350 bean.remove(el1); 351 bean.remove(el2); 352 bean.remove(el3); 353 bean.add(el1, [el2], 'click', function () { 354 ok(true, 'delegation on arary 1'); 355 ok(this == el2, 'delegation on arary, context was set to delegated element 1'); 356 }, qwery); 357 Syn.click(el2); 358 Syn.click(el3); 359 Syn.click(el4); 360 }); 361}) 362 363sink('namespaces', function (test, ok) { 364 365 test('namespace: should be able to name handlers', 1, function () { 366 var el1 = document.getElementById('foo'); 367 bean.remove(el1); 368 bean.add(el1, 'click.fat', function () {ok(true, 'bubbles up dom')}); 369 Syn.click(el1); 370 }); 371 372 test('namespace: should be able to fire an event without handlers', 1, function () { 373 var el1 = document.getElementById('foo'), succ; 374 bean.remove(el1); 375 try { 376 bean.fire(el1, 'click.fat'); 377 succ = true; 378 } catch (exc) { 379 succ = false; 380 } 381 ok(succ, 'fire namespaced event with no handlers'); 382 }); 383 384 test('namespace: should be able to target namespaced event handlers with fire', 1, function () { 385 var el1 = document.getElementById('foo'); 386 bean.remove(el1); 387 bean.add(el1, 'click.fat', function () {ok(true, 'targets namespaced event handlers with fire')}); 388 bean.add(el1, 'click', function () {ok(true, 'targets namespaced event handlers with fire')}); 389 bean.fire(el1, 'click.fat'); 390 }); 391 392 test('namespace: should be able to target multiple namespaced event handlers with fire', 2, function () { 393 var el1 = document.getElementById('foo'); 394 bean.remove(el1); 395 bean.add(el1, 'click.fat', function () {ok(true, 'target multiple namespaced event handlers with fire')}); 396 bean.add(el1, 'click.ded', function () {ok(true, 'targets multiple namespaced event handlers with fire')}); 397 bean.add(el1, 'click', function () {ok(true, 'targets multiple namespaced event handlers with fire')}); 398 bean.fire(el1, 'click.fat.ded'); 399 }); 400 401 test('namespace: should be able to remove handlers based on name', 1, function () { 402 var el1 = document.getElementById('foo'); 403 bean.remove(el1); 404 bean.add(el1, 'click.ded', function () {ok(true, 'removes handlers based on name')}); 405 bean.add(el1, 'click', function () {ok(true, 'removes handlers based on name')}); 406 bean.remove(el1, 'click.ded'); 407 Syn.click(el1); 408 }); 409 410 test('namespace: should be able to remove multiple handlers based on name', 1, function () { 411 var el1 = document.getElementById('foo'); 412 bean.remove(el1); 413 bean.add(el1, 'click.fat', function () {ok(true, 'removes multiple handlers based on name')}); 414 bean.add(el1, 'click.ded', function () {ok(true, 'removes multiple handlers based on name')}); 415 bean.add(el1, 'click', function () {ok(true, 'removes multiple handlers based on name')}); 416 bean.remove(el1, 'click.ded.fat'); 417 Syn.click(el1); 418 }); 419 420}); 421 422window.onload = start;