How to use target method in differencify

Best JavaScript code snippet using differencify

event-custom-base-tests.js

Source:event-custom-base-tests.js Github

copy

Full Screen

1YUI.add('event-custom-base-tests', function(Y) {2// Continue on line 36453var baseSuite = new Y.Test.Suite("Custom Event: Base"),4 keys = Y.Object.keys;5baseSuite.add(new Y.Test.Case({6 name: "Event Target Constructor",7 "test new Y.EventTarget()": function () {8 var target = new Y.EventTarget();9 Y.Assert.isInstanceOf(Y.EventTarget, target);10 Y.Assert.isObject(target._yuievt);11 },12 "test new Y.EventTarget(config)": function () {13 var target1 = new Y.EventTarget(),14 target2 = new Y.EventTarget({15 broadcast: 2,16 bubbles: false,17 context: target1,18 defaultTargetOnly: true,19 emitFacade: true,20 fireOnce: true,21 monitored: true,22 queuable: true,23 async: true24 }),25 config1 = target1._yuievt,26 config2 = target2._yuievt;27 Y.Assert.isObject(config1.events);28 Y.Assert.isNull(config1.targets);29 Y.Assert.isObject(config1.config);30 Y.Assert.isUndefined(config1.bubbling);31 Y.Assert.isUndefined(config1.config.broadcast);32 Y.Assert.areSame(target1, config2.config.context);33 Y.Assert.isUndefined(config1.config.defaultTargetOnly);34 Y.Assert.isUndefined(config1.config.emitFacade);35 Y.Assert.isUndefined(config1.config.fireOnce);36 Y.Assert.isUndefined(config1.config.monitored);37 Y.Assert.isUndefined(config1.config.queuable);38 Y.Assert.areSame(2, config2.config.broadcast);39 Y.Assert.isFalse(config2.config.bubbles);40 Y.Assert.areSame(target1, config2.config.context);41 Y.Assert.isTrue(config2.config.defaultTargetOnly);42 Y.Assert.isTrue(config2.config.emitFacade);43 Y.Assert.isTrue(config2.config.fireOnce);44 Y.Assert.isTrue(config2.config.monitored);45 Y.Assert.isTrue(config2.config.queuable);46 Y.Assert.isTrue(config2.config.async);47 },48 "test Y.augment(Clz, Y.EventTarget)": function () {49 var instance,50 thisObj;51 function TestClass1() {}52 TestClass1.prototype = {53 method: function() {54 thisObj = this;55 }56 };57 Y.augment(TestClass1, Y.EventTarget);58 instance = new TestClass1();59 Y.Assert.isUndefined(instance._yuievt);60 Y.Assert.isFunction(instance.on);61 Y.Assert.areNotSame(instance.on, Y.EventTarget.prototype.on);62 instance.on("test", instance.method);63 instance.fire("test");64 Y.Assert.isObject(instance._yuievt);65 Y.Assert.isUndefined(instance._yuievt.config.fireOnce);66 Y.Assert.areSame(instance.on, Y.EventTarget.prototype.on);67 Y.Assert.areSame(instance, thisObj);68 function TestClass2() {}69 TestClass2.prototype = {70 method: function () {71 thisObj = this;72 }73 };74 Y.augment(TestClass2, Y.EventTarget, true, null, {75 fireOnce: true76 });77 instance = new TestClass2();78 thisObj = null;79 Y.Assert.isUndefined(instance._yuievt);80 Y.Assert.isFunction(instance.on);81 Y.Assert.areNotSame(instance.on, Y.EventTarget.prototype.on);82 instance.on("test", instance.method);83 instance.fire("test");84 Y.Assert.isObject(instance._yuievt);85 Y.Assert.isTrue(instance._yuievt.config.fireOnce);86 Y.Assert.areSame(instance.on, Y.EventTarget.prototype.on);87 Y.Assert.areSame(instance, thisObj);88 },89 "test Y.extend(Clz, Y.EventTarget)": function () {90 var instance, thisObj;91 function TestClass() {92 TestClass.superclass.constructor.apply(this, arguments);93 }94 Y.extend(TestClass, Y.EventTarget, {95 method: function () {96 thisObj = this;97 }98 });99 instance = new TestClass();100 Y.Assert.isInstanceOf(TestClass, instance);101 Y.Assert.isInstanceOf(Y.EventTarget, instance);102 Y.Assert.isObject(instance._yuievt);103 Y.Assert.areSame(instance.on, Y.EventTarget.prototype.on);104 instance.on("test", instance.method);105 instance.fire("test");106 Y.Assert.areSame(instance, thisObj);107 }108}));109baseSuite.add(new Y.Test.Case({110 name: "target.on()",111 _should: {112 ignore: {113 // As of 3.4.1, creates a subscription to a custom event named114 // "[object Object]"115 "test target.on([{ fn: fn, context: obj }]) does nothing": true,116 // Not (yet) implemented117 "test target.on(type, { handleEvents: fn })": true118 }119 },120 "test auto-publish on subscribe": function () {121 var target = new Y.EventTarget(),122 events = target._yuievt.events,123 publishCalled;124 target.publish = (function (original) {125 return function (type) {126 if (type === 'test') {127 publishCalled = true;128 }129 return original.apply(this, arguments);130 };131 })(target.publish);132 Y.Assert.isUndefined(events.test);133 target.on("test", function () {});134 Y.Assert.isTrue(publishCalled);135 Y.Assert.isObject(events.test);136 Y.Assert.isInstanceOf(Y.CustomEvent, events.test);137 },138 "test target.on(type, fn)": function () {139 var target = new Y.EventTarget(),140 events = target._yuievt.events,141 subs,142 subscribers,143 afters,144 handle,145 thisObj,146 fired,147 argCount,148 testEvent;149 function callback() {150 fired = true;151 thisObj = this;152 argCount = arguments.length;153 }154 handle = target.on("test", callback);155 testEvent = events.test;156 subs = testEvent.getSubs();157 subscribers = subs[0];158 afters = subs[1];159 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);160 Y.Assert.isArray(subscribers);161 Y.Assert.areSame(1, subscribers.length);162 Y.Assert.areSame(0, afters.length);163 Y.Assert.areSame(1, testEvent.hasSubs());164 Y.Assert.isInstanceOf(Y.EventHandle, handle);165 Y.Assert.areSame(testEvent, handle.evt);166 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);167 target.fire("test");168 Y.Assert.isTrue(fired);169 Y.Assert.areSame(target, thisObj);170 Y.Assert.areSame(0, argCount);171 // Test that fire() did not change the subscription state of the172 // custom event173 testEvent = events.test;174 subs = testEvent.getSubs();175 subscribers = subs[0];176 afters = subs[1];177 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);178 Y.Assert.isArray(subscribers);179 Y.Assert.areSame(1, subscribers.length);180 Y.Assert.areSame(0, afters.length);181 Y.Assert.areSame(1, testEvent.hasSubs());182 },183 "test target.on(type, fn) allows duplicate subs": function () {184 var target = new Y.EventTarget(),185 events = target._yuievt.events,186 count = 0,187 subs,188 subscribers,189 afters,190 testEvent, handle1, handle2;191 function callback() {192 count++;193 }194 handle1 = target.on("test", callback);195 handle2 = target.on("test", callback);196 testEvent = events.test;197 subs = testEvent.getSubs();198 subscribers = subs[0];199 afters = subs[1];200 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);201 Y.Assert.isArray(subscribers);202 Y.Assert.areSame(2, subscribers.length);203 Y.Assert.areSame(0, afters.length);204 Y.Assert.areSame(2, testEvent.hasSubs());205 Y.Assert.areNotSame(handle1, handle2);206 Y.Assert.areSame(testEvent, handle1.evt);207 Y.Assert.areSame(handle1.evt, handle2.evt);208 Y.Assert.areNotSame(handle1.sub, handle2.sub);209 target.fire("test");210 Y.Assert.areSame(2, count);211 // Test that fire() did not change the subscription state of the212 // custom event213 testEvent = events.test;214 subs = testEvent.getSubs();215 subscribers = subs[0];216 afters = subs[1];217 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);218 Y.Assert.isArray(subscribers);219 Y.Assert.areSame(2, subscribers.length);220 Y.Assert.areSame(0, afters.length);221 Y.Assert.areSame(2, testEvent.hasSubs());222 },223 "test target.on(type, fn, obj)": function () {224 var target = new Y.EventTarget(),225 obj = {},226 count = 0,227 thisObj1, thisObj2, argCount, testEvent;228 function callback() {229 count++;230 thisObj1 = this;231 argCount = arguments.length;232 }233 target.on("test", callback, obj);234 target.fire("test");235 Y.Assert.areSame(1, count);236 Y.Assert.areSame(obj, thisObj1);237 Y.Assert.areSame(0, argCount);238 target.on("test", function () {239 thisObj2 = this;240 });241 target.fire("test");242 Y.Assert.areSame(2, count);243 Y.Assert.areSame(obj, thisObj1);244 Y.Assert.areSame(target, thisObj2);245 Y.Assert.areSame(0, argCount);246 },247 "test target.on(type, fn, obj, args)": function () {248 var target = new Y.EventTarget(),249 obj = {},250 count = 0,251 args = '',252 argCount,253 thisObj1, thisObj2, testEvent;254 function callback() {255 count++;256 thisObj1 = this;257 argCount = arguments.length;258 for (var i = 0, len = argCount; i < len; ++i) {259 args += arguments[i];260 }261 }262 target.on("test", callback, obj, "A");263 target.fire("test");264 Y.Assert.areSame(1, count);265 Y.Assert.areSame(obj, thisObj1);266 Y.Assert.areSame("A", args);267 target.on("test", function () {268 thisObj2 = this;269 });270 target.fire("test");271 Y.Assert.areSame(2, count);272 Y.Assert.areSame(obj, thisObj1);273 Y.Assert.areSame(target, thisObj2);274 Y.Assert.areSame(1, argCount);275 },276 "test target.on([type], fn)": function () {277 var target = new Y.EventTarget(),278 events = target._yuievt.events,279 subs,280 subscribers,281 afters,282 handle, thisObj, fired, argCount, testEvent;283 function callback() {284 fired = true;285 thisObj = this;286 argCount = arguments.length;287 }288 handle = target.on(["test"], callback);289 testEvent = events.test;290 subs = testEvent.getSubs();291 subscribers = subs[0];292 afters = subs[1];293 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);294 Y.Assert.isArray(subscribers);295 Y.Assert.areSame(1, subscribers.length);296 Y.Assert.areSame(0, afters.length);297 Y.Assert.areSame(1, testEvent.hasSubs());298 Y.Assert.isInstanceOf(Y.EventHandle, handle);299 Y.Assert.isArray(handle.evt);300 Y.Assert.areSame(testEvent, handle.evt[0].evt);301 Y.Assert.isUndefined(handle.sub);302 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);303 target.fire("test");304 Y.Assert.isTrue(fired);305 Y.Assert.areSame(target, thisObj);306 Y.Assert.areSame(0, argCount);307 },308 "test target.on([typeA, typeB], fn)": function () {309 var target = new Y.EventTarget(),310 events = target._yuievt.events,311 count = 0,312 subs,313 subscribers,314 afters,315 handle, thisObj, testEvent1, testEvent2;316 function callback() {317 count++;318 thisObj = this;319 }320 handle = target.on(["test1", "test2"], callback);321 testEvent1 = events.test1;322 subs = testEvent1.getSubs();323 subscribers = subs[0];324 afters = subs[1];325 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent1);326 Y.Assert.isArray(subscribers);327 Y.Assert.areSame(1, subscribers.length);328 Y.Assert.areSame(0, afters.length);329 Y.Assert.areSame(1, testEvent1.hasSubs());330 testEvent2 = events.test2;331 subs = testEvent2.getSubs();332 subscribers = subs[0];333 afters = subs[1];334 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent2);335 Y.Assert.isArray(subscribers);336 Y.Assert.areSame(1, subscribers.length);337 Y.Assert.areSame(0, afters.length);338 Y.Assert.areSame(1, testEvent2.hasSubs());339 Y.Assert.isInstanceOf(Y.EventHandle, handle);340 Y.Assert.isArray(handle.evt);341 Y.Assert.areSame(testEvent1, handle.evt[0].evt);342 Y.Assert.areSame(testEvent2, handle.evt[1].evt);343 Y.Assert.areNotSame(testEvent1, testEvent2);344 Y.Assert.isUndefined(handle.sub);345 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);346 target.fire("test1");347 Y.Assert.areSame(1, count);348 Y.Assert.areSame(target, thisObj);349 target.fire("test2");350 Y.Assert.areSame(2, count);351 Y.Assert.areSame(target, thisObj);352 },353 "test target.on([typeA, typeA], fn)": function () {354 var target = new Y.EventTarget(),355 events = target._yuievt.events,356 count = 0,357 subs,358 subscribers,359 afters,360 handle, thisObj, testEvent;361 function callback() {362 count++;363 thisObj = this;364 }365 handle = target.on(["test", "test"], callback);366 testEvent = events.test;367 subs = testEvent.getSubs();368 subscribers = subs[0];369 afters = subs[1];370 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);371 Y.Assert.isArray(subscribers);372 Y.Assert.areSame(2, subscribers.length);373 Y.Assert.areSame(0, afters.length);374 Y.Assert.areSame(2, testEvent.hasSubs());375 Y.Assert.isInstanceOf(Y.EventHandle, handle);376 Y.Assert.isArray(handle.evt);377 Y.Assert.areSame(testEvent, handle.evt[0].evt);378 Y.Assert.areSame(testEvent, handle.evt[1].evt);379 Y.Assert.isUndefined(handle.sub);380 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);381 target.fire("test");382 Y.Assert.areSame(2, count);383 Y.Assert.areSame(target, thisObj);384 },385 "test target.on([], fn) does nothing": function () {386 var target = new Y.EventTarget(),387 events = target._yuievt.events,388 count = 0,389 handle, name, subs, i;390 function callback() {391 Y.Assert.fail("I don't know how this got called");392 }393 handle = target.on([], callback);394 for (name in events) {395 if (events.hasOwnProperty(name)) {396 subs = events[name]._subscribers;397 if (subs) {398 for (i = subs.length - 1; i >= 0; --i) {399 if (subs[i].fn === callback) {400 Y.Assert.fail("subscription registered for '" + name + "' event");401 }402 }403 }404 }405 }406 Y.Assert.isInstanceOf(Y.EventHandle, handle);407 Y.Assert.isArray(handle.evt);408 Y.Assert.areSame(0, handle.evt.length);409 Y.Assert.isUndefined(handle.sub);410 },411 "test target.on([{ fn: fn, context: obj }]) does nothing": function () {412 var target = new Y.EventTarget(),413 events = target._yuievt.events,414 count = 0,415 handle, name, subs, i;416 function callback() {417 Y.Assert.fail("I don't know how this got called");418 }419 handle = target.on([{ fn: callback, context: {} }]);420 for (name in events) {421 if (events.hasOwnProperty(name)) {422 subs = events[name]._subscribers;423 if (subs) {424 for (i = subs.length - 1; i >= 0; --i) {425 if (subs[i].fn === callback) {426 Y.Assert.fail("subscription registered for '" + name + "' event");427 }428 }429 }430 }431 }432 Y.Assert.isInstanceOf(Y.EventHandle, handle);433 Y.Assert.isArray(handle.evt);434 Y.Assert.areSame(0, handle.evt.length);435 Y.Assert.isUndefined(handle.sub);436 },437 "test target.on({ type: fn })": function () {438 var target = new Y.EventTarget(),439 events = target._yuievt.events,440 subs,441 subscribers,442 afters,443 handle, thisObj, fired, argCount, testEvent;444 function callback() {445 fired = true;446 thisObj = this;447 argCount = arguments.length;448 }449 handle = target.on({ "test1": callback });450 testEvent = events.test1;451 subs = testEvent.getSubs();452 subscribers = subs[0];453 afters = subs[1];454 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);455 Y.Assert.isArray(subscribers);456 Y.Assert.areSame(1, subscribers.length);457 Y.Assert.areSame(0, afters.length);458 Y.Assert.areSame(1, testEvent.hasSubs());459 Y.Assert.isInstanceOf(Y.EventHandle, handle);460 Y.Assert.isArray(handle.evt);461 Y.Assert.areSame(1, handle.evt.length);462 Y.Assert.areSame(testEvent, handle.evt[0].evt);463 Y.Assert.isUndefined(handle.sub);464 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);465 target.fire("test1");466 Y.Assert.isTrue(fired);467 Y.Assert.areSame(target, thisObj);468 Y.Assert.areSame(0, argCount);469 handle = target.on({470 "test2": callback,471 "test3": callback472 });473 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);474 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);475 Y.Assert.areSame(1, events.test2._subscribers.length);476 Y.Assert.areSame(1, events.test3._subscribers.length);477 Y.Assert.isInstanceOf(Y.EventHandle, handle);478 Y.Assert.isArray(handle.evt);479 Y.Assert.areSame(2, handle.evt.length);480 Y.Assert.areSame(events.test2, handle.evt[0].evt);481 Y.Assert.areSame(events.test3, handle.evt[1].evt);482 Y.Assert.isUndefined(handle.sub);483 },484 "test target.on({ type: true }, fn)": function () {485 var target = new Y.EventTarget(),486 events = target._yuievt.events,487 subs,488 subscribers,489 afters,490 handle, thisObj, fired, argCount, testEvent;491 function callback() {492 fired = true;493 thisObj = this;494 argCount = arguments.length;495 }496 handle = target.on({ "test1": true }, callback);497 testEvent = events.test1;498 subs = testEvent.getSubs();499 subscribers = subs[0];500 afters = subs[1];501 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);502 Y.Assert.isArray(subscribers);503 Y.Assert.areSame(1, subscribers.length);504 Y.Assert.areSame(0, afters.length);505 Y.Assert.areSame(1, testEvent.hasSubs());506 Y.Assert.isInstanceOf(Y.EventHandle, handle);507 Y.Assert.isArray(handle.evt);508 Y.Assert.areSame(1, handle.evt.length);509 Y.Assert.areSame(testEvent, handle.evt[0].evt);510 Y.Assert.isUndefined(handle.sub);511 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);512 target.fire("test1");513 Y.Assert.isTrue(fired);514 Y.Assert.areSame(target, thisObj);515 Y.Assert.areSame(0, argCount);516 handle = target.on({ "test2": 1, "test3": false }, callback);517 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);518 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);519 Y.Assert.areSame(1, events.test2._subscribers.length);520 Y.Assert.areSame(1, events.test3._subscribers.length);521 Y.Assert.isInstanceOf(Y.EventHandle, handle);522 Y.Assert.isArray(handle.evt);523 Y.Assert.areSame(2, handle.evt.length);524 Y.Assert.areSame(events.test2, handle.evt[0].evt);525 Y.Assert.areSame(events.test3, handle.evt[1].evt);526 Y.Assert.isUndefined(handle.sub);527 },528 "test target.on({ type: { fn: wins } }, fn)": function () {529 var target = new Y.EventTarget(),530 events = target._yuievt.events,531 subs,532 subscribers,533 afters,534 handle, thisObj, fired, argCount, testEvent;535 function callback() {536 fired = true;537 thisObj = this;538 argCount = arguments.length;539 }540 handle = target.on({ "test1": { fn: callback } }, function () {541 Y.Assert.fail("This callback should not have been called.");542 });543 testEvent = events.test1;544 subs = testEvent.getSubs();545 subscribers = subs[0];546 afters = subs[1];547 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);548 Y.Assert.isArray(subscribers);549 Y.Assert.areSame(1, subscribers.length);550 Y.Assert.areSame(0, afters.length);551 Y.Assert.areSame(1, testEvent.hasSubs());552 Y.Assert.isInstanceOf(Y.EventHandle, handle);553 Y.Assert.isArray(handle.evt);554 Y.Assert.areSame(1, handle.evt.length);555 Y.Assert.areSame(testEvent, handle.evt[0].evt);556 Y.Assert.isUndefined(handle.sub);557 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);558 target.fire("test1");559 Y.Assert.isTrue(fired);560 Y.Assert.areSame(target, thisObj);561 Y.Assert.areSame(0, argCount);562 },563 "test target.on({ type: { fn: wins } }, fn, obj, args)": function () {564 var target = new Y.EventTarget(),565 obj = {},566 subs,567 subscribers,568 afters,569 events = target._yuievt.events,570 handle, thisObj, fired, argCount, testEvent;571 function callback() {572 fired = true;573 thisObj = this;574 argCount = arguments.length;575 }576 handle = target.on({ "test1": { fn: callback } }, function () {577 Y.Assert.fail("This callback should not have been called.");578 }, obj, 'ARG!');579 testEvent = events.test1;580 subs = testEvent.getSubs();581 subscribers = subs[0];582 afters = subs[1];583 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);584 Y.Assert.isArray(subscribers);585 Y.Assert.areSame(1, subscribers.length);586 Y.Assert.areSame(0, afters.length);587 Y.Assert.areSame(1, testEvent.hasSubs());588 Y.Assert.isInstanceOf(Y.EventHandle, handle);589 Y.Assert.isArray(handle.evt);590 Y.Assert.areSame(1, handle.evt.length);591 Y.Assert.areSame(testEvent, handle.evt[0].evt);592 Y.Assert.isUndefined(handle.sub);593 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);594 target.fire("test1");595 Y.Assert.isTrue(fired);596 Y.Assert.areSame(obj, thisObj);597 Y.Assert.areSame(1, argCount);598 },599 "test target.on({ type: { fn: wins, context: wins } }, fn, ctx, args)": function () {600 var target = new Y.EventTarget(),601 obj = {},602 subs,603 subscribers,604 afters,605 events = target._yuievt.events,606 handle, thisObj, fired, argCount, testEvent;607 function callback() {608 fired = true;609 thisObj = this;610 argCount = arguments.length;611 }612 handle = target.on({ "test1": { fn: callback, context: obj } },613 function () {614 Y.Assert.fail("This callback should not have been called.");615 }, {}, 'ARG!');616 testEvent = events.test1;617 subs = testEvent.getSubs();618 subscribers = subs[0];619 afters = subs[1];620 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);621 Y.Assert.isArray(subscribers);622 Y.Assert.areSame(1, subscribers.length);623 Y.Assert.areSame(0, afters.length);624 Y.Assert.areSame(1, testEvent.hasSubs());625 Y.Assert.isInstanceOf(Y.EventHandle, handle);626 Y.Assert.isArray(handle.evt);627 Y.Assert.areSame(1, handle.evt.length);628 Y.Assert.areSame(testEvent, handle.evt[0].evt);629 Y.Assert.isUndefined(handle.sub);630 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);631 target.fire("test1");632 Y.Assert.isTrue(fired);633 Y.Assert.areSame(obj, thisObj);634 Y.Assert.areSame(1, argCount);635 },636 "test target.on({ type: { context: wins } }, callback, ctx, args)": function () {637 var target = new Y.EventTarget(),638 obj = {},639 subs,640 subscribers,641 afters,642 events = target._yuievt.events,643 handle, thisObj, fired, argCount, testEvent;644 function callback() {645 fired = true;646 thisObj = this;647 argCount = arguments.length;648 }649 handle = target.on({ "test1": { context: obj } }, callback, {}, 'ARG!');650 testEvent = events.test1;651 subs = testEvent.getSubs();652 subscribers = subs[0];653 afters = subs[1];654 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);655 Y.Assert.isArray(subscribers);656 Y.Assert.areSame(1, subscribers.length);657 Y.Assert.areSame(0, afters.length);658 Y.Assert.areSame(1, testEvent.hasSubs());659 Y.Assert.isInstanceOf(Y.EventHandle, handle);660 Y.Assert.isArray(handle.evt);661 Y.Assert.areSame(1, handle.evt.length);662 Y.Assert.areSame(testEvent, handle.evt[0].evt);663 Y.Assert.isUndefined(handle.sub);664 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);665 target.fire("test1");666 Y.Assert.isTrue(fired);667 Y.Assert.areSame(obj, thisObj);668 Y.Assert.areSame(1, argCount);669 },670 "test target.on(type, { handleEvents: fn })": function () {671 var target = new Y.EventTarget(),672 events = target._yuievt.events,673 subs,674 subscribers,675 afters,676 obj, handle, thisObj, fired, argCount, testEvent;677 function callback() {678 fired = true;679 thisObj = this;680 argCount = arguments.length;681 }682 obj = { handleEvents: callback };683 handle = target.on("test", obj);684 testEvent = events.test;685 subs = testEvent.getSubs();686 subscribers = subs[0];687 afters = subs[1];688 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);689 Y.Assert.isArray(subscribers);690 Y.Assert.areSame(1, subscribers.length);691 Y.Assert.areSame(0, afters.length);692 Y.Assert.areSame(1, testEvent.hasSubs());693 Y.Assert.isInstanceOf(Y.EventHandle, handle);694 Y.Assert.areSame(testEvent, handle.evt);695 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);696 // Barring support, this is where the error will be thrown.697 // ET.on() doesn't verify the second arg is a function, and698 // Subscriber doesn't type check before treating it as a function.699 target.fire("test");700 Y.Assert.isTrue(fired);701 Y.Assert.areSame(obj, thisObj);702 Y.Assert.areSame(0, argCount);703 // Test that fire() did not change the subscription state of the704 // custom event705 testEvent = events.test;706 subs = testEvent.getSubs();707 subscribers = subs[0];708 afters = subs[1];709 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);710 Y.Assert.isArray(subscribers);711 Y.Assert.areSame(1, subscribers.length);712 Y.Assert.areSame(0, afters.length);713 Y.Assert.areSame(1, testEvent.hasSubs());714 },715 "test callback context": function () {716 var target = new Y.EventTarget(),717 targetCount = 0,718 objCount = 0,719 obj = {};720 function isTarget() {721 Y.Assert.areSame(target, this);722 targetCount++;723 }724 function isObj() {725 Y.Assert.areSame(obj, this);726 objCount++;727 }728 target.on("test1", isTarget);729 target.fire("test1"); // targetCount 1730 target.on("test2", isObj, obj);731 target.fire("test2"); // objCount 1732 target.on("test3", isObj, obj, {});733 target.fire("test3"); // objCount 2734 target.on("test4", isObj, obj, null, {}, {});735 target.fire("test4"); // objCount 3736 target.on("test5", isTarget, null, {});737 target.fire("test5"); // targetCount 2738 target.on("prefix:test6", isTarget);739 target.fire("prefix:test6", obj); // targetCount 3740 target.on(["test7", "prefix:test8"], isObj, obj);741 target.fire("test7"); // objCount 4742 target.fire("prefix:test8"); // objCount 5743 target.on({ "test9": isObj }, null, obj);744 target.fire("test9"); // objCount 6745 target.on({746 "test10": { fn: isTarget },747 "test11": { fn: isObj, context: obj }748 });749 target.fire("test10"); // targetCount 4750 target.fire("test11"); // objCount 7751 target.on({752 "test12": { fn: isObj },753 "prefix:test13": { fn: isTarget, context: target }754 }, null, obj);755 target.fire("test12"); // objCount 8756 target.fire("prefix:test13"); // targetCount 5757 Y.Assert.areSame(5, targetCount);758 Y.Assert.areSame(8, objCount);759 },760 "test subscription bound args": function () {761 var target = new Y.EventTarget(),762 obj = {},763 args;764 function callback() {765 args = Y.Array(arguments, 0, true);766 }767 target.on("test1", callback, {}, "a", 1, obj, null);768 target.fire("test1");769 Y.ArrayAssert.itemsAreSame(["a", 1, obj, null], args);770 target.on(["test2", "test3"], callback, null, "a", 2.3, obj, null);771 target.fire("test2");772 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);773 args = [];774 target.fire("test3");775 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);776 // ugh, requiring two placeholders for (unused) fn and context is ooogly777 target.on({778 "test4": callback,779 "test5": callback780 }, null, null, "a", 4.5, obj, null);781 target.fire("test4");782 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);783 args = [];784 target.fire("test5");785 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);786 target.on({787 "test6": true,788 "test7": false789 }, callback, {}, "a", 6.7, obj, null);790 target.fire("test6");791 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);792 args = [];793 target.fire("test7");794 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);795 },796 "test target.on('click', fn) registers custom event only": function () {797 var target = new Y.EventTarget(),798 events = target._yuievt.events;799 target.on("click", function () {});800 Y.Assert.isInstanceOf(Y.CustomEvent, events.click);801 Y.Assert.isUndefined(events.click.domkey);802 Y.Assert.areSame(1, events.click._subscribers.length);803 }804}));805baseSuite.add(new Y.Test.Case({806 name: "target.after",807 _should: {808 ignore: {809 // As of 3.4.1, creates a subscription to a custom event named810 // "[object Object]"811 "test target.after([{ fn: fn, context: obj }]) does nothing": true,812 // Not (yet) implemented813 "test target.after(type, { handleEvents: fn })": true814 }815 },816 "test auto-publish on subscribe": function () {817 var target = new Y.EventTarget(),818 events = target._yuievt.events,819 publishCalled;820 target.publish = (function (original) {821 return function (type) {822 if (type === 'test') {823 publishCalled = true;824 }825 return original.apply(this, arguments);826 };827 })(target.publish);828 Y.Assert.isUndefined(events.test);829 target.after("test", function () {});830 Y.Assert.isTrue(publishCalled);831 Y.Assert.isObject(events.test);832 Y.Assert.isInstanceOf(Y.CustomEvent, events.test);833 },834 "test target.after(type, fn)": function () {835 var target = new Y.EventTarget(),836 events = target._yuievt.events,837 subs,838 subscribers,839 afters,840 handle, thisObj, fired, argCount, testEvent;841 function callback() {842 fired = true;843 thisObj = this;844 argCount = arguments.length;845 }846 handle = target.after("test", callback);847 testEvent = events.test;848 subs = testEvent.getSubs();849 subscribers = subs[0];850 afters = subs[1];851 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);852 Y.Assert.isArray(afters);853 Y.Assert.areSame(0, subscribers.length);854 Y.Assert.areSame(1, afters.length);855 Y.Assert.areSame(1, testEvent.hasSubs());856 Y.Assert.isInstanceOf(Y.EventHandle, handle);857 Y.Assert.areSame(testEvent, handle.evt);858 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);859 target.fire("test");860 Y.Assert.isTrue(fired);861 Y.Assert.areSame(target, thisObj);862 Y.Assert.areSame(0, argCount);863 // Test that fire() did not change the subscription state of the864 // custom event865 testEvent = events.test;866 subs = testEvent.getSubs();867 subscribers = subs[0];868 afters = subs[1];869 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);870 Y.Assert.isArray(afters);871 Y.Assert.areSame(0, subscribers.length);872 Y.Assert.areSame(1, afters.length);873 Y.Assert.areSame(1, testEvent.hasSubs());874 },875 "test target.after(type, fn) allows duplicate subs": function () {876 var target = new Y.EventTarget(),877 events = target._yuievt.events,878 count = 0,879 subs,880 subscribers,881 afters,882 testEvent, handle1, handle2;883 function callback() {884 count++;885 }886 handle1 = target.after("test", callback);887 handle2 = target.after("test", callback);888 testEvent = events.test;889 subs = testEvent.getSubs();890 subscribers = subs[0];891 afters = subs[1];892 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);893 Y.Assert.isArray(afters);894 Y.Assert.areSame(0, subscribers.length);895 Y.Assert.areSame(2, afters.length);896 Y.Assert.areSame(2, testEvent.hasSubs());897 Y.Assert.areNotSame(handle1, handle2);898 Y.Assert.areSame(testEvent, handle1.evt);899 Y.Assert.areSame(handle1.evt, handle2.evt);900 Y.Assert.areNotSame(handle1.sub, handle2.sub);901 target.fire("test");902 Y.Assert.areSame(2, count);903 // Test that fire() did not change the subscription state of the904 // custom event905 testEvent = events.test;906 subs = testEvent.getSubs();907 subscribers = subs[0];908 afters = subs[1];909 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);910 Y.Assert.isArray(afters);911 Y.Assert.areSame(0, subscribers.length);912 Y.Assert.areSame(2, afters.length);913 Y.Assert.areSame(2, testEvent.hasSubs());914 },915 "test target.after(type, fn, obj)": function () {916 var target = new Y.EventTarget(),917 obj = {},918 count = 0,919 thisObj1, thisObj2, argCount, testEvent;920 function callback() {921 count++;922 thisObj1 = this;923 argCount = arguments.length;924 }925 target.after("test", callback, obj);926 target.fire("test");927 Y.Assert.areSame(1, count);928 Y.Assert.areSame(obj, thisObj1);929 Y.Assert.areSame(0, argCount);930 target.after("test", function () {931 thisObj2 = this;932 });933 target.fire("test");934 Y.Assert.areSame(2, count);935 Y.Assert.areSame(obj, thisObj1);936 Y.Assert.areSame(target, thisObj2);937 Y.Assert.areSame(0, argCount);938 },939 "test target.after(type, fn, obj, args)": function () {940 var target = new Y.EventTarget(),941 obj = {},942 count = 0,943 args = '',944 argCount,945 thisObj1, thisObj2, testEvent;946 function callback() {947 count++;948 thisObj1 = this;949 argCount = arguments.length;950 for (var i = 0, len = argCount; i < len; ++i) {951 args += arguments[i];952 }953 }954 target.after("test", callback, obj, "A");955 target.fire("test");956 Y.Assert.areSame(1, count);957 Y.Assert.areSame(obj, thisObj1);958 Y.Assert.areSame(1, argCount);959 Y.Assert.areSame("A", args);960 target.after("test", function () {961 thisObj2 = this;962 });963 target.fire("test");964 Y.Assert.areSame(2, count);965 Y.Assert.areSame(obj, thisObj1);966 Y.Assert.areSame(target, thisObj2);967 },968 "test target.after([type], fn)": function () {969 var target = new Y.EventTarget(),970 events = target._yuievt.events,971 subs,972 subscribers,973 afters,974 handle, thisObj, fired, argCount, testEvent;975 function callback() {976 fired = true;977 thisObj = this;978 argCount = arguments.length;979 }980 handle = target.after(["test"], callback);981 testEvent = events.test;982 subs = testEvent.getSubs();983 subscribers = subs[0];984 afters = subs[1];985 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);986 Y.Assert.isArray(afters);987 Y.Assert.areSame(0, subscribers.length);988 Y.Assert.areSame(1, afters.length);989 Y.Assert.areSame(1, testEvent.hasSubs());990 Y.Assert.isInstanceOf(Y.EventHandle, handle);991 Y.Assert.isArray(handle.evt);992 Y.Assert.areSame(testEvent, handle.evt[0].evt);993 Y.Assert.isUndefined(handle.sub);994 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);995 target.fire("test");996 Y.Assert.isTrue(fired);997 Y.Assert.areSame(target, thisObj);998 Y.Assert.areSame(0, argCount);999 },1000 "test target.after([typeA, typeB], fn)": function () {1001 var target = new Y.EventTarget(),1002 events = target._yuievt.events,1003 count = 0,1004 subs,1005 subscribers,1006 afters,1007 handle, thisObj, testEvent1, testEvent2;1008 function callback() {1009 count++;1010 thisObj = this;1011 }1012 handle = target.after(["test1", "test2"], callback);1013 testEvent1 = events.test1;1014 subs = testEvent1.getSubs();1015 subscribers = subs[0];1016 afters = subs[1];1017 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent1);1018 Y.Assert.isArray(afters);1019 Y.Assert.areSame(0, subscribers.length);1020 Y.Assert.areSame(1, afters.length);1021 Y.Assert.areSame(1, testEvent1.hasSubs());1022 testEvent2 = events.test2;1023 subs = testEvent2.getSubs();1024 subscribers = subs[0];1025 afters = subs[1];1026 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent2);1027 Y.Assert.isArray(afters);1028 Y.Assert.areSame(0, subscribers.length);1029 Y.Assert.areSame(1, afters.length);1030 Y.Assert.areSame(1, testEvent2.hasSubs());1031 Y.Assert.isInstanceOf(Y.EventHandle, handle);1032 Y.Assert.isArray(handle.evt);1033 Y.Assert.areSame(testEvent1, handle.evt[0].evt);1034 Y.Assert.areSame(testEvent2, handle.evt[1].evt);1035 Y.Assert.areNotSame(testEvent1, testEvent2);1036 Y.Assert.isUndefined(handle.sub);1037 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1038 target.fire("test1");1039 Y.Assert.areSame(1, count);1040 Y.Assert.areSame(target, thisObj);1041 target.fire("test2");1042 Y.Assert.areSame(2, count);1043 Y.Assert.areSame(target, thisObj);1044 },1045 "test target.after([typeA, typeA], fn)": function () {1046 var target = new Y.EventTarget(),1047 events = target._yuievt.events,1048 count = 0,1049 subs,1050 subscribers,1051 afters,1052 handle, thisObj, testEvent;1053 function callback() {1054 count++;1055 thisObj = this;1056 }1057 handle = target.after(["test", "test"], callback);1058 testEvent = events.test;1059 subs = testEvent.getSubs();1060 subscribers = subs[0];1061 afters = subs[1];1062 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1063 Y.Assert.isArray(afters);1064 Y.Assert.areSame(0, subscribers.length);1065 Y.Assert.areSame(2, afters.length);1066 Y.Assert.areSame(2, testEvent.hasSubs());1067 Y.Assert.isInstanceOf(Y.EventHandle, handle);1068 Y.Assert.isArray(handle.evt);1069 Y.Assert.areSame(testEvent, handle.evt[0].evt);1070 Y.Assert.areSame(testEvent, handle.evt[1].evt);1071 Y.Assert.isUndefined(handle.sub);1072 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1073 target.fire("test");1074 Y.Assert.areSame(2, count);1075 Y.Assert.areSame(target, thisObj);1076 },1077 "test target.after([], fn) does nothing": function () {1078 var target = new Y.EventTarget(),1079 events = target._yuievt.events,1080 count = 0,1081 handle, name, subs, i;1082 function callback() {1083 Y.Assert.fail("I don't know how this got called");1084 }1085 handle = target.after([], callback);1086 for (name in events) {1087 if (events.hasOwnProperty(name)) {1088 subs = events[name]._afters;1089 for (i = subs.length - 1; i >= 0; --i) {1090 if (subs[i].fn === callback) {1091 Y.Assert.fail("subscription registered for '" + name + "' event");1092 }1093 }1094 }1095 }1096 Y.Assert.isInstanceOf(Y.EventHandle, handle);1097 Y.Assert.isArray(handle.evt);1098 Y.Assert.areSame(0, handle.evt.length);1099 Y.Assert.isUndefined(handle.sub);1100 },1101 "test target.after([{ fn: fn, context: obj }]) does nothing": function () {1102 var target = new Y.EventTarget(),1103 events = target._yuievt.events,1104 count = 0,1105 handle, name, subs, i;1106 function callback() {1107 Y.Assert.fail("I don't know how this got called");1108 }1109 handle = target.after([{ fn: callback, context: {} }]);1110 for (name in events) {1111 if (events.hasOwnProperty(name)) {1112 subs = events[name]._afters;1113 for (i = subs.length - 1; i >= 0; --i) {1114 if (subs[i].fn === callback) {1115 Y.Assert.fail("subscription registered for '" + name + "' event");1116 }1117 }1118 }1119 }1120 Y.Assert.isInstanceOf(Y.EventHandle, handle);1121 Y.Assert.isArray(handle.evt);1122 Y.Assert.areSame(0, handle.evt.length);1123 Y.Assert.isUndefined(handle.sub);1124 },1125 "test target.after({ type: fn })": function () {1126 var target = new Y.EventTarget(),1127 events = target._yuievt.events,1128 subs,1129 subscribers,1130 afters,1131 handle, thisObj, fired, argCount, testEvent;1132 function callback() {1133 fired = true;1134 thisObj = this;1135 argCount = arguments.length;1136 }1137 handle = target.after({ "test1": callback });1138 testEvent = events.test1;1139 subs = testEvent.getSubs();1140 subscribers = subs[0];1141 afters = subs[1];1142 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1143 Y.Assert.isArray(afters);1144 Y.Assert.areSame(0, subscribers.length);1145 Y.Assert.areSame(1, afters.length);1146 Y.Assert.areSame(1, testEvent.hasSubs());1147 Y.Assert.isInstanceOf(Y.EventHandle, handle);1148 Y.Assert.isArray(handle.evt);1149 Y.Assert.areSame(1, handle.evt.length);1150 Y.Assert.areSame(testEvent, handle.evt[0].evt);1151 Y.Assert.isUndefined(handle.sub);1152 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1153 target.fire("test1");1154 Y.Assert.isTrue(fired);1155 Y.Assert.areSame(target, thisObj);1156 Y.Assert.areSame(0, argCount);1157 handle = target.after({1158 "test2": callback,1159 "test3": callback1160 });1161 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);1162 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);1163 Y.Assert.areSame(1, events.test2._afters.length);1164 Y.Assert.areSame(1, events.test3._afters.length);1165 Y.Assert.isInstanceOf(Y.EventHandle, handle);1166 Y.Assert.isArray(handle.evt);1167 Y.Assert.areSame(2, handle.evt.length);1168 Y.Assert.areSame(events.test2, handle.evt[0].evt);1169 Y.Assert.areSame(events.test3, handle.evt[1].evt);1170 Y.Assert.isUndefined(handle.sub);1171 },1172 "test target.after({ type: true }, fn)": function () {1173 var target = new Y.EventTarget(),1174 events = target._yuievt.events,1175 subs,1176 subscribers,1177 afters,1178 handle, thisObj, fired, argCount, testEvent;1179 function callback() {1180 fired = true;1181 thisObj = this;1182 argCount = arguments.length;1183 }1184 handle = target.after({ "test1": true }, callback);1185 testEvent = events.test1;1186 subs = testEvent.getSubs();1187 subscribers = subs[0];1188 afters = subs[1];1189 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1190 Y.Assert.isArray(afters);1191 Y.Assert.areSame(0, subscribers.length);1192 Y.Assert.areSame(1, afters.length);1193 Y.Assert.areSame(1, testEvent.hasSubs());1194 Y.Assert.isInstanceOf(Y.EventHandle, handle);1195 Y.Assert.isArray(handle.evt);1196 Y.Assert.areSame(1, handle.evt.length);1197 Y.Assert.areSame(testEvent, handle.evt[0].evt);1198 Y.Assert.isUndefined(handle.sub);1199 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1200 target.fire("test1");1201 Y.Assert.isTrue(fired);1202 Y.Assert.areSame(target, thisObj);1203 Y.Assert.areSame(0, argCount);1204 handle = target.after({ "test2": 1, "test3": false }, callback);1205 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);1206 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);1207 Y.Assert.areSame(1, events.test2._afters.length);1208 Y.Assert.areSame(1, events.test3._afters.length);1209 Y.Assert.isInstanceOf(Y.EventHandle, handle);1210 Y.Assert.isArray(handle.evt);1211 Y.Assert.areSame(2, handle.evt.length);1212 Y.Assert.areSame(events.test2, handle.evt[0].evt);1213 Y.Assert.areSame(events.test3, handle.evt[1].evt);1214 Y.Assert.isUndefined(handle.sub);1215 },1216 "test target.after(type, { handleEvents: fn })": function () {1217 var target = new Y.EventTarget(),1218 events = target._yuievt.events,1219 subs,1220 subscribers,1221 afters,1222 obj, handle, thisObj, fired, argCount, testEvent;1223 function callback() {1224 fired = true;1225 thisObj = this;1226 argCount = arguments.length;1227 }1228 obj = { handleEvents: callback };1229 handle = target.after("test", obj);1230 testEvent = events.test;1231 subs = testEvent.getSubs();1232 subscribers = subs[0];1233 afters = subs[1];1234 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1235 Y.Assert.isArray(afters);1236 Y.Assert.areSame(0, subscribers.length);1237 Y.Assert.areSame(1, afters.length);1238 Y.Assert.areSame(1, testEvent.hasSubs());1239 Y.Assert.isInstanceOf(Y.EventHandle, handle);1240 Y.Assert.areSame(testEvent, handle.evt);1241 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);1242 // Barring support, this is where the error will be thrown.1243 // ET.after() doesn't verify the second arg is a function, and1244 // Subscriber doesn't type check before treating it as a function.1245 target.fire("test");1246 Y.Assert.isTrue(fired);1247 Y.Assert.areSame(obj, thisObj);1248 Y.Assert.areSame(0, argCount);1249 // Test that fire() did not change the subscription state of the custom event1250 testEvent = events.test;1251 subs = testEvent.getSubs();1252 subscribers = subs[0];1253 afters = subs[1];1254 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1255 Y.Assert.isArray(afters);1256 Y.Assert.areSame(0, subscribers.length);1257 Y.Assert.areSame(1, afters.length);1258 Y.Assert.areSame(1, testEvent.hasSubs());1259 },1260 "test callback context": function () {1261 var target = new Y.EventTarget(),1262 targetCount = 0,1263 objCount = 0,1264 obj = {};1265 function isTarget() {1266 Y.Assert.areSame(target, this);1267 targetCount++;1268 }1269 function isObj() {1270 Y.Assert.areSame(obj, this);1271 objCount++;1272 }1273 target.after("test1", isTarget);1274 target.fire("test1");1275 target.after("test2", isObj, obj);1276 target.fire("test2");1277 target.after("test3", isObj, obj, {});1278 target.fire("test3");1279 target.after("test4", isObj, obj, null, {}, {});1280 target.fire("test4");1281 target.after("test5", isTarget, null, {});1282 target.fire("test5");1283 target.after("prefix:test6", isTarget);1284 target.fire("prefix:test6", obj);1285 target.after(["test7", "prefix:test8"], isObj, obj);1286 target.fire("test7");1287 target.fire("prefix:test8");1288 target.after({ "test9": isObj }, null, obj);1289 target.fire("test9");1290 target.after({1291 "test10": { fn: isTarget },1292 "test11": { fn: isObj, context: obj }1293 });1294 target.fire("test10");1295 target.fire("test11");1296 target.after({1297 "test12": { fn: isObj },1298 "prefix:test13": { fn: isTarget, context: target }1299 }, null, obj);1300 target.fire("test12");1301 target.fire("prefix:test13");1302 Y.Assert.areSame(5, targetCount);1303 Y.Assert.areSame(8, objCount);1304 },1305 "test subscription bound args": function () {1306 var target = new Y.EventTarget(),1307 obj = {},1308 args;1309 function callback() {1310 args = Y.Array(arguments, 0, true);1311 }1312 target.after("test1", callback, {}, "a", 1, obj, null);1313 target.fire("test1");1314 Y.ArrayAssert.itemsAreSame(["a", 1, obj, null], args);1315 target.after(["test2", "test3"], callback, null, "a", 2.3, obj, null);1316 target.fire("test2");1317 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);1318 args = [];1319 target.fire("test3");1320 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);1321 // ugh, requiring two placeholders for (unused) fn and context is ooogly1322 target.after({1323 "test4": callback,1324 "test5": callback1325 }, null, null, "a", 4.5, obj, null);1326 target.fire("test4");1327 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);1328 args = [];1329 target.fire("test5");1330 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);1331 target.after({1332 "test6": true,1333 "test7": false1334 }, callback, {}, "a", 6.7, obj, null);1335 target.fire("test6");1336 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);1337 args = [];1338 target.fire("test7");1339 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);1340 },1341 "test target.after('click', fn) registers custom event only": function () {1342 var target = new Y.EventTarget(),1343 events = target._yuievt.events;1344 target.after("click", function () {});1345 Y.Assert.isInstanceOf(Y.CustomEvent, events.click);1346 Y.Assert.isUndefined(events.click.domkey);1347 Y.Assert.areSame(1, events.click._afters.length);1348 }1349}));1350baseSuite.add(new Y.Test.Case({1351 name: "target.once",1352 _should: {1353 ignore: {1354 // As of 3.4.1, creates a subscription to a custom event named1355 // "[object Object]"1356 "test target.once([{ fn: fn, context: obj }]) does nothing": true,1357 // Not (yet) implemented1358 "test target.once(type, { handleEvents: fn })": true1359 }1360 },1361 "test auto-publish on subscribe": function () {1362 var target = new Y.EventTarget(),1363 events = target._yuievt.events,1364 publishCalled;1365 target.publish = (function (original) {1366 return function (type) {1367 if (type === 'test') {1368 publishCalled = true;1369 }1370 return original.apply(this, arguments);1371 };1372 })(target.publish);1373 Y.Assert.isUndefined(events.test);1374 target.once("test", function () {});1375 Y.Assert.isTrue(publishCalled);1376 Y.Assert.isObject(events.test);1377 Y.Assert.isInstanceOf(Y.CustomEvent, events.test);1378 },1379 "test target.once(type, fn)": function () {1380 var target = new Y.EventTarget(),1381 events = target._yuievt.events,1382 subs,1383 subscribers,1384 afters,1385 handle, thisObj, fired, argCount, testEvent;1386 function callback() {1387 fired = true;1388 thisObj = this;1389 argCount = arguments.length;1390 }1391 handle = target.once("test", callback);1392 testEvent = events.test;1393 subs = testEvent.getSubs();1394 subscribers = subs[0];1395 afters = subs[1];1396 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1397 Y.Assert.isArray(subscribers);1398 Y.Assert.areSame(1, subscribers.length);1399 Y.Assert.areSame(0, afters.length);1400 Y.Assert.areSame(1, testEvent.hasSubs());1401 Y.Assert.isInstanceOf(Y.EventHandle, handle);1402 Y.Assert.areSame(testEvent, handle.evt);1403 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);1404 target.fire("test");1405 Y.Assert.isTrue(fired);1406 Y.Assert.areSame(target, thisObj);1407 Y.Assert.areSame(0, argCount);1408 // Test that fire() resulted in immediate detach of once() sub1409 testEvent = events.test;1410 subs = testEvent.getSubs();1411 subscribers = subs[0];1412 afters = subs[1];1413 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1414 Y.Assert.isArray(subscribers);1415 Y.Assert.areSame(0, subscribers.length);1416 Y.Assert.areSame(0, afters.length);1417 Y.Assert.areSame(0, testEvent.hasSubs());1418 },1419 "test target.once(type, fn) allows duplicate subs": function () {1420 var target = new Y.EventTarget(),1421 events = target._yuievt.events,1422 count = 0,1423 subs,1424 subscribers,1425 afters,1426 testEvent, handle1, handle2;1427 function callback() {1428 count++;1429 }1430 handle1 = target.once("test", callback);1431 handle2 = target.once("test", callback);1432 testEvent = events.test;1433 subs = testEvent.getSubs();1434 subscribers = subs[0];1435 afters = subs[1];1436 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1437 Y.Assert.isArray(subscribers);1438 Y.Assert.areSame(2, subscribers.length);1439 Y.Assert.areSame(0, afters.length);1440 Y.Assert.areSame(2, testEvent.hasSubs());1441 Y.Assert.areNotSame(handle1, handle2);1442 Y.Assert.areSame(testEvent, handle1.evt);1443 Y.Assert.areSame(handle1.evt, handle2.evt);1444 Y.Assert.areNotSame(handle1.sub, handle2.sub);1445 target.fire("test");1446 Y.Assert.areSame(2, count);1447 // Test that fire() resulted in immediate detach of once() sub1448 testEvent = events.test;1449 subs = testEvent.getSubs();1450 subscribers = subs[0];1451 afters = subs[1];1452 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1453 Y.Assert.isArray(subscribers);1454 Y.Assert.areSame(0, subscribers.length);1455 Y.Assert.areSame(0, afters.length);1456 Y.Assert.areSame(0, testEvent.hasSubs());1457 target.fire("test");1458 Y.Assert.areSame(2, count);1459 },1460 "test target.once(type, fn, obj)": function () {1461 var target = new Y.EventTarget(),1462 obj = {},1463 count = 0,1464 thisObj1, thisObj2, argCount, testEvent;1465 function callback() {1466 count++;1467 thisObj1 = this;1468 argCount = arguments.length;1469 }1470 target.once("test", callback, obj);1471 target.fire("test");1472 Y.Assert.areSame(1, count);1473 Y.Assert.areSame(obj, thisObj1);1474 Y.Assert.areSame(0, argCount);1475 // Subscriber should be detached, so count should not increment1476 target.fire("test");1477 Y.Assert.areSame(1, count);1478 target.once("test", function () {1479 thisObj2 = this;1480 });1481 target.fire("test");1482 Y.Assert.areSame(1, count);1483 Y.Assert.areSame(obj, thisObj1);1484 Y.Assert.areSame(target, thisObj2);1485 // Subscriber should be detached, so count should not increment1486 target.fire("test");1487 Y.Assert.areSame(1, count);1488 },1489 "test target.once(type, fn, obj, args)": function () {1490 var target = new Y.EventTarget(),1491 obj = {},1492 count = 0,1493 args = '',1494 argCount,1495 thisObj1, thisObj2, testEvent;1496 function callback() {1497 count++;1498 thisObj1 = this;1499 argCount = arguments.length;1500 for (var i = 0, len = argCount; i < len; ++i) {1501 args += arguments[i];1502 }1503 }1504 target.once("test", callback, obj, "A");1505 target.fire("test");1506 Y.Assert.areSame(1, count);1507 Y.Assert.areSame(obj, thisObj1);1508 Y.Assert.areSame("A", args);1509 // Subscriber should be detached, so count should not increment1510 target.fire("test");1511 Y.Assert.areSame(1, count);1512 target.once("test", function () {1513 thisObj2 = this;1514 });1515 target.fire("test");1516 Y.Assert.areSame(1, count);1517 Y.Assert.areSame(obj, thisObj1);1518 Y.Assert.areSame(target, thisObj2);1519 // Subscriber should be detached, so count should not increment1520 target.fire("test");1521 Y.Assert.areSame(1, count);1522 },1523 "test target.once([type], fn)": function () {1524 var target = new Y.EventTarget(),1525 events = target._yuievt.events,1526 subs,1527 subscribers,1528 afters,1529 handle, thisObj, fired, argCount, testEvent;1530 function callback() {1531 fired = true;1532 thisObj = this;1533 argCount = arguments.length;1534 }1535 handle = target.once(["test"], callback);1536 testEvent = events.test;1537 subs = testEvent.getSubs();1538 subscribers = subs[0];1539 afters = subs[1];1540 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1541 Y.Assert.isArray(subscribers);1542 Y.Assert.areSame(1, subscribers.length);1543 Y.Assert.areSame(0, afters.length);1544 Y.Assert.areSame(1, testEvent.hasSubs());1545 Y.Assert.isInstanceOf(Y.EventHandle, handle);1546 Y.Assert.isArray(handle.evt);1547 Y.Assert.areSame(testEvent, handle.evt[0].evt);1548 Y.Assert.isUndefined(handle.sub);1549 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1550 target.fire("test");1551 Y.Assert.isTrue(fired);1552 Y.Assert.areSame(target, thisObj);1553 Y.Assert.areSame(0, argCount);1554 Y.Assert.areSame(0, testEvent._subscribers && testEvent._subscribers.length);1555 fired = false;1556 target.fire("test");1557 Y.Assert.isFalse(fired);1558 },1559 "test target.once([typeA, typeB], fn)": function () {1560 var target = new Y.EventTarget(),1561 events = target._yuievt.events,1562 count = 0,1563 subs,1564 subscribers,1565 afters,1566 handle, thisObj, testEvent1, testEvent2;1567 function callback() {1568 count++;1569 thisObj = this;1570 }1571 handle = target.once(["test1", "test2"], callback);1572 testEvent1 = events.test1;1573 subs = testEvent1.getSubs();1574 subscribers = subs[0];1575 afters = subs[1];1576 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent1);1577 Y.Assert.isArray(subscribers);1578 Y.Assert.areSame(1, subscribers.length);1579 Y.Assert.areSame(0, afters.length);1580 Y.Assert.areSame(1, testEvent1.hasSubs());1581 testEvent2 = events.test2;1582 subs = testEvent2.getSubs();1583 subscribers = subs[0];1584 afters = subs[1];1585 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent2);1586 Y.Assert.isArray(subscribers);1587 Y.Assert.areSame(1, subscribers.length);1588 Y.Assert.areSame(0, afters.length);1589 Y.Assert.areSame(1, testEvent2.hasSubs());1590 Y.Assert.isInstanceOf(Y.EventHandle, handle);1591 Y.Assert.isArray(handle.evt);1592 Y.Assert.areSame(testEvent1, handle.evt[0].evt);1593 Y.Assert.areSame(testEvent2, handle.evt[1].evt);1594 Y.Assert.areNotSame(testEvent1, testEvent2);1595 Y.Assert.isUndefined(handle.sub);1596 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1597 target.fire("test1");1598 Y.Assert.areSame(1, count);1599 Y.Assert.areSame(target, thisObj);1600 Y.Assert.areSame(0, testEvent1._subscribers && testEvent1._subscribers.length);1601 target.fire("test2");1602 Y.Assert.areSame(2, count);1603 Y.Assert.areSame(target, thisObj);1604 Y.Assert.areSame(0, testEvent2._subscribers && testEvent2._subscribers.length);1605 },1606 "test target.once([typeA, typeA], fn)": function () {1607 var target = new Y.EventTarget(),1608 events = target._yuievt.events,1609 subs,1610 subscribers,1611 afters,1612 count = 0,1613 handle, thisObj, testEvent;1614 function callback() {1615 count++;1616 thisObj = this;1617 }1618 handle = target.once(["test", "test"], callback);1619 testEvent = events.test;1620 subs = testEvent.getSubs();1621 subscribers = subs[0];1622 afters = subs[1];1623 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1624 Y.Assert.isArray(subscribers);1625 Y.Assert.areSame(2, subscribers.length);1626 Y.Assert.areSame(0, afters.length);1627 Y.Assert.areSame(2, testEvent.hasSubs());1628 Y.Assert.isInstanceOf(Y.EventHandle, handle);1629 Y.Assert.isArray(handle.evt);1630 Y.Assert.areSame(testEvent, handle.evt[0].evt);1631 Y.Assert.areSame(testEvent, handle.evt[1].evt);1632 Y.Assert.isUndefined(handle.sub);1633 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1634 target.fire("test");1635 Y.Assert.areSame(2, count);1636 Y.Assert.areSame(target, thisObj);1637 Y.Assert.areSame(0, testEvent._subscribers && testEvent._subscribers.length);1638 },1639 "test target.once([], fn) does nothing": function () {1640 var target = new Y.EventTarget(),1641 events = target._yuievt.events,1642 count = 0,1643 handle, name, subs, i;1644 function callback() {1645 Y.Assert.fail("I don't know how this got called");1646 }1647 handle = target.once([], callback);1648 for (name in events) {1649 if (events.hasOwnProperty(name)) {1650 subs = events[name]._subscribers;1651 if (subs) {1652 for (i = subs.length - 1; i >= 0; --i) {1653 if (subs[i].fn === callback) {1654 Y.Assert.fail("subscription registered for '" + name + "' event");1655 }1656 }1657 }1658 }1659 }1660 Y.Assert.isInstanceOf(Y.EventHandle, handle);1661 Y.Assert.isArray(handle.evt);1662 Y.Assert.areSame(0, handle.evt.length);1663 Y.Assert.isUndefined(handle.sub);1664 },1665 "test target.once([{ fn: fn, context: obj }]) does nothing": function () {1666 var target = new Y.EventTarget(),1667 events = target._yuievt.events,1668 count = 0,1669 handle, name, subs, i;1670 function callback() {1671 Y.Assert.fail("I don't know how this got called");1672 }1673 handle = target.once([{ fn: callback, context: {} }]);1674 for (name in events) {1675 if (events.hasOwnProperty(name)) {1676 subs = events[name]._subscribers;1677 if (subs) {1678 for (i = subs.length - 1; i >= 0; --i) {1679 if (subs[i].fn === callback) {1680 Y.Assert.fail("subscription registered for '" + name + "' event");1681 }1682 }1683 }1684 }1685 }1686 Y.Assert.isInstanceOf(Y.EventHandle, handle);1687 Y.Assert.isArray(handle.evt);1688 Y.Assert.areSame(0, handle.evt.length);1689 Y.Assert.isUndefined(handle.sub);1690 },1691 "test target.once({ type: fn })": function () {1692 var target = new Y.EventTarget(),1693 events = target._yuievt.events,1694 subs,1695 subscribers,1696 afters,1697 handle, thisObj, fired, argCount, testEvent;1698 function callback() {1699 fired = true;1700 thisObj = this;1701 argCount = arguments.length;1702 }1703 handle = target.once({ "test1": callback });1704 testEvent = events.test1;1705 subs = testEvent.getSubs();1706 subscribers = subs[0];1707 afters = subs[1];1708 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1709 Y.Assert.isArray(subscribers);1710 Y.Assert.areSame(1, subscribers.length);1711 Y.Assert.areSame(0, afters.length);1712 Y.Assert.areSame(1, testEvent.hasSubs());1713 Y.Assert.isInstanceOf(Y.EventHandle, handle);1714 Y.Assert.isArray(handle.evt);1715 Y.Assert.areSame(1, handle.evt.length);1716 Y.Assert.areSame(testEvent, handle.evt[0].evt);1717 Y.Assert.isUndefined(handle.sub);1718 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1719 target.fire("test1");1720 Y.Assert.isTrue(fired);1721 Y.Assert.areSame(target, thisObj);1722 Y.Assert.areSame(0, argCount);1723 Y.Assert.areSame(0, testEvent._subscribers && testEvent._subscribers.length);1724 handle = target.once({1725 "test2": callback,1726 "test3": callback1727 });1728 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);1729 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);1730 Y.Assert.areSame(1, events.test2._subscribers.length);1731 Y.Assert.areSame(1, events.test3._subscribers.length);1732 Y.Assert.isInstanceOf(Y.EventHandle, handle);1733 Y.Assert.isArray(handle.evt);1734 Y.Assert.areSame(2, handle.evt.length);1735 Y.Assert.areSame(events.test2, handle.evt[0].evt);1736 Y.Assert.areSame(events.test3, handle.evt[1].evt);1737 Y.Assert.isUndefined(handle.sub);1738 target.fire("test2");1739 Y.Assert.areSame(0, events.test2._subscribers && events.test2._subscribers.length);1740 Y.Assert.areSame(1, events.test3._subscribers.length);1741 target.fire("test3");1742 Y.Assert.areSame(0, events.test2._subscribers && events.test2._subscribers.length);1743 Y.Assert.areSame(0, events.test3._subscribers && events.test3._subscribers.length);1744 },1745 "test target.once({ type: true }, fn)": function () {1746 var target = new Y.EventTarget(),1747 events = target._yuievt.events,1748 subs,1749 subscribers,1750 afters,1751 handle, thisObj, fired, argCount, testEvent;1752 function callback() {1753 fired = true;1754 thisObj = this;1755 argCount = arguments.length;1756 }1757 handle = target.once({ "test1": true }, callback);1758 testEvent = events.test1;1759 subs = testEvent.getSubs();1760 subscribers = subs[0];1761 afters = subs[1];1762 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1763 Y.Assert.isArray(subscribers);1764 Y.Assert.areSame(1, subscribers.length);1765 Y.Assert.areSame(0, afters.length);1766 Y.Assert.areSame(1, testEvent.hasSubs());1767 Y.Assert.isInstanceOf(Y.EventHandle, handle);1768 Y.Assert.isArray(handle.evt);1769 Y.Assert.areSame(1, handle.evt.length);1770 Y.Assert.areSame(testEvent, handle.evt[0].evt);1771 Y.Assert.isUndefined(handle.sub);1772 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1773 target.fire("test1");1774 Y.Assert.isTrue(fired);1775 Y.Assert.areSame(target, thisObj);1776 Y.Assert.areSame(0, argCount);1777 Y.Assert.areSame(0, testEvent._subscribers && testEvent._subscribers.length);1778 handle = target.once({ "test2": 1, "test3": false }, callback);1779 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);1780 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);1781 Y.Assert.areSame(1, events.test2._subscribers.length);1782 Y.Assert.areSame(1, events.test3._subscribers.length);1783 Y.Assert.isInstanceOf(Y.EventHandle, handle);1784 Y.Assert.isArray(handle.evt);1785 Y.Assert.areSame(2, handle.evt.length);1786 Y.Assert.areSame(events.test2, handle.evt[0].evt);1787 Y.Assert.areSame(events.test3, handle.evt[1].evt);1788 Y.Assert.isUndefined(handle.sub);1789 target.fire("test2");1790 Y.Assert.areSame(0, events.test2._subscribers && events.test2._subscribers.length);1791 Y.Assert.areSame(1, events.test3._subscribers && events.test3._subscribers.length);1792 target.fire("test3");1793 Y.Assert.areSame(0, events.test2._subscribers && events.test2._subscribers.length);1794 Y.Assert.areSame(0, events.test3._subscribers && events.test3._subscribers.length);1795 },1796 "test target.once(type, { handleEvents: fn })": function () {1797 var target = new Y.EventTarget(),1798 events = target._yuievt.events,1799 subs,1800 subscribers,1801 afters,1802 obj, handle, thisObj, fired, argCount, testEvent;1803 function callback() {1804 fired = true;1805 thisObj = this;1806 argCount = arguments.length;1807 }1808 obj = { handleEvents: callback };1809 handle = target.once("test", obj);1810 testEvent = events.test;1811 subs = testEvent.getSubs();1812 subscribers = subs[0];1813 afters = subs[1];1814 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1815 Y.Assert.isArray(subscribers);1816 Y.Assert.areSame(1, subscribers.length);1817 Y.Assert.areSame(0, afters.length);1818 Y.Assert.areSame(1, testEvent.hasSubs());1819 Y.Assert.isInstanceOf(Y.EventHandle, handle);1820 Y.Assert.areSame(testEvent, handle.evt);1821 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);1822 // Barring support, this is where the error will be thrown.1823 // ET.once() doesn't verify the second arg is a function, and1824 // Subscriber doesn't type check before treating it as a function.1825 target.fire("test");1826 Y.Assert.isTrue(fired);1827 Y.Assert.areSame(obj, thisObj);1828 Y.Assert.areSame(0, argCount);1829 // Fire should immediate detach the subscription1830 testEvent = events.test;1831 subs = testEvent.getSubs();1832 subscribers = subs[0];1833 afters = subs[1];1834 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1835 Y.Assert.isArray(subscribers);1836 Y.Assert.areSame(0, subscribers.length);1837 Y.Assert.areSame(0, afters.length);1838 Y.Assert.areSame(1, testEvent.hasSubs());1839 },1840 "test callback context": function () {1841 var target = new Y.EventTarget(),1842 events = target._yuievt.events,1843 targetCount = 0,1844 objCount = 0,1845 obj = {};1846 function isTarget() {1847 Y.Assert.areSame(target, this);1848 targetCount++;1849 }1850 function isObj() {1851 Y.Assert.areSame(obj, this);1852 objCount++;1853 }1854 target.once("test1", isTarget);1855 target.fire("test1");1856 Y.Assert.areSame(1, targetCount);1857 Y.Assert.areSame(0, objCount);1858 target.once("test2", isObj, obj);1859 target.fire("test2");1860 Y.Assert.areSame(1, targetCount);1861 Y.Assert.areSame(1, objCount);1862 target.once("test3", isObj, obj, {});1863 target.fire("test3");1864 Y.Assert.areSame(1, targetCount);1865 Y.Assert.areSame(2, objCount);1866 target.once("test4", isObj, obj, null, {}, {});1867 target.fire("test4");1868 Y.Assert.areSame(1, targetCount);1869 Y.Assert.areSame(3, objCount);1870 target.once("test5", isTarget, null, {});1871 target.fire("test5");1872 Y.Assert.areSame(2, targetCount);1873 Y.Assert.areSame(3, objCount);1874 target.once("prefix:test6", isTarget);1875 target.fire("prefix:test6", obj);1876 Y.Assert.areSame(3, targetCount);1877 Y.Assert.areSame(3, objCount);1878 target.once(["test7", "prefix:test8"], isObj, obj);1879 target.fire("test7");1880 target.fire("prefix:test8");1881 Y.Assert.areSame(3, targetCount);1882 Y.Assert.areSame(5, objCount);1883 target.once({ "test9": isObj }, null, obj);1884 target.fire("test9");1885 Y.Assert.areSame(3, targetCount);1886 Y.Assert.areSame(6, objCount);1887 target.once({1888 "test10": { fn: isTarget },1889 "test11": { fn: isObj, context: obj }1890 });1891 target.fire("test10");1892 target.fire("test11");1893 Y.Assert.areSame(4, targetCount);1894 Y.Assert.areSame(7, objCount);1895 target.once({1896 "test12": { fn: isObj },1897 "prefix:test13": { fn: isTarget, context: target }1898 }, null, obj);1899 target.fire("test12");1900 target.fire("prefix:test13");1901 Y.Assert.areSame(5, targetCount);1902 Y.Assert.areSame(8, objCount);1903 Y.Assert.areSame(0, events.test1._subscribers && events.test1._subscribers.length);1904 Y.Assert.areSame(0, events.test2._subscribers && events.test2._subscribers.length);1905 Y.Assert.areSame(0, events.test3._subscribers && events.test3._subscribers.length);1906 Y.Assert.areSame(0, events.test4._subscribers && events.test4._subscribers.length);1907 Y.Assert.areSame(0, events.test5._subscribers && events.test5._subscribers.length);1908 Y.Assert.areSame(0, events['prefix:test6']._subscribers && events['prefix:test6']._subscribers.length);1909 Y.Assert.areSame(0, events.test7._subscribers && events.test7._subscribers.length);1910 Y.Assert.areSame(0, events['prefix:test8']._subscribers && events['prefix:test8']._subscribers.length);1911 Y.Assert.areSame(0, events.test9._subscribers && events.test9._subscribers.length);1912 Y.Assert.areSame(0, events.test10._subscribers && events.test10._subscribers.length);1913 Y.Assert.areSame(0, events.test11._subscribers && events.test11._subscribers.length);1914 Y.Assert.areSame(0, events.test12._subscribers && events.test12._subscribers.length);1915 Y.Assert.areSame(0, events['prefix:test13']._subscribers && events['prefix:test13']._subscribers.length);1916 },1917 "test subscription bound args": function () {1918 var target = new Y.EventTarget(),1919 events = target._yuievt.events,1920 obj = {},1921 args;1922 function callback() {1923 args = Y.Array(arguments, 0, true);1924 }1925 target.once("test1", callback, {}, "a", 1, obj, null);1926 target.fire("test1");1927 Y.ArrayAssert.itemsAreSame(["a", 1, obj, null], args);1928 target.once(["test2", "test3"], callback, null, "a", 2.3, obj, null);1929 target.fire("test2");1930 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);1931 args = [];1932 target.fire("test3");1933 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);1934 // ugh, requiring two placeholders for (unused) fn and context is ooogly1935 target.once({1936 "test4": callback,1937 "test5": callback1938 }, null, null, "a", 4.5, obj, null);1939 target.fire("test4");1940 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);1941 args = [];1942 target.fire("test5");1943 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);1944 target.once({1945 "test6": true,1946 "test7": false1947 }, callback, {}, "a", 6.7, obj, null);1948 target.fire("test6");1949 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);1950 args = [];1951 target.fire("test7");1952 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);1953 Y.Assert.areSame(0, events.test1._subscribers && events.test1._subscribers.length);1954 Y.Assert.areSame(0, events.test2._subscribers && events.test2._subscribers.length);1955 Y.Assert.areSame(0, events.test3._subscribers && events.test3._subscribers.length);1956 Y.Assert.areSame(0, events.test4._subscribers && events.test4._subscribers.length);1957 Y.Assert.areSame(0, events.test5._subscribers && events.test5._subscribers.length);1958 Y.Assert.areSame(0, events.test6._subscribers && events.test6._subscribers.length);1959 Y.Assert.areSame(0, events.test7._subscribers && events.test7._subscribers.length);1960 },1961 "test target.once('click', fn) registers custom event only": function () {1962 var target = new Y.EventTarget(),1963 events = target._yuievt.events,1964 fired = false;1965 target.once("click", function () {1966 fired = true;1967 // Not an emitFacade event, so there's no e to verify type1968 });1969 Y.Assert.isInstanceOf(Y.CustomEvent, events.click);1970 Y.Assert.isUndefined(events.click.domkey);1971 Y.Assert.areSame(1, events.click._subscribers.length);1972 target.fire("click");1973 Y.Assert.isTrue(fired);1974 Y.Assert.areSame(0, events.click._subscribers && events.click._subscribers.length);1975 }1976}));1977baseSuite.add(new Y.Test.Case({1978 name: "target.onceAfter",1979 _should: {1980 ignore: {1981 // As of 3.4.1, creates a subscription to a custom event named1982 // "[object Object]"1983 "test target.onceAfter([{ fn: fn, context: obj }]) does nothing": true,1984 // Not (yet) implemented1985 "test target.onceAfter(type, { handleEvents: fn })": true1986 }1987 },1988 test_onceAfter: function () {1989 var a = new Y.EventTarget({ emitFacade: true, prefix: 'a' }),1990 result = '';1991 a.on('foo', function () { result += 'A'; });1992 a.once('foo', function () { result += 'B'; });1993 a.after('foo', function () { result += 'C'; });1994 a.onceAfter('foo', function () { result += 'D'; });1995 a.fire('foo');1996 a.fire('foo');1997 Y.Assert.areSame("ABCDAC", result);1998 },1999 "test auto-publish on subscribe": function () {2000 var target = new Y.EventTarget(),2001 events = target._yuievt.events,2002 publishCalled;2003 target.publish = (function (original) {2004 return function (type) {2005 if (type === 'test') {2006 publishCalled = true;2007 }2008 return original.apply(this, arguments);2009 };2010 })(target.publish);2011 Y.Assert.isUndefined(events.test);2012 target.onceAfter("test", function () {});2013 Y.Assert.isTrue(publishCalled);2014 Y.Assert.isObject(events.test);2015 Y.Assert.isInstanceOf(Y.CustomEvent, events.test);2016 },2017 "test target.onceAfter(type, fn)": function () {2018 var target = new Y.EventTarget(),2019 events = target._yuievt.events,2020 subs,2021 subscribers,2022 afters,2023 handle, thisObj, fired, argCount, testEvent;2024 function callback() {2025 fired = true;2026 thisObj = this;2027 argCount = arguments.length;2028 }2029 handle = target.onceAfter("test", callback);2030 testEvent = events.test;2031 subs = testEvent.getSubs();2032 subscribers = subs[0];2033 afters = subs[1];2034 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2035 Y.Assert.isArray(afters);2036 Y.Assert.areSame(0, subscribers.length);2037 Y.Assert.areSame(1, afters.length);2038 Y.Assert.areSame(1, testEvent.hasSubs());2039 Y.Assert.isInstanceOf(Y.EventHandle, handle);2040 Y.Assert.areSame(testEvent, handle.evt);2041 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);2042 target.fire("test");2043 Y.Assert.isTrue(fired);2044 Y.Assert.areSame(target, thisObj);2045 Y.Assert.areSame(0, argCount);2046 // Test that fire() resulted in immediate detach of onceAfter() sub2047 testEvent = events.test;2048 subs = testEvent.getSubs();2049 subscribers = subs[0];2050 afters = subs[1];2051 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2052 Y.Assert.isArray(afters);2053 Y.Assert.areSame(0, subscribers.length);2054 Y.Assert.areSame(0, afters.length);2055 Y.Assert.areSame(0, testEvent.hasSubs());2056 },2057 "test target.onceAfter(type, fn) allows duplicate subs": function () {2058 var target = new Y.EventTarget(),2059 events = target._yuievt.events,2060 count = 0,2061 subs,2062 subscribers,2063 afters,2064 testEvent, handle1, handle2;2065 function callback() {2066 count++;2067 }2068 handle1 = target.onceAfter("test", callback);2069 handle2 = target.onceAfter("test", callback);2070 testEvent = events.test;2071 subs = testEvent.getSubs();2072 subscribers = subs[0];2073 afters = subs[1];2074 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2075 Y.Assert.isArray(afters);2076 Y.Assert.areSame(0, subscribers.length);2077 Y.Assert.areSame(2, afters.length);2078 Y.Assert.areSame(2, testEvent.hasSubs());2079 Y.Assert.areNotSame(handle1, handle2);2080 Y.Assert.areSame(testEvent, handle1.evt);2081 Y.Assert.areSame(handle1.evt, handle2.evt);2082 Y.Assert.areNotSame(handle1.sub, handle2.sub);2083 target.fire("test");2084 Y.Assert.areSame(2, count);2085 // Test that fire() resulted in immediate detach of onceAfter() sub2086 testEvent = events.test;2087 subs = testEvent.getSubs();2088 subscribers = subs[0];2089 afters = subs[1];2090 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2091 Y.Assert.isArray(afters);2092 Y.Assert.areSame(0, subscribers.length);2093 Y.Assert.areSame(0, afters.length);2094 Y.Assert.areSame(0, testEvent.hasSubs());2095 target.fire("test");2096 Y.Assert.areSame(2, count);2097 },2098 "test target.onceAfter(type, fn, obj)": function () {2099 var target = new Y.EventTarget(),2100 obj = {},2101 count = 0,2102 thisObj1, thisObj2, argCount, testEvent;2103 function callback() {2104 count++;2105 thisObj1 = this;2106 argCount = arguments.length;2107 }2108 target.onceAfter("test", callback, obj);2109 target.fire("test");2110 Y.Assert.areSame(1, count);2111 Y.Assert.areSame(obj, thisObj1);2112 Y.Assert.areSame(0, argCount);2113 // Subscriber should be detached, so count should not increment2114 target.fire("test");2115 Y.Assert.areSame(1, count);2116 target.onceAfter("test", function () {2117 thisObj2 = this;2118 });2119 target.fire("test");2120 Y.Assert.areSame(1, count);2121 Y.Assert.areSame(obj, thisObj1);2122 Y.Assert.areSame(target, thisObj2);2123 // Subscriber should be detached, so count should not increment2124 target.fire("test");2125 Y.Assert.areSame(1, count);2126 },2127 "test target.onceAfter(type, fn, obj, args)": function () {2128 var target = new Y.EventTarget(),2129 obj = {},2130 count = 0,2131 args = '',2132 argCount,2133 thisObj1, thisObj2, testEvent;2134 function callback() {2135 count++;2136 thisObj1 = this;2137 argCount = arguments.length;2138 for (var i = 0, len = argCount; i < len; ++i) {2139 args += arguments[i];2140 }2141 }2142 target.onceAfter("test", callback, obj, "A");2143 target.fire("test");2144 Y.Assert.areSame(1, count);2145 Y.Assert.areSame(obj, thisObj1);2146 Y.Assert.areSame("A", args);2147 // Subscriber should be detached, so count should not increment2148 target.fire("test");2149 Y.Assert.areSame(1, count);2150 target.onceAfter("test", function () {2151 thisObj2 = this;2152 });2153 target.fire("test");2154 Y.Assert.areSame(1, count);2155 Y.Assert.areSame(obj, thisObj1);2156 Y.Assert.areSame(target, thisObj2);2157 // Subscriber should be detached, so count should not increment2158 target.fire("test");2159 Y.Assert.areSame(1, count);2160 },2161 "test target.onceAfter([type], fn)": function () {2162 var target = new Y.EventTarget(),2163 events = target._yuievt.events,2164 subs,2165 subscribers,2166 afters,2167 handle, thisObj, fired, argCount, testEvent;2168 function callback() {2169 fired = true;2170 thisObj = this;2171 argCount = arguments.length;2172 }2173 handle = target.onceAfter(["test"], callback);2174 testEvent = events.test;2175 subs = testEvent.getSubs();2176 subscribers = subs[0];2177 afters = subs[1];2178 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2179 Y.Assert.isArray(afters);2180 Y.Assert.areSame(0, subscribers.length);2181 Y.Assert.areSame(1, afters.length);2182 Y.Assert.areSame(1, testEvent.hasSubs());2183 Y.Assert.isInstanceOf(Y.EventHandle, handle);2184 Y.Assert.isArray(handle.evt);2185 Y.Assert.areSame(testEvent, handle.evt[0].evt);2186 Y.Assert.isUndefined(handle.sub);2187 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);2188 target.fire("test");2189 Y.Assert.isTrue(fired);2190 Y.Assert.areSame(target, thisObj);2191 Y.Assert.areSame(0, argCount);2192 Y.Assert.areSame(0, testEvent._afters.length);2193 fired = false;2194 target.fire("test");2195 Y.Assert.isFalse(fired);2196 },2197 "test target.onceAfter([typeA, typeB], fn)": function () {2198 var target = new Y.EventTarget(),2199 events = target._yuievt.events,2200 count = 0,2201 subs,2202 subscribers,2203 afters,2204 handle,2205 thisObj,2206 testEvent1,2207 testEvent2;2208 function callback() {2209 count++;2210 thisObj = this;2211 }2212 handle = target.onceAfter(["test1", "test2"], callback);2213 testEvent1 = events.test1;2214 subs = testEvent1.getSubs();2215 subscribers = subs[0];2216 afters = subs[1];2217 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent1);2218 Y.Assert.isArray(afters);2219 Y.Assert.areSame(0, subscribers.length);2220 Y.Assert.areSame(1, afters.length);2221 Y.Assert.areSame(1, testEvent1.hasSubs());2222 testEvent2 = events.test2;2223 subs = testEvent2.getSubs();2224 subscribers = subs[0];2225 afters = subs[1];2226 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent2);2227 Y.Assert.isArray(afters);2228 Y.Assert.areSame(0,subscribers.length);2229 Y.Assert.areSame(1, afters.length);2230 Y.Assert.areSame(1, testEvent2.hasSubs());2231 Y.Assert.isInstanceOf(Y.EventHandle, handle);2232 Y.Assert.isArray(handle.evt);2233 Y.Assert.areSame(testEvent1, handle.evt[0].evt);2234 Y.Assert.areSame(testEvent2, handle.evt[1].evt);2235 Y.Assert.areNotSame(testEvent1, testEvent2);2236 Y.Assert.isUndefined(handle.sub);2237 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);2238 target.fire("test1");2239 Y.Assert.areSame(1, count);2240 Y.Assert.areSame(target, thisObj);2241 Y.Assert.areSame(0, testEvent1._afters && testEvent1._afters.length);2242 target.fire("test2");2243 Y.Assert.areSame(2, count);2244 Y.Assert.areSame(target, thisObj);2245 Y.Assert.areSame(0, testEvent2._afters.length);2246 },2247 "test target.onceAfter([typeA, typeA], fn)": function () {2248 var target = new Y.EventTarget(),2249 events = target._yuievt.events,2250 count = 0,2251 subs,2252 subscribers,2253 afters,2254 handle, thisObj, testEvent;2255 function callback() {2256 count++;2257 thisObj = this;2258 }2259 handle = target.onceAfter(["test", "test"], callback);2260 testEvent = events.test;2261 subs = testEvent.getSubs();2262 subscribers = subs[0];2263 afters = subs[1];2264 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2265 Y.Assert.isArray(afters);2266 Y.Assert.areSame(0, subscribers.length);2267 Y.Assert.areSame(2, afters.length);2268 Y.Assert.areSame(2, testEvent.hasSubs());2269 Y.Assert.isInstanceOf(Y.EventHandle, handle);2270 Y.Assert.isArray(handle.evt);2271 Y.Assert.areSame(testEvent, handle.evt[0].evt);2272 Y.Assert.areSame(testEvent, handle.evt[1].evt);2273 Y.Assert.isUndefined(handle.sub);2274 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);2275 target.fire("test");2276 Y.Assert.areSame(2, count);2277 Y.Assert.areSame(target, thisObj);2278 Y.Assert.areSame(0, testEvent._afters && testEvent._afters.length);2279 },2280 "test target.onceAfter([], fn) does nothing": function () {2281 var target = new Y.EventTarget(),2282 events = target._yuievt.events,2283 count = 0,2284 handle, name, subs, i;2285 function callback() {2286 Y.Assert.fail("I don't know how this got called");2287 }2288 handle = target.onceAfter([], callback);2289 for (name in events) {2290 if (events.hasOwnProperty(name)) {2291 subs = events[name]._afters;2292 for (i = subs.length - 1; i >= 0; --i) {2293 if (subs[i].fn === callback) {2294 Y.Assert.fail("subscription registered for '" + name + "' event");2295 }2296 }2297 }2298 }2299 Y.Assert.isInstanceOf(Y.EventHandle, handle);2300 Y.Assert.isArray(handle.evt);2301 Y.Assert.areSame(0, handle.evt.length);2302 Y.Assert.isUndefined(handle.sub);2303 },2304 "test target.onceAfter([{ fn: fn, context: obj }]) does nothing": function () {2305 var target = new Y.EventTarget(),2306 events = target._yuievt.events,2307 count = 0,2308 handle, name, subs, i;2309 function callback() {2310 Y.Assert.fail("I don't know how this got called");2311 }2312 handle = target.onceAfter([{ fn: callback, context: {} }]);2313 for (name in events) {2314 if (events.hasOwnProperty(name)) {2315 subs = events[name]._afters;2316 for (i = subs.length - 1; i >= 0; --i) {2317 if (subs[i].fn === callback) {2318 Y.Assert.fail("subscription registered for '" + name + "' event");2319 }2320 }2321 }2322 }2323 Y.Assert.isInstanceOf(Y.EventHandle, handle);2324 Y.Assert.isArray(handle.evt);2325 Y.Assert.areSame(0, handle.evt.length);2326 Y.Assert.isUndefined(handle.sub);2327 },2328 "test target.onceAfter({ type: fn })": function () {2329 var target = new Y.EventTarget(),2330 events = target._yuievt.events,2331 subs,2332 subscribers,2333 afters,2334 handle, thisObj, fired, argCount, testEvent;2335 function callback() {2336 fired = true;2337 thisObj = this;2338 argCount = arguments.length;2339 }2340 handle = target.onceAfter({ "test1": callback });2341 testEvent = events.test1;2342 subs = testEvent.getSubs();2343 subscribers = subs[0];2344 afters = subs[1];2345 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2346 Y.Assert.isArray(afters);2347 Y.Assert.areSame(0, subscribers.length);2348 Y.Assert.areSame(1, afters.length);2349 Y.Assert.areSame(1, testEvent.hasSubs());2350 Y.Assert.isInstanceOf(Y.EventHandle, handle);2351 Y.Assert.isArray(handle.evt);2352 Y.Assert.areSame(1, handle.evt.length);2353 Y.Assert.areSame(testEvent, handle.evt[0].evt);2354 Y.Assert.isUndefined(handle.sub);2355 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);2356 target.fire("test1");2357 Y.Assert.isTrue(fired);2358 Y.Assert.areSame(target, thisObj);2359 Y.Assert.areSame(0, argCount);2360 Y.Assert.areSame(0, testEvent._afters.length);2361 handle = target.onceAfter({2362 "test2": callback,2363 "test3": callback2364 });2365 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);2366 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);2367 Y.Assert.areSame(1, events.test2._afters.length);2368 Y.Assert.areSame(1, events.test3._afters.length);2369 Y.Assert.isInstanceOf(Y.EventHandle, handle);2370 Y.Assert.isArray(handle.evt);2371 Y.Assert.areSame(2, handle.evt.length);2372 Y.Assert.areSame(events.test2, handle.evt[0].evt);2373 Y.Assert.areSame(events.test3, handle.evt[1].evt);2374 Y.Assert.isUndefined(handle.sub);2375 target.fire("test2");2376 Y.Assert.areSame(0, events.test2._afters.length);2377 Y.Assert.areSame(1, events.test3._afters.length);2378 target.fire("test3");2379 Y.Assert.areSame(0, events.test2._afters.length);2380 Y.Assert.areSame(0, events.test3._afters.length);2381 },2382 "test target.onceAfter({ type: true }, fn)": function () {2383 var target = new Y.EventTarget(),2384 events = target._yuievt.events,2385 subs,2386 subscribers,2387 after,2388 handle,2389 thisObj,2390 fired,2391 argCount,2392 testEvent;2393 function callback() {2394 fired = true;2395 thisObj = this;2396 argCount = arguments.length;2397 }2398 handle = target.onceAfter({ "test1": true }, callback);2399 testEvent = events.test1;2400 subs = testEvent.getSubs();2401 subscribers = subs[0];2402 afters = subs[1];2403 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2404 Y.Assert.isArray(afters);2405 Y.Assert.areSame(0, subscribers.length);2406 Y.Assert.areSame(1, afters.length);2407 Y.Assert.areSame(1, testEvent.hasSubs());2408 Y.Assert.isInstanceOf(Y.EventHandle, handle);2409 Y.Assert.isArray(handle.evt);2410 Y.Assert.areSame(1, handle.evt.length);2411 Y.Assert.areSame(testEvent, handle.evt[0].evt);2412 Y.Assert.isUndefined(handle.sub);2413 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);2414 target.fire("test1");2415 Y.Assert.isTrue(fired);2416 Y.Assert.areSame(target, thisObj);2417 Y.Assert.areSame(0, argCount);2418 Y.Assert.areSame(0, testEvent._afters.length);2419 handle = target.onceAfter({ "test2": 1, "test3": false }, callback);2420 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);2421 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);2422 Y.Assert.areSame(1, events.test2._afters.length);2423 Y.Assert.areSame(1, events.test3._afters.length);2424 Y.Assert.isInstanceOf(Y.EventHandle, handle);2425 Y.Assert.isArray(handle.evt);2426 Y.Assert.areSame(2, handle.evt.length);2427 Y.Assert.areSame(events.test2, handle.evt[0].evt);2428 Y.Assert.areSame(events.test3, handle.evt[1].evt);2429 Y.Assert.isUndefined(handle.sub);2430 target.fire("test2");2431 Y.Assert.areSame(0, events.test2._afters.length);2432 Y.Assert.areSame(1, events.test3._afters.length);2433 target.fire("test3");2434 Y.Assert.areSame(0, events.test2._afters.length);2435 Y.Assert.areSame(0, events.test3._afters.length);2436 },2437 "test target.onceAfter(type, { handleEvents: fn })": function () {2438 var target = new Y.EventTarget(),2439 events = target._yuievt.events,2440 subs,2441 subscribers,2442 afters,2443 obj, handle, thisObj, fired, argCount, testEvent;2444 function callback() {2445 fired = true;2446 thisObj = this;2447 argCount = arguments.length;2448 }2449 obj = { handleEvents: callback };2450 handle = target.onceAfter("test", obj);2451 testEvent = events.test;2452 subs = testEvent.getSubs();2453 subscribers = subs[0];2454 afters = subs[1];2455 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2456 Y.Assert.isArray(testEvent._afters);2457 Y.Assert.areSame(0, testEvent._subscribers.length);2458 Y.Assert.areSame(1, testEvent._afters.length);2459 Y.Assert.areSame(1, testEvent.hasSubs());2460 Y.Assert.isInstanceOf(Y.EventHandle, handle);2461 Y.Assert.areSame(testEvent, handle.evt);2462 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);2463 // Barring support, this is where the error will be thrown.2464 // ET.onceAfter() doesn't verify the second arg is a function, and2465 // Subscriber doesn't type check before treating it as a function.2466 target.fire("test");2467 Y.Assert.isTrue(fired);2468 Y.Assert.areSame(obj, thisObj);2469 Y.Assert.areSame(0, argCount);2470 // Fire should immediate detach the subscription2471 testEvent = events.test;2472 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2473 Y.Assert.isArray(testEvent._afters);2474 Y.Assert.areSame(0, testEvent._subscribers.length);2475 Y.Assert.areSame(0, testEvent._afters.length);2476 Y.Assert.areSame(0, testEvent.hasSubs());2477 },2478 "test callback context": function () {2479 var target = new Y.EventTarget(),2480 events = target._yuievt.events,2481 targetCount = 0,2482 objCount = 0,2483 obj = {};2484 function isTarget() {2485 Y.Assert.areSame(target, this);2486 targetCount++;2487 }2488 function isObj() {2489 Y.Assert.areSame(obj, this);2490 objCount++;2491 }2492 target.onceAfter("test1", isTarget);2493 target.fire("test1");2494 Y.Assert.areSame(1, targetCount);2495 Y.Assert.areSame(0, objCount);2496 target.onceAfter("test2", isObj, obj);2497 target.fire("test2");2498 Y.Assert.areSame(1, targetCount);2499 Y.Assert.areSame(1, objCount);2500 target.onceAfter("test3", isObj, obj, {});2501 target.fire("test3");2502 Y.Assert.areSame(1, targetCount);2503 Y.Assert.areSame(2, objCount);2504 target.onceAfter("test4", isObj, obj, null, {}, {});2505 target.fire("test4");2506 Y.Assert.areSame(1, targetCount);2507 Y.Assert.areSame(3, objCount);2508 target.onceAfter("test5", isTarget, null, {});2509 target.fire("test5");2510 Y.Assert.areSame(2, targetCount);2511 Y.Assert.areSame(3, objCount);2512 target.onceAfter("prefix:test6", isTarget);2513 target.fire("prefix:test6", obj);2514 Y.Assert.areSame(3, targetCount);2515 Y.Assert.areSame(3, objCount);2516 target.onceAfter(["test7", "prefix:test8"], isObj, obj);2517 target.fire("test7");2518 target.fire("prefix:test8");2519 Y.Assert.areSame(3, targetCount);2520 Y.Assert.areSame(5, objCount);2521 target.onceAfter({ "test9": isObj }, null, obj);2522 target.fire("test9");2523 Y.Assert.areSame(3, targetCount);2524 Y.Assert.areSame(6, objCount);2525 target.onceAfter({2526 "test10": { fn: isTarget },2527 "test11": { fn: isObj, context: obj }2528 });2529 target.fire("test10");2530 target.fire("test11");2531 Y.Assert.areSame(4, targetCount);2532 Y.Assert.areSame(7, objCount);2533 target.onceAfter({2534 "test12": { fn: isObj },2535 "prefix:test13": { fn: isTarget, context: target }2536 }, null, obj);2537 target.fire("test12");2538 target.fire("prefix:test13");2539 Y.Assert.areSame(5, targetCount);2540 Y.Assert.areSame(8, objCount);2541 Y.Assert.areSame(0, events.test1._afters.length);2542 Y.Assert.areSame(0, events.test2._afters.length);2543 Y.Assert.areSame(0, events.test3._afters.length);2544 Y.Assert.areSame(0, events.test4._afters.length);2545 Y.Assert.areSame(0, events.test5._afters.length);2546 Y.Assert.areSame(0, events['prefix:test6']._afters.length);2547 Y.Assert.areSame(0, events.test7._afters.length);2548 Y.Assert.areSame(0, events['prefix:test8']._afters.length);2549 Y.Assert.areSame(0, events.test9._afters.length);2550 Y.Assert.areSame(0, events.test10._afters.length);2551 Y.Assert.areSame(0, events.test11._afters.length);2552 Y.Assert.areSame(0, events.test12._afters.length);2553 Y.Assert.areSame(0, events['prefix:test13']._afters.length);2554 },2555 "test subscription bound args": function () {2556 var target = new Y.EventTarget(),2557 events = target._yuievt.events,2558 obj = {},2559 args;2560 function callback() {2561 args = Y.Array(arguments, 0, true);2562 }2563 target.onceAfter("test1", callback, {}, "a", 1, obj, null);2564 target.fire("test1");2565 Y.ArrayAssert.itemsAreSame(["a", 1, obj, null], args);2566 target.onceAfter(["test2", "test3"], callback, null, "a", 2.3, obj, null);2567 target.fire("test2");2568 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);2569 args = [];2570 target.fire("test3");2571 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);2572 // ugh, requiring two placeholders for (unused) fn and context is ooogly2573 target.onceAfter({2574 "test4": callback,2575 "test5": callback2576 }, null, null, "a", 4.5, obj, null);2577 target.fire("test4");2578 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);2579 args = [];2580 target.fire("test5");2581 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);2582 target.onceAfter({2583 "test6": true,2584 "test7": false2585 }, callback, {}, "a", 6.7, obj, null);2586 target.fire("test6");2587 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);2588 args = [];2589 target.fire("test7");2590 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);2591 Y.Assert.areSame(0, events.test1._afters.length);2592 Y.Assert.areSame(0, events.test2._afters.length);2593 Y.Assert.areSame(0, events.test3._afters.length);2594 Y.Assert.areSame(0, events.test4._afters.length);2595 Y.Assert.areSame(0, events.test5._afters.length);2596 Y.Assert.areSame(0, events.test6._afters.length);2597 Y.Assert.areSame(0, events.test7._afters.length);2598 },2599 "test target.onceAfter('click', fn) registers custom event only": function () {2600 var target = new Y.EventTarget(),2601 events = target._yuievt.events,2602 fired = false;2603 target.onceAfter("click", function () {2604 fired = true;2605 // Not an emitFacade event, so there's no e to verify type2606 });2607 Y.Assert.isInstanceOf(Y.CustomEvent, events.click);2608 Y.Assert.isUndefined(events.click.domkey);2609 Y.Assert.areSame(1, events.click._afters.length);2610 target.fire("click");2611 Y.Assert.isTrue(fired);2612 Y.Assert.areSame(0, events.click._afters.length);2613 }2614}));2615baseSuite.add(new Y.Test.Case({2616 name: "target.detach",2617 "test target.detach() with not subs is harmless": function () {2618 var target = new Y.EventTarget();2619 function fn() {}2620 target.detach('test');2621 target.detach('category|test');2622 target.detach('prefix:test');2623 target.detach('category|prefix:test');2624 target.detach('test', fn);2625 target.detach('category|test', fn);2626 target.detach('prefix:test', fn);2627 target.detach('category|prefix:test', fn);2628 target.detach();2629 Y.Assert.isTrue(true);2630 },2631 "test target.detachAll() with not subs is harmless": function () {2632 var target = new Y.EventTarget();2633 target.detachAll();2634 target.detachAll('test');2635 target.detachAll('prefix:test');2636 target.detachAll('category|test');2637 target.detachAll('category|prefix:test');2638 Y.Assert.isTrue(true);2639 },2640 "test target.on() + target.detach(type, fn)": function () {2641 var count = 0,2642 target = new Y.EventTarget();2643 function fn() {2644 count++;2645 }2646 target.on('test', fn);2647 target.fire('test');2648 Y.Assert.areSame(1, count);2649 target.detach('test', fn);2650 target.fire('test');2651 Y.Assert.areSame(1, count);2652 target.on('test', fn);2653 target.on('test', fn);2654 target.fire('test');2655 Y.Assert.areSame(3, count);2656 target.detach('test', fn);2657 target.fire('test');2658 Y.Assert.areSame(3, count);2659 },2660 "test target.on(type, fn, thisObj) + target.detach(type, fn)": function () {2661 var count = 0,2662 a = {},2663 b = {},2664 target = new Y.EventTarget();2665 function fn() {2666 count++;2667 }2668 target.on('test', fn, a);2669 target.fire('test');2670 Y.Assert.areSame(1, count);2671 target.detach('test', fn);2672 target.fire('test');2673 Y.Assert.areSame(1, count);2674 target.on('test', fn, a);2675 target.on('test', fn, b);2676 target.fire('test');2677 Y.Assert.areSame(3, count);2678 target.detach('test', fn);2679 target.fire('test');2680 Y.Assert.areSame(3, count);2681 },2682 "test target.on() + target.detach(type)": function () {2683 var count = 0,2684 target = new Y.EventTarget();2685 function fn() {2686 count++;2687 }2688 target.on('test', fn);2689 target.fire('test');2690 Y.Assert.areSame(1, count);2691 target.detach('test');2692 target.fire('test');2693 Y.Assert.areSame(1, count);2694 target.on('test', fn);2695 target.on('test', fn);2696 target.fire('test');2697 Y.Assert.areSame(3, count);2698 target.detach('test');2699 target.fire('test');2700 Y.Assert.areSame(3, count);2701 },2702 "test target.on() + target.detach()": function () {2703 var count = 0,2704 target = new Y.EventTarget();2705 function fn() {2706 count++;2707 }2708 target.on('test', fn);2709 target.fire('test');2710 Y.Assert.areSame(1, count);2711 target.detach();2712 target.fire('test');2713 Y.Assert.areSame(1, count);2714 target.on('test', fn);2715 target.on('test', fn);2716 target.fire('test');2717 Y.Assert.areSame(3, count);2718 target.detach();2719 target.fire('test');2720 Y.Assert.areSame(3, count);2721 },2722 "test target.on() + target.detachAll()": function () {2723 var count = 0,2724 target = new Y.EventTarget();2725 function fn() {2726 count++;2727 }2728 target.on('test', fn);2729 target.fire('test');2730 Y.Assert.areSame(1, count);2731 target.detachAll();2732 target.fire('test');2733 Y.Assert.areSame(1, count);2734 target.on('test', fn);2735 target.on('test', fn);2736 target.fire('test');2737 Y.Assert.areSame(3, count);2738 target.detachAll();2739 target.fire('test');2740 Y.Assert.areSame(3, count);2741 },2742 "test target.on() + handle.detach()": function () {2743 var count = 0,2744 target = new Y.EventTarget(),2745 sub;2746 function increment() {2747 count++;2748 }2749 sub = target.on('test', increment);2750 target.fire('test');2751 Y.Assert.areSame(1, count);2752 sub.detach();2753 target.fire('test');2754 Y.Assert.areSame(1, count);2755 },2756 "test target.on('cat|__', fn) + target.detach('cat|___')": function () {2757 var count = 0,2758 target = new Y.EventTarget();2759 function increment() {2760 count++;2761 }2762 target.on('cat|test', increment);2763 target.fire('test');2764 Y.Assert.areSame(1, count);2765 target.detach('cat|test');2766 target.fire('test');2767 Y.Assert.areSame(1, count);2768 },2769 "test target.on('cat|__', fn) + target.detach('cat|___', fn)": function () {2770 var count = 0,2771 target = new Y.EventTarget();2772 function increment() {2773 count++;2774 }2775 target.on('cat|test', increment);2776 target.fire('test');2777 Y.Assert.areSame(1, count);2778 target.detach('cat|test', increment);2779 target.fire('test');2780 Y.Assert.areSame(1, count);2781 },2782 "test target.on('cat|__', fn) + target.detach('cat|*')": function () {2783 var count = 0,2784 target = new Y.EventTarget();2785 function increment() {2786 count++;2787 }2788 target.on('cat|test', increment);2789 target.fire('test');2790 Y.Assert.areSame(1, count);2791 target.detach('cat|*');2792 target.fire('test');2793 Y.Assert.areSame(1, count);2794 },2795 "test target.on({...}) + target.detach(type)": function () {2796 var count = 0,2797 target = new Y.EventTarget();2798 function increment() {2799 count++;2800 }2801 target.on({2802 test1: increment,2803 test2: increment2804 });2805 target.fire('test1');2806 target.fire('test2');2807 Y.Assert.areSame(2, count);2808 target.detach('test1');2809 target.fire('test1');2810 Y.Assert.areSame(2, count);2811 target.fire('test2');2812 Y.Assert.areSame(3, count);2813 target.detach('test2');2814 target.fire('test1');2815 target.fire('test2');2816 Y.Assert.areSame(3, count);2817 },2818 "test target.on({...}) + target.detach(type, fn)": function () {2819 var count = 0,2820 target = new Y.EventTarget();2821 function increment() {2822 count++;2823 }2824 target.on({2825 test1: increment,2826 test2: increment2827 });2828 target.fire('test1');2829 target.fire('test2');2830 Y.Assert.areSame(2, count);2831 target.detach('test1', increment);2832 target.fire('test1');2833 Y.Assert.areSame(2, count);2834 target.fire('test2');2835 Y.Assert.areSame(3, count);2836 target.detach('test2', increment);2837 target.fire('test1');2838 target.fire('test2');2839 Y.Assert.areSame(3, count);2840 },2841 "test target.on({...}) + target.detachAll()": function () {2842 var count = 0,2843 target = new Y.EventTarget();2844 function increment() {2845 count++;2846 }2847 target.on({2848 test1: increment,2849 test2: increment2850 });2851 target.fire('test1');2852 target.fire('test2');2853 Y.Assert.areSame(2, count);2854 target.detachAll();2855 target.fire('test1');2856 Y.Assert.areSame(2, count);2857 target.fire('test2');2858 Y.Assert.areSame(2, count);2859 },2860 "test target.on({'cat|type': fn}) + target.detach(type, fn)": function () {2861 var count = 0,2862 target = new Y.EventTarget();2863 function increment() {2864 count++;2865 }2866 target.on({2867 'cat|test1': increment,2868 test2: increment2869 });2870 target.fire('test1');2871 target.fire('test2');2872 Y.Assert.areSame(2, count);2873 target.detach('test1');2874 target.fire('test1');2875 Y.Assert.areSame(2, count);2876 target.fire('test2');2877 Y.Assert.areSame(3, count);2878 },2879 "test target.on({'cat|type': fn}) + target.detach('cat|type')": function () {2880 var count = 0,2881 target = new Y.EventTarget();2882 function increment() {2883 count++;2884 }2885 target.on({2886 'cat|test1': increment,2887 test2: increment2888 });2889 target.fire('test1');2890 target.fire('test2');2891 Y.Assert.areSame(2, count);2892 target.detach('cat|test1');2893 target.fire('test1');2894 Y.Assert.areSame(2, count);2895 target.fire('test2');2896 Y.Assert.areSame(3, count);2897 },2898 "test target.on({'cat|type': fn}) + target.detach('cat|*')": function () {2899 var count = 0,2900 target = new Y.EventTarget();2901 function increment() {2902 count++;2903 }2904 target.on({2905 'cat|test1': increment,2906 'cat|test2': increment,2907 test3: increment2908 });2909 target.fire('test1');2910 target.fire('test2');2911 target.fire('test3');2912 Y.Assert.areSame(3, count);2913 target.detach('cat|*');2914 target.fire('test1');2915 Y.Assert.areSame(3, count);2916 target.fire('test2');2917 Y.Assert.areSame(3, count);2918 target.fire('test3');2919 Y.Assert.areSame(4, count);2920 },2921 "test target.on([type], fn) + target.detach(type, fn)": function () {2922 var count = 0,2923 target = new Y.EventTarget();2924 function increment() {2925 count++;2926 }2927 target.on(['test'], increment);2928 target.fire('test');2929 Y.Assert.areSame(1, count);2930 target.detach('test', increment);2931 target.fire('test');2932 Y.Assert.areSame(1, count);2933 },2934 "test target.on([type], fn) + target.detach(type)": function () {2935 var count = 0,2936 target = new Y.EventTarget();2937 function increment() {2938 count++;2939 }2940 target.on(['test'], increment);2941 target.fire('test');2942 Y.Assert.areSame(1, count);2943 target.detach('test');2944 target.fire('test');2945 Y.Assert.areSame(1, count);2946 },2947 "test target.on([typeA, typeB], fn) + target.detach(typeA)": function () {2948 var count = 0,2949 target = new Y.EventTarget();2950 function increment() {2951 count++;2952 }2953 target.on(['test1', 'test2'], increment);2954 target.fire('test1');2955 target.fire('test2');2956 Y.Assert.areSame(2, count);2957 target.detach('test1');2958 target.fire('test1');2959 target.fire('test2');2960 Y.Assert.areSame(3, count);2961 target.detach('test2');2962 target.fire('test1');2963 target.fire('test2');2964 Y.Assert.areSame(3, count);2965 },2966 "test target.on([typeA, typeB], fn) + target.detach()": function () {2967 var count = 0,2968 target = new Y.EventTarget();2969 function increment() {2970 count++;2971 }2972 target.on(['test1', 'test2'], increment);2973 target.fire('test1');2974 target.fire('test2');2975 Y.Assert.areSame(2, count);2976 target.detach();2977 target.fire('test1');2978 target.fire('test2');2979 Y.Assert.areSame(2, count);2980 },2981 "test target.on({}) + target.detach() is harmless": function () {2982 var count = 0,2983 target = new Y.EventTarget();2984 target.on({});2985 target.detach();2986 Y.Assert.areSame(0, count);2987 },2988 "test target.on([], fn) + target.detach() is harmless": function () {2989 var count = 0,2990 target = new Y.EventTarget();2991 function increment() {2992 count++;2993 }2994 target.on([], increment);2995 target.detach();2996 Y.Assert.areSame(0, count);2997 },2998 "test target.on({}) + handle.detach() is harmless": function () {2999 var target = new Y.EventTarget(),3000 handle;3001 handle = target.on({});3002 handle.detach();3003 Y.Assert.isTrue(true);3004 },3005 "test target.on([], fn) + handle.detach() is harmless": function () {3006 var target = new Y.EventTarget(),3007 handle;3008 handle = target.on([], function () {});3009 handle.detach();3010 Y.Assert.isTrue(true);3011 },3012 "test target.on({...}) + handle.detach()": function () {3013 var count = 0,3014 target = new Y.EventTarget(),3015 handle;3016 function increment() {3017 count++;3018 }3019 handle = target.on({3020 test1: increment,3021 test2: increment3022 });3023 target.fire('test1');3024 target.fire('test2');3025 Y.Assert.areSame(2, count);3026 handle.detach();3027 target.fire('test1');3028 target.fire('test2');3029 Y.Assert.areSame(2, count);3030 },3031 "test target.on([typeA, typeB], fn) + handle.detach()": function () {3032 var count = 0,3033 target = new Y.EventTarget(),3034 handle;3035 function increment() {3036 count++;3037 }3038 handle = target.on(['test1', 'test2'], increment);3039 target.fire('test1');3040 target.fire('test2');3041 Y.Assert.areSame(2, count);3042 handle.detach();3043 target.fire('test1');3044 target.fire('test2');3045 Y.Assert.areSame(2, count);3046 },3047 "test target.on([typeA, typeA], fn) + handle.detach()": function () {3048 var count = 0,3049 target = new Y.EventTarget(),3050 handle;3051 function increment() {3052 count++;3053 }3054 handle = target.on(['test1', 'test2'], increment);3055 target.fire('test1');3056 target.fire('test2');3057 Y.Assert.areSame(2, count);3058 handle.detach();3059 target.fire('test1');3060 target.fire('test2');3061 Y.Assert.areSame(2, count);3062 },3063 "test target.on(type) + target.detach(prefix:type)": function () {3064 var target = new Y.EventTarget({ prefix: 'pre' }),3065 count = 0;3066 function increment() {3067 count++;3068 }3069 target.on('test', increment);3070 target.fire('test');3071 Y.Assert.areSame(1, count);3072 target.detach('test');3073 target.fire('test');3074 Y.Assert.areSame(1, count);3075 target.on('test', increment);3076 target.fire('test');3077 Y.Assert.areSame(2, count);3078 target.detach('pre:test');3079 target.fire('test');3080 Y.Assert.areSame(2, count);3081 },3082 "test target.after() + target.detach(type, fn)": function () {3083 var count = 0,3084 target = new Y.EventTarget();3085 function fn() {3086 count++;3087 }3088 target.after('test', fn);3089 target.fire('test');3090 Y.Assert.areSame(1, count);3091 target.detach('test', fn);3092 target.fire('test');3093 Y.Assert.areSame(1, count);3094 target.after('test', fn);3095 target.after('test', fn);3096 target.fire('test');3097 Y.Assert.areSame(3, count);3098 target.detach('test', fn);3099 target.fire('test');3100 Y.Assert.areSame(3, count);3101 },3102 "test target.after(type, fn, thisObj) + target.detach(type, fn)": function () {3103 var count = 0,3104 a = {},3105 b = {},3106 target = new Y.EventTarget();3107 function fn() {3108 count++;3109 }3110 target.after('test', fn, a);3111 target.fire('test');3112 Y.Assert.areSame(1, count);3113 target.detach('test', fn);3114 target.fire('test');3115 Y.Assert.areSame(1, count);3116 target.after('test', fn, a);3117 target.after('test', fn, b);3118 target.fire('test');3119 Y.Assert.areSame(3, count);3120 target.detach('test', fn);3121 target.fire('test');3122 Y.Assert.areSame(3, count);3123 },3124 "test target.after() + target.detach(type)": function () {3125 var count = 0,3126 target = new Y.EventTarget();3127 function fn() {3128 count++;3129 }3130 target.after('test', fn);3131 target.fire('test');3132 Y.Assert.areSame(1, count);3133 target.detach('test');3134 target.fire('test');3135 Y.Assert.areSame(1, count);3136 target.after('test', fn);3137 target.after('test', fn);3138 target.fire('test');3139 Y.Assert.areSame(3, count);3140 target.detach('test');3141 target.fire('test');3142 Y.Assert.areSame(3, count);3143 },3144 "test target.after() + target.detach()": function () {3145 var count = 0,3146 target = new Y.EventTarget();3147 function fn() {3148 count++;3149 }3150 target.after('test', fn);3151 target.fire('test');3152 Y.Assert.areSame(1, count);3153 target.detach();3154 target.fire('test');3155 Y.Assert.areSame(1, count);3156 target.after('test', fn);3157 target.after('test', fn);3158 target.fire('test');3159 Y.Assert.areSame(3, count);3160 target.detach();3161 target.fire('test');3162 Y.Assert.areSame(3, count);3163 },3164 "test target.after() + target.detachAll()": function () {3165 var count = 0,3166 target = new Y.EventTarget();3167 function fn() {3168 count++;3169 }3170 target.after('test', fn);3171 target.fire('test');3172 Y.Assert.areSame(1, count);3173 target.detachAll();3174 target.fire('test');3175 Y.Assert.areSame(1, count);3176 target.after('test', fn);3177 target.after('test', fn);3178 target.fire('test');3179 Y.Assert.areSame(3, count);3180 target.detachAll();3181 target.fire('test');3182 Y.Assert.areSame(3, count);3183 },3184 "test target.after() + handle.detach()": function () {3185 var count = 0,3186 target = new Y.EventTarget(),3187 sub;3188 function increment() {3189 count++;3190 }3191 sub = target.after('test', increment);3192 target.fire('test');3193 Y.Assert.areSame(1, count);3194 sub.detach();3195 target.fire('test');3196 Y.Assert.areSame(1, count);3197 },3198 "test target.after('cat|__', fn) + target.detach('cat|___')": function () {3199 var count = 0,3200 target = new Y.EventTarget();3201 function increment() {3202 count++;3203 }3204 target.after('cat|test', increment);3205 target.fire('test');3206 Y.Assert.areSame(1, count);3207 target.detach('cat|test');3208 target.fire('test');3209 Y.Assert.areSame(1, count);3210 },3211 "test target.after('cat|__', fn) + target.detach('cat|___', fn)": function () {3212 var count = 0,3213 target = new Y.EventTarget();3214 function increment() {3215 count++;3216 }3217 target.after('cat|test', increment);3218 target.fire('test');3219 Y.Assert.areSame(1, count);3220 target.detach('cat|test', increment);3221 target.fire('test');3222 Y.Assert.areSame(1, count);3223 },3224 "test target.after('cat|__', fn) + target.detach('cat|*')": function () {3225 var count = 0,3226 target = new Y.EventTarget();3227 function increment() {3228 count++;3229 }3230 target.after('cat|test', increment);3231 target.fire('test');3232 Y.Assert.areSame(1, count);3233 target.detach('cat|*');3234 target.fire('test');3235 Y.Assert.areSame(1, count);3236 },3237 "test target.after('cat|__', fn) + target.detach('cat|*'), with prefix": function () {3238 var count = 0,3239 target = new Y.EventTarget({prefix:"foo"});3240 function increment() {3241 count++;3242 }3243 target.after('cat|test', increment);3244 target.fire('test');3245 Y.Assert.areSame(1, count);3246 target.detach('cat|*');3247 target.fire('test');3248 Y.Assert.areSame(1, count);3249 },3250 "test target.after({...}) + target.detach(type)": function () {3251 var count = 0,3252 target = new Y.EventTarget();3253 function increment() {3254 count++;3255 }3256 target.after({3257 test1: increment,3258 test2: increment3259 });3260 target.fire('test1');3261 target.fire('test2');3262 Y.Assert.areSame(2, count);3263 target.detach('test1');3264 target.fire('test1');3265 Y.Assert.areSame(2, count);3266 target.fire('test2');3267 Y.Assert.areSame(3, count);3268 target.detach('test2');3269 target.fire('test1');3270 target.fire('test2');3271 Y.Assert.areSame(3, count);3272 },3273 "test target.after({...}) + target.detach(type, fn)": function () {3274 var count = 0,3275 target = new Y.EventTarget();3276 function increment() {3277 count++;3278 }3279 target.after({3280 test1: increment,3281 test2: increment3282 });3283 target.fire('test1');3284 target.fire('test2');3285 Y.Assert.areSame(2, count);3286 target.detach('test1', increment);3287 target.fire('test1');3288 Y.Assert.areSame(2, count);3289 target.fire('test2');3290 Y.Assert.areSame(3, count);3291 target.detach('test2', increment);3292 target.fire('test1');3293 target.fire('test2');3294 Y.Assert.areSame(3, count);3295 },3296 "test target.after({...}) + target.detachAll()": function () {3297 var count = 0,3298 target = new Y.EventTarget();3299 function increment() {3300 count++;3301 }3302 target.after({3303 test1: increment,3304 test2: increment3305 });3306 target.fire('test1');3307 target.fire('test2');3308 Y.Assert.areSame(2, count);3309 target.detachAll();3310 target.fire('test1');3311 Y.Assert.areSame(2, count);3312 target.fire('test2');3313 Y.Assert.areSame(2, count);3314 },3315 "test target.after({'cat|type': fn}) + target.detach(type, fn)": function () {3316 var count = 0,3317 target = new Y.EventTarget();3318 function increment() {3319 count++;3320 }3321 target.after({3322 'cat|test1': increment,3323 test2: increment3324 });3325 target.fire('test1');3326 target.fire('test2');3327 Y.Assert.areSame(2, count);3328 target.detach('test1');3329 target.fire('test1');3330 Y.Assert.areSame(2, count);3331 target.fire('test2');3332 Y.Assert.areSame(3, count);3333 },3334 "test target.after({'cat|type': fn}) + target.detach('cat|type')": function () {3335 var count = 0,3336 target = new Y.EventTarget();3337 function increment() {3338 count++;3339 }3340 target.after({3341 'cat|test1': increment,3342 test2: increment3343 });3344 target.fire('test1');3345 target.fire('test2');3346 Y.Assert.areSame(2, count);3347 target.detach('cat|test1');3348 target.fire('test1');3349 Y.Assert.areSame(2, count);3350 target.fire('test2');3351 Y.Assert.areSame(3, count);3352 },3353 "test target.after({'cat|type': fn}) + target.detach('cat|*')": function () {3354 var count = 0,3355 target = new Y.EventTarget();3356 function increment() {3357 count++;3358 }3359 target.after({3360 'cat|test1': increment,3361 'cat|test2': increment,3362 test3: increment3363 });3364 target.fire('test1');3365 target.fire('test2');3366 target.fire('test3');3367 Y.Assert.areSame(3, count);3368 target.detach('cat|*');3369 target.fire('test1');3370 Y.Assert.areSame(3, count);3371 target.fire('test2');3372 Y.Assert.areSame(3, count);3373 target.fire('test3');3374 Y.Assert.areSame(4, count);3375 },3376 "test target.after([type], fn) + target.detach(type, fn)": function () {3377 var count = 0,3378 target = new Y.EventTarget();3379 function increment() {3380 count++;3381 }3382 target.after(['test'], increment);3383 target.fire('test');3384 Y.Assert.areSame(1, count);3385 target.detach('test', increment);3386 target.fire('test');3387 Y.Assert.areSame(1, count);3388 },3389 "test target.after([type], fn) + target.detach(type)": function () {3390 var count = 0,3391 target = new Y.EventTarget();3392 function increment() {3393 count++;3394 }3395 target.after(['test'], increment);3396 target.fire('test');3397 Y.Assert.areSame(1, count);3398 target.detach('test');3399 target.fire('test');3400 Y.Assert.areSame(1, count);3401 },3402 "test target.after([typeA, typeB], fn) + target.detach(typeA)": function () {3403 var count = 0,3404 target = new Y.EventTarget();3405 function increment() {3406 count++;3407 }3408 target.after(['test1', 'test2'], increment);3409 target.fire('test1');3410 target.fire('test2');3411 Y.Assert.areSame(2, count);3412 target.detach('test1');3413 target.fire('test1');3414 target.fire('test2');3415 Y.Assert.areSame(3, count);3416 target.detach('test2');3417 target.fire('test1');3418 target.fire('test2');3419 Y.Assert.areSame(3, count);3420 },3421 "test target.after([typeA, typeB], fn) + target.detach()": function () {3422 var count = 0,3423 target = new Y.EventTarget();3424 function increment() {3425 count++;3426 }3427 target.after(['test1', 'test2'], increment);3428 target.fire('test1');3429 target.fire('test2');3430 Y.Assert.areSame(2, count);3431 target.detach();3432 target.fire('test1');3433 target.fire('test2');3434 Y.Assert.areSame(2, count);3435 },3436 "test target.after({}) + target.detach() is harmless": function () {3437 var count = 0,3438 target = new Y.EventTarget();3439 target.after({});3440 target.detach();3441 Y.Assert.areSame(0, count);3442 },3443 "test target.after([], fn) + target.detach() is harmless": function () {3444 var count = 0,3445 target = new Y.EventTarget();3446 function increment() {3447 count++;3448 }3449 target.after([], increment);3450 target.detach();3451 Y.Assert.areSame(0, count);3452 },3453 "test target.after({}) + handle.detach() is harmless": function () {3454 var target = new Y.EventTarget(),3455 handle;3456 handle = target.after({});3457 handle.detach();3458 Y.Assert.isTrue(true);3459 },3460 "test target.after([], fn) + handle.detach() is harmless": function () {3461 var target = new Y.EventTarget(),3462 handle;3463 handle = target.after([], function () {});3464 handle.detach();3465 Y.Assert.isTrue(true);3466 },3467 "test target.after({...}) + handle.detach()": function () {3468 var count = 0,3469 target = new Y.EventTarget(),3470 handle;3471 function increment() {3472 count++;3473 }3474 handle = target.after({3475 test1: increment,3476 test2: increment3477 });3478 target.fire('test1');3479 target.fire('test2');3480 Y.Assert.areSame(2, count);3481 handle.detach();3482 target.fire('test1');3483 target.fire('test2');3484 Y.Assert.areSame(2, count);3485 },3486 "test target.after([typeA, typeB], fn) + handle.detach()": function () {3487 var count = 0,3488 target = new Y.EventTarget(),3489 handle;3490 function increment() {3491 count++;3492 }3493 handle = target.after(['test1', 'test2'], increment);3494 target.fire('test1');3495 target.fire('test2');3496 Y.Assert.areSame(2, count);3497 handle.detach();3498 target.fire('test1');3499 target.fire('test2');3500 Y.Assert.areSame(2, count);3501 },3502 "test target.after([typeA, typeA], fn) + handle.detach()": function () {3503 var count = 0,3504 target = new Y.EventTarget(),3505 handle;3506 function increment() {3507 count++;3508 }3509 handle = target.after(['test1', 'test2'], increment);3510 target.fire('test1');3511 target.fire('test2');3512 Y.Assert.areSame(2, count);3513 handle.detach();3514 target.fire('test1');3515 target.fire('test2');3516 Y.Assert.areSame(2, count);3517 },3518 "test target.after(type) + target.detach(prefix:type)": function () {3519 var target = new Y.EventTarget({ prefix: 'pre' }),3520 count = 0;3521 function increment() {3522 count++;3523 }3524 target.after('test', increment);3525 target.fire('test');3526 Y.Assert.areSame(1, count);3527 target.detach('test');3528 target.fire('test');3529 Y.Assert.areSame(1, count);3530 target.after('test', increment);3531 target.fire('test');3532 Y.Assert.areSame(2, count);3533 target.detach('pre:test');3534 target.fire('test');3535 Y.Assert.areSame(2, count);3536 },3537 "test target.on() + target.after() + target.detach(type) detaches both": function () {3538 var target = new Y.EventTarget(),3539 count = 0;3540 function incrementOn() {3541 count++;3542 }3543 function incrementAfter() {3544 count++;3545 }3546 target.on('test', incrementOn);3547 target.after('test', incrementAfter);3548 target.fire('test');3549 Y.Assert.areSame(2, count);3550 target.detach('test');3551 target.fire('test');3552 Y.Assert.areSame(2, count);3553 },3554 "test target.detach('~AFTER~')": function () {3555 var target = new Y.EventTarget(),3556 count = 0;3557 target.after('test', function () {3558 count++;3559 });3560 target.detach('~AFTER~');3561 target.fire('test');3562 Y.Assert.areSame(1, count);3563 },3564 "test Y.detach(type, fn)": function() {3565 var count = 0;3566 tester = function() {3567 count++;3568 Y.detach('foo', tester);3569 };3570 Y.on('foo', tester);3571 Y.fire('foo');3572 Y.fire('foo');3573 Y.Assert.areEqual(1, count);3574 }3575}));3576baseSuite.add(new Y.Test.Case({3577 name: "target.fire",3578 "test target.fire() with no subscribers": function () {3579 var target = new Y.EventTarget();3580 target.fire("test1");3581 target.fire("test2", "a");3582 target.fire("test3", {});3583 target.fire("foo:test4");3584 target.fire("*:test5");3585 target.fire(":test6");3586 target.fire("|test7");3587 target.fire("~AFTER~test8");3588 target.fire("test9 test10");3589 target.fire("test11_fire");3590 Y.Assert.isTrue(true);3591 },3592 "test on() and fire() argument aggregation": function () {3593 var target = new Y.EventTarget(),3594 args;3595 function callback () {3596 args = Y.Array(arguments, 0, true);3597 }3598 target.on("test1", callback);3599 target.fire("test1");3600 Y.ArrayAssert.itemsAreSame([], args);3601 target.on("test2", callback, {});3602 target.fire("test2");3603 Y.ArrayAssert.itemsAreSame([], args);3604 target.on("test2", callback, {}, "x");3605 target.fire("test2");3606 Y.ArrayAssert.itemsAreSame(["x"], args);3607 target.on("test3", callback, {}, "x", false, null);3608 target.fire("test3");3609 Y.ArrayAssert.itemsAreSame(["x", false, null], args);3610 target.on("test4", callback);3611 target.fire("test4", "a");3612 Y.ArrayAssert.itemsAreSame(["a"], args);3613 target.on("test5", callback);3614 target.fire("test5", "a", false, null);3615 Y.ArrayAssert.itemsAreSame(["a", false, null], args);3616 target.on("test6", callback, {}, "x", false, null);3617 target.fire("test6", "a", true, target);3618 Y.ArrayAssert.itemsAreSame(["a", true, target, "x", false, null], args);3619 target.on("test6", callback, null, "x", false, null);3620 target.fire("test6", "a", true, target);3621 Y.ArrayAssert.itemsAreSame(["a", true, target, "x", false, null], args);3622 },3623 "test target.fire(*) arg is passed as is": function () {3624 var target = new Y.EventTarget(),3625 values = [3626 "a", 1.7, true, {k:"v"}, ["val"], /abc/, new Date(),3627 "", 0, false, {}, [], null, undefined],3628 received = [],3629 i, len;3630 function callback () {3631 received.push(arguments[0]);3632 }3633 target.on("test1", callback);3634 for (i = 0, len = values.length; i < len; ++i) {3635 target.fire("test1", values[i]);3636 }3637 Y.ArrayAssert.itemsAreSame(values, received);3638 received = [];3639 target.on("test2", callback, {});3640 for (i = 0, len = values.length; i < len; ++i) {3641 target.fire("test2", values[i]);3642 }3643 Y.ArrayAssert.itemsAreSame(values, received);3644 received = [];3645 target.on("test3", callback, {}, "x");3646 for (i = 0, len = values.length; i < len; ++i) {3647 target.fire("test3", values[i]);3648 }3649 Y.ArrayAssert.itemsAreSame(values, received);3650 },3651 // TODO: break out facade logic to event-custom-complex.js3652 "test broadcast": function() {3653 var o = new Y.EventTarget(), s1, s2, s3, s4;3654 o.publish('y:foo2', {3655 emitFacade: true,3656 broadcast: 13657 });3658 Y.on('y:foo2', function() {3659 //Y.log('Y foo2 executed');3660 s1 = 1;3661 });3662 Y.Global.on('y:foo2', function() {3663 //Y.log('GLOBAL foo2 executed');3664 s2 = 1;3665 });3666 o.fire('y:foo2');3667 Y.Assert.areEqual(1, s1);3668 Y.Assert.areNotEqual(1, s2);3669 s1 = 0;3670 s2 = 0;3671 o.publish('y:bar', {3672 emitFacade: true,3673 broadcast: 23674 });3675 Y.on('y:bar', function() {3676 //Y.log('Y bar executed');3677 s3 = 1;3678 });3679 Y.Global.on('y:bar', function() {3680 //Y.log('GLOBAL bar executed');3681 s4 = 1;3682 });3683 o.fire('y:bar');3684 Y.Assert.areEqual(1, s3);3685 Y.Assert.areEqual(1, s4);3686 Y.Global.on('y:bar', function(e) {3687 Y.Assert.areEqual(0, e.stopped);3688 // Y.Assert.areEqual(0, e._event.stopped);3689 //Y.log('GLOBAL bar executed');3690 e.stopPropagation();3691 });3692 o.fire('y:bar');3693 o.fire('y:bar');3694 Y.Global.detachAll();3695 }3696 // on/after lifecycle3697 // on/after prevention with return false3698}));3699baseSuite.add(new Y.Test.Case({3700 name: "target.publish()",3701 // broadcast3702 // monitored3703 // context3704 // contextFn3705 // details3706 // fireOnce3707 // async3708 // queuable3709 // silent3710 // type3711 // default configs from ET constructor3712 test_fire_once: function() {3713 var notified = 0,3714 test = this;3715 Y.publish('fireonce', {3716 fireOnce: true3717 });3718 Y.fire('fireonce', 'foo', 'bar');3719 Y.on('fireonce', function(arg1, arg2) {3720 notified++;3721 Y.Assert.areEqual('foo', arg1, 'arg1 not correct for lazy fireOnce listener');3722 Y.Assert.areEqual('bar', arg2, 'arg2 not correct for lazy fireOnce listener');3723 });3724 Y.fire('fireonce', 'foo2', 'bar2');3725 Y.fire('fireonce', 'foo3', 'bar3');3726 test.global_notified = false;3727 Y.on('fireonce', function(arg1, arg2) {3728 //Y.log('the notification is asynchronous, so I need to wait for this test');3729 Y.Assert.areEqual(1, notified, 'listener notified more than once.');3730 test.global_notified = true;3731 });3732 // it is no longer asynchronous3733 // Y.Assert.isFalse(global_notified, 'notification was not asynchronous');3734 },3735 test_async_fireonce: function() {3736 Y.Assert.isTrue(this.global_notified, 'asynchronous notification did not seem to work.');3737 }3738 // node.fire("click") does not fire click subscribers3739}));3740 /*3741 testChain: function() {3742 var fired1 = false,3743 fired2 = false,3744 fired3 = false,3745 fired4 = false,3746 fired5 = false;3747 // should be executed once, after f23748 var f1 = function() {3749 Y.Assert.isTrue(fired2);3750 fired1 = true;3751 };3752 // should be executed once, before f13753 var f2 = function() {3754 Y.Assert.isFalse(fired1);3755 fired2 = true;3756 };3757 // should be executed once, different event from f1 and f23758 var f3 = function() {3759 fired3 = true;3760 };3761 // detached before fired, should not executed3762 var f4 = function() {3763 fired4 = true;3764 };3765 // should fire once, preserving the custom prefix rather3766 // than using the configured event target prefix3767 var f5 = function() {3768 fired5 = true;3769 };3770 // configure chaining via global default or on the event target3771 YUI({ // chain: true3772 base:'../../../build/',3773 logInclude: {3774 test: true3775 }3776 }).use('event-custom', function(Y2) {3777 var o = new Y2.EventTarget({3778 prefix: 'foo',3779 chain : true3780 });3781 // without event target prefix manipulation (incomplete now)3782 // @TODO an error here is throwing an uncaught exception rather than failing the test3783 // Y2.after('p:e', f1).on('p:e', f2).on('p:e2', f3).on('detach, p:e', f4).detach('detach, p:e').fire('p:e').fire('p:e2');3784 // with event target prefix manipulation ('e' is the same event as 'foo:e',3785 // but 'pre:e' is a different event only accessible by using that exact name)3786o.after('e', f1).on('foo:e', f2).on('foo:e2', f3).on('detach, e', f4).detach('detach,e').fire('foo:e').fire('e2').on('pre:e', f5).fire('pre:e');3787 Y.Assert.isTrue(fired1); // verifies chaining, on/after order, and adding the event target prefix3788 Y.Assert.isTrue(fired2); // verifies chaining, on/after order, and accepting the prefix in the event name3789 Y.Assert.isTrue(fired3); // verifies no interaction between events, and prefix manipulation3790 Y.Assert.isFalse(fired4); // verifies detach works (regardless of spaces after comma)3791 Y.Assert.isTrue(fired5); // verifies custom prefix3792 });3793 },3794 */3795Y.Test.Runner.add(baseSuite);...

Full Screen

Full Screen

event-custom-base-deprecated-tests.js

Source:event-custom-base-deprecated-tests.js Github

copy

Full Screen

1YUI.add('event-custom-base-deprecated-tests', function(Y) {2var baseSuite = new Y.Test.Suite("Custom Event Deprecated: Base"),3 keys = Y.Object.keys;4// IMPORTANT - THIS IS WHAT WE ARE TESTING5Y.CustomEvent.keepDeprecatedSubs = true;6baseSuite.add(new Y.Test.Case({7 name: "Event Target constructor",8 "test new Y.EventTarget()": function () {9 var target = new Y.EventTarget();10 Y.Assert.isInstanceOf(Y.EventTarget, target);11 Y.Assert.isObject(target._yuievt);12 },13 "test new Y.EventTarget(config)": function () {14 var target1 = new Y.EventTarget(),15 target2 = new Y.EventTarget({16 broadcast: 2,17 bubbles: false,18 context: target1,19 defaultTargetOnly: true,20 emitFacade: true,21 fireOnce: true,22 monitored: true,23 queuable: true,24 async: true25 }),26 config1 = target1._yuievt,27 config2 = target2._yuievt;28 Y.Assert.isObject(config1.events);29 Y.Assert.isNull(config1.targets);30 Y.Assert.isObject(config1.config);31 Y.Assert.isUndefined(config1.bubbling);32 Y.Assert.isUndefined(config1.config.broadcast);33 Y.Assert.areSame(target1, config2.config.context);34 Y.Assert.isUndefined(config1.config.defaultTargetOnly);35 Y.Assert.isUndefined(config1.config.emitFacade);36 Y.Assert.isUndefined(config1.config.fireOnce);37 Y.Assert.isUndefined(config1.config.monitored);38 Y.Assert.isUndefined(config1.config.queuable);39 Y.Assert.areSame(2, config2.config.broadcast);40 Y.Assert.isFalse(config2.config.bubbles);41 Y.Assert.areSame(target1, config2.config.context);42 Y.Assert.isTrue(config2.config.defaultTargetOnly);43 Y.Assert.isTrue(config2.config.emitFacade);44 Y.Assert.isTrue(config2.config.fireOnce);45 Y.Assert.isTrue(config2.config.monitored);46 Y.Assert.isTrue(config2.config.queuable);47 Y.Assert.isTrue(config2.config.async);48 },49 "test Y.augment(Clz, Y.EventTarget)": function () {50 var instance,51 thisObj;52 function TestClass1() {}53 TestClass1.prototype = {54 method: function() {55 thisObj = this;56 }57 };58 Y.augment(TestClass1, Y.EventTarget);59 instance = new TestClass1();60 Y.Assert.isUndefined(instance._yuievt);61 Y.Assert.isFunction(instance.on);62 Y.Assert.areNotSame(instance.on, Y.EventTarget.prototype.on);63 instance.on("test", instance.method);64 instance.fire("test");65 Y.Assert.isObject(instance._yuievt);66 Y.Assert.isUndefined(instance._yuievt.config.fireOnce);67 Y.Assert.areSame(instance.on, Y.EventTarget.prototype.on);68 Y.Assert.areSame(instance, thisObj);69 function TestClass2() {}70 TestClass2.prototype = {71 method: function () {72 thisObj = this;73 }74 };75 Y.augment(TestClass2, Y.EventTarget, true, null, {76 fireOnce: true77 });78 instance = new TestClass2();79 thisObj = null;80 Y.Assert.isUndefined(instance._yuievt);81 Y.Assert.isFunction(instance.on);82 Y.Assert.areNotSame(instance.on, Y.EventTarget.prototype.on);83 instance.on("test", instance.method);84 instance.fire("test");85 Y.Assert.isObject(instance._yuievt);86 Y.Assert.isTrue(instance._yuievt.config.fireOnce);87 Y.Assert.areSame(instance.on, Y.EventTarget.prototype.on);88 Y.Assert.areSame(instance, thisObj);89 },90 "test Y.extend(Clz, Y.EventTarget)": function () {91 var instance, thisObj;92 function TestClass() {93 TestClass.superclass.constructor.apply(this, arguments);94 }95 Y.extend(TestClass, Y.EventTarget, {96 method: function () {97 thisObj = this;98 }99 });100 instance = new TestClass();101 Y.Assert.isInstanceOf(TestClass, instance);102 Y.Assert.isInstanceOf(Y.EventTarget, instance);103 Y.Assert.isObject(instance._yuievt);104 Y.Assert.areSame(instance.on, Y.EventTarget.prototype.on);105 instance.on("test", instance.method);106 instance.fire("test");107 Y.Assert.areSame(instance, thisObj);108 }109}));110baseSuite.add(new Y.Test.Case({111 name: "target.on()",112 _should: {113 ignore: {114 // As of 3.4.1, creates a subscription to a custom event named115 // "[object Object]"116 "test target.on([{ fn: fn, context: obj }]) does nothing": true,117 // Not (yet) implemented118 "test target.on(type, { handleEvents: fn })": true119 }120 },121 "test auto-publish on subscribe": function () {122 var target = new Y.EventTarget(),123 events = target._yuievt.events,124 publishCalled;125 target.publish = (function (original) {126 return function (type) {127 if (type === 'test') {128 publishCalled = true;129 }130 return original.apply(this, arguments);131 };132 })(target.publish);133 Y.Assert.isUndefined(events.test);134 target.on("test", function () {});135 Y.Assert.isTrue(publishCalled);136 Y.Assert.isObject(events.test);137 Y.Assert.isInstanceOf(Y.CustomEvent, events.test);138 },139 "test target.on(type, fn)": function () {140 var target = new Y.EventTarget(),141 events = target._yuievt.events,142 handle, thisObj, fired, argCount, testEvent;143 function callback() {144 fired = true;145 thisObj = this;146 argCount = arguments.length;147 }148 handle = target.on("test", callback);149 testEvent = events.test;150 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);151 Y.Assert.isObject(testEvent.subscribers);152 Y.Assert.areSame(1, keys(testEvent.subscribers).length);153 Y.Assert.areSame(0, keys(testEvent.afters).length);154 Y.Assert.areSame(1, testEvent.hasSubs());155 Y.Assert.isInstanceOf(Y.EventHandle, handle);156 Y.Assert.areSame(testEvent, handle.evt);157 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);158 target.fire("test");159 Y.Assert.isTrue(fired);160 Y.Assert.areSame(target, thisObj);161 Y.Assert.areSame(0, argCount);162 // Test that fire() did not change the subscription state of the163 // custom event164 testEvent = events.test;165 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);166 Y.Assert.isObject(testEvent.subscribers);167 Y.Assert.areSame(1, keys(testEvent.subscribers).length);168 Y.Assert.areSame(0, keys(testEvent.afters).length);169 Y.Assert.areSame(1, testEvent.hasSubs());170 },171 "test target.on(type, fn) allows duplicate subs": function () {172 var target = new Y.EventTarget(),173 events = target._yuievt.events,174 count = 0,175 testEvent, handle1, handle2;176 function callback() {177 count++;178 }179 handle1 = target.on("test", callback);180 handle2 = target.on("test", callback);181 testEvent = events.test;182 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);183 Y.Assert.isObject(testEvent.subscribers);184 Y.Assert.areSame(2, keys(testEvent.subscribers).length);185 Y.Assert.areSame(0, keys(testEvent.afters).length);186 Y.Assert.areSame(2, testEvent.hasSubs());187 Y.Assert.areNotSame(handle1, handle2);188 Y.Assert.areSame(testEvent, handle1.evt);189 Y.Assert.areSame(handle1.evt, handle2.evt);190 Y.Assert.areNotSame(handle1.sub, handle2.sub);191 target.fire("test");192 Y.Assert.areSame(2, count);193 // Test that fire() did not change the subscription state of the194 // custom event195 testEvent = events.test;196 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);197 Y.Assert.isObject(testEvent.subscribers);198 Y.Assert.areSame(2, keys(testEvent.subscribers).length);199 Y.Assert.areSame(0, keys(testEvent.afters).length);200 Y.Assert.areSame(2, testEvent.hasSubs());201 },202 "test target.on(type, fn, obj)": function () {203 var target = new Y.EventTarget(),204 obj = {},205 count = 0,206 thisObj1, thisObj2, argCount, testEvent;207 function callback() {208 count++;209 thisObj1 = this;210 argCount = arguments.length;211 }212 target.on("test", callback, obj);213 target.fire("test");214 Y.Assert.areSame(1, count);215 Y.Assert.areSame(obj, thisObj1);216 Y.Assert.areSame(0, argCount);217 target.on("test", function () {218 thisObj2 = this;219 });220 target.fire("test");221 Y.Assert.areSame(2, count);222 Y.Assert.areSame(obj, thisObj1);223 Y.Assert.areSame(target, thisObj2);224 Y.Assert.areSame(0, argCount);225 },226 "test target.on(type, fn, obj, args)": function () {227 var target = new Y.EventTarget(),228 obj = {},229 count = 0,230 args = '',231 argCount,232 thisObj1, thisObj2, testEvent;233 function callback() {234 count++;235 thisObj1 = this;236 argCount = arguments.length;237 for (var i = 0, len = argCount; i < len; ++i) {238 args += arguments[i];239 }240 }241 target.on("test", callback, obj, "A");242 target.fire("test");243 Y.Assert.areSame(1, count);244 Y.Assert.areSame(obj, thisObj1);245 Y.Assert.areSame("A", args);246 target.on("test", function () {247 thisObj2 = this;248 });249 target.fire("test");250 Y.Assert.areSame(2, count);251 Y.Assert.areSame(obj, thisObj1);252 Y.Assert.areSame(target, thisObj2);253 Y.Assert.areSame(1, argCount);254 },255 "test target.on([type], fn)": function () {256 var target = new Y.EventTarget(),257 events = target._yuievt.events,258 handle, thisObj, fired, argCount, testEvent;259 function callback() {260 fired = true;261 thisObj = this;262 argCount = arguments.length;263 }264 handle = target.on(["test"], callback);265 testEvent = events.test;266 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);267 Y.Assert.isObject(testEvent.subscribers);268 Y.Assert.areSame(1, keys(testEvent.subscribers).length);269 Y.Assert.areSame(0, keys(testEvent.afters).length);270 Y.Assert.areSame(1, testEvent.hasSubs());271 Y.Assert.isInstanceOf(Y.EventHandle, handle);272 Y.Assert.isArray(handle.evt);273 Y.Assert.areSame(testEvent, handle.evt[0].evt);274 Y.Assert.isUndefined(handle.sub);275 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);276 target.fire("test");277 Y.Assert.isTrue(fired);278 Y.Assert.areSame(target, thisObj);279 Y.Assert.areSame(0, argCount);280 },281 "test target.on([typeA, typeB], fn)": function () {282 var target = new Y.EventTarget(),283 events = target._yuievt.events,284 count = 0,285 handle, thisObj, testEvent1, testEvent2;286 function callback() {287 count++;288 thisObj = this;289 }290 handle = target.on(["test1", "test2"], callback);291 testEvent1 = events.test1;292 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent1);293 Y.Assert.isObject(testEvent1.subscribers);294 Y.Assert.areSame(1, keys(testEvent1.subscribers).length);295 Y.Assert.areSame(0, keys(testEvent1.afters).length);296 Y.Assert.areSame(1, testEvent1.hasSubs());297 testEvent2 = events.test2;298 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent2);299 Y.Assert.isObject(testEvent2.subscribers);300 Y.Assert.areSame(1, keys(testEvent2.subscribers).length);301 Y.Assert.areSame(0, keys(testEvent2.afters).length);302 Y.Assert.areSame(1, testEvent2.hasSubs());303 Y.Assert.isInstanceOf(Y.EventHandle, handle);304 Y.Assert.isArray(handle.evt);305 Y.Assert.areSame(testEvent1, handle.evt[0].evt);306 Y.Assert.areSame(testEvent2, handle.evt[1].evt);307 Y.Assert.areNotSame(testEvent1, testEvent2);308 Y.Assert.isUndefined(handle.sub);309 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);310 target.fire("test1");311 Y.Assert.areSame(1, count);312 Y.Assert.areSame(target, thisObj);313 target.fire("test2");314 Y.Assert.areSame(2, count);315 Y.Assert.areSame(target, thisObj);316 },317 "test target.on([typeA, typeA], fn)": function () {318 var target = new Y.EventTarget(),319 events = target._yuievt.events,320 count = 0,321 handle, thisObj, testEvent;322 function callback() {323 count++;324 thisObj = this;325 }326 handle = target.on(["test", "test"], callback);327 testEvent = events.test;328 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);329 Y.Assert.isObject(testEvent.subscribers);330 Y.Assert.areSame(2, keys(testEvent.subscribers).length);331 Y.Assert.areSame(0, keys(testEvent.afters).length);332 Y.Assert.areSame(2, testEvent.hasSubs());333 Y.Assert.isInstanceOf(Y.EventHandle, handle);334 Y.Assert.isArray(handle.evt);335 Y.Assert.areSame(testEvent, handle.evt[0].evt);336 Y.Assert.areSame(testEvent, handle.evt[1].evt);337 Y.Assert.isUndefined(handle.sub);338 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);339 target.fire("test");340 Y.Assert.areSame(2, count);341 Y.Assert.areSame(target, thisObj);342 },343 "test target.on([], fn) does nothing": function () {344 var target = new Y.EventTarget(),345 events = target._yuievt.events,346 count = 0,347 handle, name, subs, i;348 function callback() {349 Y.Assert.fail("I don't know how this got called");350 }351 handle = target.on([], callback);352 for (name in events) {353 if (events.hasOwnProperty(name)) {354 subs = events[name].subscribers;355 for (i = subs.length - 1; i >= 0; --i) {356 if (subs[i].fn === callback) {357 Y.Assert.fail("subscription registered for '" + name + "' event");358 }359 }360 }361 }362 Y.Assert.isInstanceOf(Y.EventHandle, handle);363 Y.Assert.isArray(handle.evt);364 Y.Assert.areSame(0, handle.evt.length);365 Y.Assert.isUndefined(handle.sub);366 },367 "test target.on([{ fn: fn, context: obj }]) does nothing": function () {368 var target = new Y.EventTarget(),369 events = target._yuievt.events,370 count = 0,371 handle, name, subs, i;372 function callback() {373 Y.Assert.fail("I don't know how this got called");374 }375 handle = target.on([{ fn: callback, context: {} }]);376 for (name in events) {377 if (events.hasOwnProperty(name)) {378 subs = events[name].subscribers;379 for (i = subs.length - 1; i >= 0; --i) {380 if (subs[i].fn === callback) {381 Y.Assert.fail("subscription registered for '" + name + "' event");382 }383 }384 }385 }386 Y.Assert.isInstanceOf(Y.EventHandle, handle);387 Y.Assert.isArray(handle.evt);388 Y.Assert.areSame(0, handle.evt.length);389 Y.Assert.isUndefined(handle.sub);390 },391 "test target.on({ type: fn })": function () {392 var target = new Y.EventTarget(),393 events = target._yuievt.events,394 handle, thisObj, fired, argCount, testEvent;395 function callback() {396 fired = true;397 thisObj = this;398 argCount = arguments.length;399 }400 handle = target.on({ "test1": callback });401 testEvent = events.test1;402 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);403 Y.Assert.isObject(testEvent.subscribers);404 Y.Assert.areSame(1, keys(testEvent.subscribers).length);405 Y.Assert.areSame(0, keys(testEvent.afters).length);406 Y.Assert.areSame(1, testEvent.hasSubs());407 Y.Assert.isInstanceOf(Y.EventHandle, handle);408 Y.Assert.isArray(handle.evt);409 Y.Assert.areSame(1, handle.evt.length);410 Y.Assert.areSame(testEvent, handle.evt[0].evt);411 Y.Assert.isUndefined(handle.sub);412 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);413 target.fire("test1");414 Y.Assert.isTrue(fired);415 Y.Assert.areSame(target, thisObj);416 Y.Assert.areSame(0, argCount);417 handle = target.on({418 "test2": callback,419 "test3": callback420 });421 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);422 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);423 Y.Assert.areSame(1, keys(events.test2.subscribers).length);424 Y.Assert.areSame(1, keys(events.test3.subscribers).length);425 Y.Assert.isInstanceOf(Y.EventHandle, handle);426 Y.Assert.isArray(handle.evt);427 Y.Assert.areSame(2, handle.evt.length);428 Y.Assert.areSame(events.test2, handle.evt[0].evt);429 Y.Assert.areSame(events.test3, handle.evt[1].evt);430 Y.Assert.isUndefined(handle.sub);431 },432 "test target.on({ type: true }, fn)": function () {433 var target = new Y.EventTarget(),434 events = target._yuievt.events,435 handle, thisObj, fired, argCount, testEvent;436 function callback() {437 fired = true;438 thisObj = this;439 argCount = arguments.length;440 }441 handle = target.on({ "test1": true }, callback);442 testEvent = events.test1;443 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);444 Y.Assert.isObject(testEvent.subscribers);445 Y.Assert.areSame(1, keys(testEvent.subscribers).length);446 Y.Assert.areSame(0, keys(testEvent.afters).length);447 Y.Assert.areSame(1, testEvent.hasSubs());448 Y.Assert.isInstanceOf(Y.EventHandle, handle);449 Y.Assert.isArray(handle.evt);450 Y.Assert.areSame(1, handle.evt.length);451 Y.Assert.areSame(testEvent, handle.evt[0].evt);452 Y.Assert.isUndefined(handle.sub);453 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);454 target.fire("test1");455 Y.Assert.isTrue(fired);456 Y.Assert.areSame(target, thisObj);457 Y.Assert.areSame(0, argCount);458 handle = target.on({ "test2": 1, "test3": false }, callback);459 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);460 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);461 Y.Assert.areSame(1, keys(events.test2.subscribers).length);462 Y.Assert.areSame(1, keys(events.test3.subscribers).length);463 Y.Assert.isInstanceOf(Y.EventHandle, handle);464 Y.Assert.isArray(handle.evt);465 Y.Assert.areSame(2, handle.evt.length);466 Y.Assert.areSame(events.test2, handle.evt[0].evt);467 Y.Assert.areSame(events.test3, handle.evt[1].evt);468 Y.Assert.isUndefined(handle.sub);469 },470 "test target.on({ type: { fn: wins } }, fn)": function () {471 var target = new Y.EventTarget(),472 events = target._yuievt.events,473 handle, thisObj, fired, argCount, testEvent;474 function callback() {475 fired = true;476 thisObj = this;477 argCount = arguments.length;478 }479 handle = target.on({ "test1": { fn: callback } }, function () {480 Y.Assert.fail("This callback should not have been called.");481 });482 testEvent = events.test1;483 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);484 Y.Assert.isObject(testEvent.subscribers);485 Y.Assert.areSame(1, keys(testEvent.subscribers).length);486 Y.Assert.areSame(0, keys(testEvent.afters).length);487 Y.Assert.areSame(1, testEvent.hasSubs());488 Y.Assert.isInstanceOf(Y.EventHandle, handle);489 Y.Assert.isArray(handle.evt);490 Y.Assert.areSame(1, handle.evt.length);491 Y.Assert.areSame(testEvent, handle.evt[0].evt);492 Y.Assert.isUndefined(handle.sub);493 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);494 target.fire("test1");495 Y.Assert.isTrue(fired);496 Y.Assert.areSame(target, thisObj);497 Y.Assert.areSame(0, argCount);498 },499 "test target.on({ type: { fn: wins } }, fn, obj, args)": function () {500 var target = new Y.EventTarget(),501 obj = {},502 events = target._yuievt.events,503 handle, thisObj, fired, argCount, testEvent;504 function callback() {505 fired = true;506 thisObj = this;507 argCount = arguments.length;508 }509 handle = target.on({ "test1": { fn: callback } }, function () {510 Y.Assert.fail("This callback should not have been called.");511 }, obj, 'ARG!');512 testEvent = events.test1;513 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);514 Y.Assert.isObject(testEvent.subscribers);515 Y.Assert.areSame(1, keys(testEvent.subscribers).length);516 Y.Assert.areSame(0, keys(testEvent.afters).length);517 Y.Assert.areSame(1, testEvent.hasSubs());518 Y.Assert.isInstanceOf(Y.EventHandle, handle);519 Y.Assert.isArray(handle.evt);520 Y.Assert.areSame(1, handle.evt.length);521 Y.Assert.areSame(testEvent, handle.evt[0].evt);522 Y.Assert.isUndefined(handle.sub);523 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);524 target.fire("test1");525 Y.Assert.isTrue(fired);526 Y.Assert.areSame(obj, thisObj);527 Y.Assert.areSame(1, argCount);528 },529 "test target.on({ type: { fn: wins, context: wins } }, fn, ctx, args)": function () {530 var target = new Y.EventTarget(),531 obj = {},532 events = target._yuievt.events,533 handle, thisObj, fired, argCount, testEvent;534 function callback() {535 fired = true;536 thisObj = this;537 argCount = arguments.length;538 }539 handle = target.on({ "test1": { fn: callback, context: obj } },540 function () {541 Y.Assert.fail("This callback should not have been called.");542 }, {}, 'ARG!');543 testEvent = events.test1;544 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);545 Y.Assert.isObject(testEvent.subscribers);546 Y.Assert.areSame(1, keys(testEvent.subscribers).length);547 Y.Assert.areSame(0, keys(testEvent.afters).length);548 Y.Assert.areSame(1, testEvent.hasSubs());549 Y.Assert.isInstanceOf(Y.EventHandle, handle);550 Y.Assert.isArray(handle.evt);551 Y.Assert.areSame(1, handle.evt.length);552 Y.Assert.areSame(testEvent, handle.evt[0].evt);553 Y.Assert.isUndefined(handle.sub);554 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);555 target.fire("test1");556 Y.Assert.isTrue(fired);557 Y.Assert.areSame(obj, thisObj);558 Y.Assert.areSame(1, argCount);559 },560 "test target.on({ type: { context: wins } }, callback, ctx, args)": function () {561 var target = new Y.EventTarget(),562 obj = {},563 events = target._yuievt.events,564 handle, thisObj, fired, argCount, testEvent;565 function callback() {566 fired = true;567 thisObj = this;568 argCount = arguments.length;569 }570 handle = target.on({ "test1": { context: obj } }, callback, {}, 'ARG!');571 testEvent = events.test1;572 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);573 Y.Assert.isObject(testEvent.subscribers);574 Y.Assert.areSame(1, keys(testEvent.subscribers).length);575 Y.Assert.areSame(0, keys(testEvent.afters).length);576 Y.Assert.areSame(1, testEvent.hasSubs());577 Y.Assert.isInstanceOf(Y.EventHandle, handle);578 Y.Assert.isArray(handle.evt);579 Y.Assert.areSame(1, handle.evt.length);580 Y.Assert.areSame(testEvent, handle.evt[0].evt);581 Y.Assert.isUndefined(handle.sub);582 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);583 target.fire("test1");584 Y.Assert.isTrue(fired);585 Y.Assert.areSame(obj, thisObj);586 Y.Assert.areSame(1, argCount);587 },588 "test target.on(type, { handleEvents: fn })": function () {589 var target = new Y.EventTarget(),590 events = target._yuievt.events,591 obj, handle, thisObj, fired, argCount, testEvent;592 function callback() {593 fired = true;594 thisObj = this;595 argCount = arguments.length;596 }597 obj = { handleEvents: callback };598 handle = target.on("test", obj);599 testEvent = events.test;600 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);601 Y.Assert.isObject(testEvent.subscribers);602 Y.Assert.areSame(1, keys(testEvent.subscribers).length);603 Y.Assert.areSame(0, keys(testEvent.afters).length);604 Y.Assert.areSame(1, testEvent.hasSubs());605 Y.Assert.isInstanceOf(Y.EventHandle, handle);606 Y.Assert.areSame(testEvent, handle.evt);607 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);608 // Barring support, this is where the error will be thrown.609 // ET.on() doesn't verify the second arg is a function, and610 // Subscriber doesn't type check before treating it as a function.611 target.fire("test");612 Y.Assert.isTrue(fired);613 Y.Assert.areSame(obj, thisObj);614 Y.Assert.areSame(0, argCount);615 // Test that fire() did not change the subscription state of the616 // custom event617 testEvent = events.test;618 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);619 Y.Assert.isObject(testEvent.subscribers);620 Y.Assert.areSame(1, keys(testEvent.subscribers).length);621 Y.Assert.areSame(0, keys(testEvent.afters).length);622 Y.Assert.areSame(1, testEvent.hasSubs());623 },624 "test callback context": function () {625 var target = new Y.EventTarget(),626 targetCount = 0,627 objCount = 0,628 obj = {};629 function isTarget() {630 Y.Assert.areSame(target, this);631 targetCount++;632 }633 function isObj() {634 Y.Assert.areSame(obj, this);635 objCount++;636 }637 target.on("test1", isTarget);638 target.fire("test1"); // targetCount 1639 target.on("test2", isObj, obj);640 target.fire("test2"); // objCount 1641 target.on("test3", isObj, obj, {});642 target.fire("test3"); // objCount 2643 target.on("test4", isObj, obj, null, {}, {});644 target.fire("test4"); // objCount 3645 target.on("test5", isTarget, null, {});646 target.fire("test5"); // targetCount 2647 target.on("prefix:test6", isTarget);648 target.fire("prefix:test6", obj); // targetCount 3649 target.on(["test7", "prefix:test8"], isObj, obj);650 target.fire("test7"); // objCount 4651 target.fire("prefix:test8"); // objCount 5652 target.on({ "test9": isObj }, null, obj);653 target.fire("test9"); // objCount 6654 target.on({655 "test10": { fn: isTarget },656 "test11": { fn: isObj, context: obj }657 });658 target.fire("test10"); // targetCount 4659 target.fire("test11"); // objCount 7660 target.on({661 "test12": { fn: isObj },662 "prefix:test13": { fn: isTarget, context: target }663 }, null, obj);664 target.fire("test12"); // objCount 8665 target.fire("prefix:test13"); // targetCount 5666 Y.Assert.areSame(5, targetCount);667 Y.Assert.areSame(8, objCount);668 },669 "test subscription bound args": function () {670 var target = new Y.EventTarget(),671 obj = {},672 args;673 function callback() {674 args = Y.Array(arguments, 0, true);675 }676 target.on("test1", callback, {}, "a", 1, obj, null);677 target.fire("test1");678 Y.ArrayAssert.itemsAreSame(["a", 1, obj, null], args);679 target.on(["test2", "test3"], callback, null, "a", 2.3, obj, null);680 target.fire("test2");681 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);682 args = [];683 target.fire("test3");684 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);685 // ugh, requiring two placeholders for (unused) fn and context is ooogly686 target.on({687 "test4": callback,688 "test5": callback689 }, null, null, "a", 4.5, obj, null);690 target.fire("test4");691 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);692 args = [];693 target.fire("test5");694 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);695 target.on({696 "test6": true,697 "test7": false698 }, callback, {}, "a", 6.7, obj, null);699 target.fire("test6");700 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);701 args = [];702 target.fire("test7");703 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);704 },705 "test target.on('click', fn) registers custom event only": function () {706 var target = new Y.EventTarget(),707 events = target._yuievt.events;708 target.on("click", function () {});709 Y.Assert.isInstanceOf(Y.CustomEvent, events.click);710 Y.Assert.isUndefined(events.click.domkey);711 Y.Assert.areSame(1, keys(events.click.subscribers).length);712 }713}));714baseSuite.add(new Y.Test.Case({715 name: "target.after",716 _should: {717 ignore: {718 // As of 3.4.1, creates a subscription to a custom event named719 // "[object Object]"720 "test target.after([{ fn: fn, context: obj }]) does nothing": true,721 // Not (yet) implemented722 "test target.after(type, { handleEvents: fn })": true723 }724 },725 "test auto-publish on subscribe": function () {726 var target = new Y.EventTarget(),727 events = target._yuievt.events,728 publishCalled;729 target.publish = (function (original) {730 return function (type) {731 if (type === 'test') {732 publishCalled = true;733 }734 return original.apply(this, arguments);735 };736 })(target.publish);737 Y.Assert.isUndefined(events.test);738 target.after("test", function () {});739 Y.Assert.isTrue(publishCalled);740 Y.Assert.isObject(events.test);741 Y.Assert.isInstanceOf(Y.CustomEvent, events.test);742 },743 "test target.after(type, fn)": function () {744 var target = new Y.EventTarget(),745 events = target._yuievt.events,746 handle, thisObj, fired, argCount, testEvent;747 function callback() {748 fired = true;749 thisObj = this;750 argCount = arguments.length;751 }752 handle = target.after("test", callback);753 testEvent = events.test;754 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);755 Y.Assert.isObject(testEvent.afters);756 Y.Assert.areSame(0, keys(testEvent.subscribers).length);757 Y.Assert.areSame(1, keys(testEvent.afters).length);758 Y.Assert.areSame(1, testEvent.hasSubs());759 Y.Assert.isInstanceOf(Y.EventHandle, handle);760 Y.Assert.areSame(testEvent, handle.evt);761 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);762 target.fire("test");763 Y.Assert.isTrue(fired);764 Y.Assert.areSame(target, thisObj);765 Y.Assert.areSame(0, argCount);766 // Test that fire() did not change the subscription state of the767 // custom event768 testEvent = events.test;769 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);770 Y.Assert.isObject(testEvent.afters);771 Y.Assert.areSame(0, keys(testEvent.subscribers).length);772 Y.Assert.areSame(1, keys(testEvent.afters).length);773 Y.Assert.areSame(1, testEvent.hasSubs());774 },775 "test target.after(type, fn) allows duplicate subs": function () {776 var target = new Y.EventTarget(),777 events = target._yuievt.events,778 count = 0,779 testEvent, handle1, handle2;780 function callback() {781 count++;782 }783 handle1 = target.after("test", callback);784 handle2 = target.after("test", callback);785 testEvent = events.test;786 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);787 Y.Assert.isObject(testEvent.afters);788 Y.Assert.areSame(0, keys(testEvent.subscribers).length);789 Y.Assert.areSame(2, keys(testEvent.afters).length);790 Y.Assert.areSame(2, testEvent.hasSubs());791 Y.Assert.areNotSame(handle1, handle2);792 Y.Assert.areSame(testEvent, handle1.evt);793 Y.Assert.areSame(handle1.evt, handle2.evt);794 Y.Assert.areNotSame(handle1.sub, handle2.sub);795 target.fire("test");796 Y.Assert.areSame(2, count);797 // Test that fire() did not change the subscription state of the798 // custom event799 testEvent = events.test;800 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);801 Y.Assert.isObject(testEvent.afters);802 Y.Assert.areSame(0, keys(testEvent.subscribers).length);803 Y.Assert.areSame(2, keys(testEvent.afters).length);804 Y.Assert.areSame(2, testEvent.hasSubs());805 },806 "test target.after(type, fn, obj)": function () {807 var target = new Y.EventTarget(),808 obj = {},809 count = 0,810 thisObj1, thisObj2, argCount, testEvent;811 function callback() {812 count++;813 thisObj1 = this;814 argCount = arguments.length;815 }816 target.after("test", callback, obj);817 target.fire("test");818 Y.Assert.areSame(1, count);819 Y.Assert.areSame(obj, thisObj1);820 Y.Assert.areSame(0, argCount);821 target.after("test", function () {822 thisObj2 = this;823 });824 target.fire("test");825 Y.Assert.areSame(2, count);826 Y.Assert.areSame(obj, thisObj1);827 Y.Assert.areSame(target, thisObj2);828 Y.Assert.areSame(0, argCount);829 },830 "test target.after(type, fn, obj, args)": function () {831 var target = new Y.EventTarget(),832 obj = {},833 count = 0,834 args = '',835 argCount,836 thisObj1, thisObj2, testEvent;837 function callback() {838 count++;839 thisObj1 = this;840 argCount = arguments.length;841 for (var i = 0, len = argCount; i < len; ++i) {842 args += arguments[i];843 }844 }845 target.after("test", callback, obj, "A");846 target.fire("test");847 Y.Assert.areSame(1, count);848 Y.Assert.areSame(obj, thisObj1);849 Y.Assert.areSame(1, argCount);850 Y.Assert.areSame("A", args);851 target.after("test", function () {852 thisObj2 = this;853 });854 target.fire("test");855 Y.Assert.areSame(2, count);856 Y.Assert.areSame(obj, thisObj1);857 Y.Assert.areSame(target, thisObj2);858 },859 "test target.after([type], fn)": function () {860 var target = new Y.EventTarget(),861 events = target._yuievt.events,862 handle, thisObj, fired, argCount, testEvent;863 function callback() {864 fired = true;865 thisObj = this;866 argCount = arguments.length;867 }868 handle = target.after(["test"], callback);869 testEvent = events.test;870 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);871 Y.Assert.isObject(testEvent.afters);872 Y.Assert.areSame(0, keys(testEvent.subscribers).length);873 Y.Assert.areSame(1, keys(testEvent.afters).length);874 Y.Assert.areSame(1, testEvent.hasSubs());875 Y.Assert.isInstanceOf(Y.EventHandle, handle);876 Y.Assert.isArray(handle.evt);877 Y.Assert.areSame(testEvent, handle.evt[0].evt);878 Y.Assert.isUndefined(handle.sub);879 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);880 target.fire("test");881 Y.Assert.isTrue(fired);882 Y.Assert.areSame(target, thisObj);883 Y.Assert.areSame(0, argCount);884 },885 "test target.after([typeA, typeB], fn)": function () {886 var target = new Y.EventTarget(),887 events = target._yuievt.events,888 count = 0,889 handle, thisObj, testEvent1, testEvent2;890 function callback() {891 count++;892 thisObj = this;893 }894 handle = target.after(["test1", "test2"], callback);895 testEvent1 = events.test1;896 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent1);897 Y.Assert.isObject(testEvent1.afters);898 Y.Assert.areSame(0, keys(testEvent1.subscribers).length);899 Y.Assert.areSame(1, keys(testEvent1.afters).length);900 Y.Assert.areSame(1, testEvent1.hasSubs());901 testEvent2 = events.test2;902 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent2);903 Y.Assert.isObject(testEvent2.afters);904 Y.Assert.areSame(0, keys(testEvent2.subscribers).length);905 Y.Assert.areSame(1, keys(testEvent2.afters).length);906 Y.Assert.areSame(1, testEvent2.hasSubs());907 Y.Assert.isInstanceOf(Y.EventHandle, handle);908 Y.Assert.isArray(handle.evt);909 Y.Assert.areSame(testEvent1, handle.evt[0].evt);910 Y.Assert.areSame(testEvent2, handle.evt[1].evt);911 Y.Assert.areNotSame(testEvent1, testEvent2);912 Y.Assert.isUndefined(handle.sub);913 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);914 target.fire("test1");915 Y.Assert.areSame(1, count);916 Y.Assert.areSame(target, thisObj);917 target.fire("test2");918 Y.Assert.areSame(2, count);919 Y.Assert.areSame(target, thisObj);920 },921 "test target.after([typeA, typeA], fn)": function () {922 var target = new Y.EventTarget(),923 events = target._yuievt.events,924 count = 0,925 handle, thisObj, testEvent;926 function callback() {927 count++;928 thisObj = this;929 }930 handle = target.after(["test", "test"], callback);931 testEvent = events.test;932 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);933 Y.Assert.isObject(testEvent.afters);934 Y.Assert.areSame(0, keys(testEvent.subscribers).length);935 Y.Assert.areSame(2, keys(testEvent.afters).length);936 Y.Assert.areSame(2, testEvent.hasSubs());937 Y.Assert.isInstanceOf(Y.EventHandle, handle);938 Y.Assert.isArray(handle.evt);939 Y.Assert.areSame(testEvent, handle.evt[0].evt);940 Y.Assert.areSame(testEvent, handle.evt[1].evt);941 Y.Assert.isUndefined(handle.sub);942 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);943 target.fire("test");944 Y.Assert.areSame(2, count);945 Y.Assert.areSame(target, thisObj);946 },947 "test target.after([], fn) does nothing": function () {948 var target = new Y.EventTarget(),949 events = target._yuievt.events,950 count = 0,951 handle, name, subs, i;952 function callback() {953 Y.Assert.fail("I don't know how this got called");954 }955 handle = target.after([], callback);956 for (name in events) {957 if (events.hasOwnProperty(name)) {958 subs = events[name].afters;959 for (i = subs.length - 1; i >= 0; --i) {960 if (subs[i].fn === callback) {961 Y.Assert.fail("subscription registered for '" + name + "' event");962 }963 }964 }965 }966 Y.Assert.isInstanceOf(Y.EventHandle, handle);967 Y.Assert.isArray(handle.evt);968 Y.Assert.areSame(0, handle.evt.length);969 Y.Assert.isUndefined(handle.sub);970 },971 "test target.after([{ fn: fn, context: obj }]) does nothing": function () {972 var target = new Y.EventTarget(),973 events = target._yuievt.events,974 count = 0,975 handle, name, subs, i;976 function callback() {977 Y.Assert.fail("I don't know how this got called");978 }979 handle = target.after([{ fn: callback, context: {} }]);980 for (name in events) {981 if (events.hasOwnProperty(name)) {982 subs = events[name].afters;983 for (i = subs.length - 1; i >= 0; --i) {984 if (subs[i].fn === callback) {985 Y.Assert.fail("subscription registered for '" + name + "' event");986 }987 }988 }989 }990 Y.Assert.isInstanceOf(Y.EventHandle, handle);991 Y.Assert.isArray(handle.evt);992 Y.Assert.areSame(0, handle.evt.length);993 Y.Assert.isUndefined(handle.sub);994 },995 "test target.after({ type: fn })": function () {996 var target = new Y.EventTarget(),997 events = target._yuievt.events,998 handle, thisObj, fired, argCount, testEvent;999 function callback() {1000 fired = true;1001 thisObj = this;1002 argCount = arguments.length;1003 }1004 handle = target.after({ "test1": callback });1005 testEvent = events.test1;1006 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1007 Y.Assert.isObject(testEvent.afters);1008 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1009 Y.Assert.areSame(1, keys(testEvent.afters).length);1010 Y.Assert.areSame(1, testEvent.hasSubs());1011 Y.Assert.isInstanceOf(Y.EventHandle, handle);1012 Y.Assert.isArray(handle.evt);1013 Y.Assert.areSame(1, handle.evt.length);1014 Y.Assert.areSame(testEvent, handle.evt[0].evt);1015 Y.Assert.isUndefined(handle.sub);1016 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1017 target.fire("test1");1018 Y.Assert.isTrue(fired);1019 Y.Assert.areSame(target, thisObj);1020 Y.Assert.areSame(0, argCount);1021 handle = target.after({1022 "test2": callback,1023 "test3": callback1024 });1025 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);1026 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);1027 Y.Assert.areSame(1, keys(events.test2.afters).length);1028 Y.Assert.areSame(1, keys(events.test3.afters).length);1029 Y.Assert.isInstanceOf(Y.EventHandle, handle);1030 Y.Assert.isArray(handle.evt);1031 Y.Assert.areSame(2, handle.evt.length);1032 Y.Assert.areSame(events.test2, handle.evt[0].evt);1033 Y.Assert.areSame(events.test3, handle.evt[1].evt);1034 Y.Assert.isUndefined(handle.sub);1035 },1036 "test target.after({ type: true }, fn)": function () {1037 var target = new Y.EventTarget(),1038 events = target._yuievt.events,1039 handle, thisObj, fired, argCount, testEvent;1040 function callback() {1041 fired = true;1042 thisObj = this;1043 argCount = arguments.length;1044 }1045 handle = target.after({ "test1": true }, callback);1046 testEvent = events.test1;1047 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1048 Y.Assert.isObject(testEvent.afters);1049 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1050 Y.Assert.areSame(1, keys(testEvent.afters).length);1051 Y.Assert.areSame(1, testEvent.hasSubs());1052 Y.Assert.isInstanceOf(Y.EventHandle, handle);1053 Y.Assert.isArray(handle.evt);1054 Y.Assert.areSame(1, handle.evt.length);1055 Y.Assert.areSame(testEvent, handle.evt[0].evt);1056 Y.Assert.isUndefined(handle.sub);1057 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1058 target.fire("test1");1059 Y.Assert.isTrue(fired);1060 Y.Assert.areSame(target, thisObj);1061 Y.Assert.areSame(0, argCount);1062 handle = target.after({ "test2": 1, "test3": false }, callback);1063 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);1064 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);1065 Y.Assert.areSame(1, keys(events.test2.afters).length);1066 Y.Assert.areSame(1, keys(events.test3.afters).length);1067 Y.Assert.isInstanceOf(Y.EventHandle, handle);1068 Y.Assert.isArray(handle.evt);1069 Y.Assert.areSame(2, handle.evt.length);1070 Y.Assert.areSame(events.test2, handle.evt[0].evt);1071 Y.Assert.areSame(events.test3, handle.evt[1].evt);1072 Y.Assert.isUndefined(handle.sub);1073 },1074 "test target.after(type, { handleEvents: fn })": function () {1075 var target = new Y.EventTarget(),1076 events = target._yuievt.events,1077 obj, handle, thisObj, fired, argCount, testEvent;1078 function callback() {1079 fired = true;1080 thisObj = this;1081 argCount = arguments.length;1082 }1083 obj = { handleEvents: callback };1084 handle = target.after("test", obj);1085 testEvent = events.test;1086 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1087 Y.Assert.isObject(testEvent.afters);1088 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1089 Y.Assert.areSame(1, keys(testEvent.afters).length);1090 Y.Assert.areSame(1, testEvent.hasSubs());1091 Y.Assert.isInstanceOf(Y.EventHandle, handle);1092 Y.Assert.areSame(testEvent, handle.evt);1093 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);1094 // Barring support, this is where the error will be thrown.1095 // ET.after() doesn't verify the second arg is a function, and1096 // Subscriber doesn't type check before treating it as a function.1097 target.fire("test");1098 Y.Assert.isTrue(fired);1099 Y.Assert.areSame(obj, thisObj);1100 Y.Assert.areSame(0, argCount);1101 // Test that fire() did not change the subscription state of the1102 // custom event1103 testEvent = events.test;1104 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1105 Y.Assert.isObject(testEvent.afters);1106 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1107 Y.Assert.areSame(1, keys(testEvent.afters).length);1108 Y.Assert.areSame(1, testEvent.hasSubs());1109 },1110 "test callback context": function () {1111 var target = new Y.EventTarget(),1112 targetCount = 0,1113 objCount = 0,1114 obj = {};1115 function isTarget() {1116 Y.Assert.areSame(target, this);1117 targetCount++;1118 }1119 function isObj() {1120 Y.Assert.areSame(obj, this);1121 objCount++;1122 }1123 target.after("test1", isTarget);1124 target.fire("test1");1125 target.after("test2", isObj, obj);1126 target.fire("test2");1127 target.after("test3", isObj, obj, {});1128 target.fire("test3");1129 target.after("test4", isObj, obj, null, {}, {});1130 target.fire("test4");1131 target.after("test5", isTarget, null, {});1132 target.fire("test5");1133 target.after("prefix:test6", isTarget);1134 target.fire("prefix:test6", obj);1135 target.after(["test7", "prefix:test8"], isObj, obj);1136 target.fire("test7");1137 target.fire("prefix:test8");1138 target.after({ "test9": isObj }, null, obj);1139 target.fire("test9");1140 target.after({1141 "test10": { fn: isTarget },1142 "test11": { fn: isObj, context: obj }1143 });1144 target.fire("test10");1145 target.fire("test11");1146 target.after({1147 "test12": { fn: isObj },1148 "prefix:test13": { fn: isTarget, context: target }1149 }, null, obj);1150 target.fire("test12");1151 target.fire("prefix:test13");1152 Y.Assert.areSame(5, targetCount);1153 Y.Assert.areSame(8, objCount);1154 },1155 "test subscription bound args": function () {1156 var target = new Y.EventTarget(),1157 obj = {},1158 args;1159 function callback() {1160 args = Y.Array(arguments, 0, true);1161 }1162 target.after("test1", callback, {}, "a", 1, obj, null);1163 target.fire("test1");1164 Y.ArrayAssert.itemsAreSame(["a", 1, obj, null], args);1165 target.after(["test2", "test3"], callback, null, "a", 2.3, obj, null);1166 target.fire("test2");1167 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);1168 args = [];1169 target.fire("test3");1170 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);1171 // ugh, requiring two placeholders for (unused) fn and context is ooogly1172 target.after({1173 "test4": callback,1174 "test5": callback1175 }, null, null, "a", 4.5, obj, null);1176 target.fire("test4");1177 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);1178 args = [];1179 target.fire("test5");1180 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);1181 target.after({1182 "test6": true,1183 "test7": false1184 }, callback, {}, "a", 6.7, obj, null);1185 target.fire("test6");1186 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);1187 args = [];1188 target.fire("test7");1189 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);1190 },1191 "test target.after('click', fn) registers custom event only": function () {1192 var target = new Y.EventTarget(),1193 events = target._yuievt.events;1194 target.after("click", function () {});1195 Y.Assert.isInstanceOf(Y.CustomEvent, events.click);1196 Y.Assert.isUndefined(events.click.domkey);1197 Y.Assert.areSame(1, keys(events.click.afters).length);1198 }1199}));1200baseSuite.add(new Y.Test.Case({1201 name: "target.once",1202 _should: {1203 ignore: {1204 // As of 3.4.1, creates a subscription to a custom event named1205 // "[object Object]"1206 "test target.once([{ fn: fn, context: obj }]) does nothing": true,1207 // Not (yet) implemented1208 "test target.once(type, { handleEvents: fn })": true1209 }1210 },1211 "test auto-publish on subscribe": function () {1212 var target = new Y.EventTarget(),1213 events = target._yuievt.events,1214 publishCalled;1215 target.publish = (function (original) {1216 return function (type) {1217 if (type === 'test') {1218 publishCalled = true;1219 }1220 return original.apply(this, arguments);1221 };1222 })(target.publish);1223 Y.Assert.isUndefined(events.test);1224 target.once("test", function () {});1225 Y.Assert.isTrue(publishCalled);1226 Y.Assert.isObject(events.test);1227 Y.Assert.isInstanceOf(Y.CustomEvent, events.test);1228 },1229 "test target.once(type, fn)": function () {1230 var target = new Y.EventTarget(),1231 events = target._yuievt.events,1232 handle, thisObj, fired, argCount, testEvent;1233 function callback() {1234 fired = true;1235 thisObj = this;1236 argCount = arguments.length;1237 }1238 handle = target.once("test", callback);1239 testEvent = events.test;1240 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1241 Y.Assert.isObject(testEvent.subscribers);1242 Y.Assert.areSame(1, keys(testEvent.subscribers).length);1243 Y.Assert.areSame(0, keys(testEvent.afters).length);1244 Y.Assert.areSame(1, testEvent.hasSubs());1245 Y.Assert.isInstanceOf(Y.EventHandle, handle);1246 Y.Assert.areSame(testEvent, handle.evt);1247 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);1248 target.fire("test");1249 Y.Assert.isTrue(fired);1250 Y.Assert.areSame(target, thisObj);1251 Y.Assert.areSame(0, argCount);1252 // Test that fire() resulted in immediate detach of once() sub1253 testEvent = events.test;1254 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1255 Y.Assert.isObject(testEvent.subscribers);1256 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1257 Y.Assert.areSame(0, keys(testEvent.afters).length);1258 Y.Assert.areSame(0, testEvent.hasSubs());1259 },1260 "test target.once(type, fn) allows duplicate subs": function () {1261 var target = new Y.EventTarget(),1262 events = target._yuievt.events,1263 count = 0,1264 testEvent, handle1, handle2;1265 function callback() {1266 count++;1267 }1268 handle1 = target.once("test", callback);1269 handle2 = target.once("test", callback);1270 testEvent = events.test;1271 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1272 Y.Assert.isObject(testEvent.subscribers);1273 Y.Assert.areSame(2, keys(testEvent.subscribers).length);1274 Y.Assert.areSame(0, keys(testEvent.afters).length);1275 Y.Assert.areSame(2, testEvent.hasSubs());1276 Y.Assert.areNotSame(handle1, handle2);1277 Y.Assert.areSame(testEvent, handle1.evt);1278 Y.Assert.areSame(handle1.evt, handle2.evt);1279 Y.Assert.areNotSame(handle1.sub, handle2.sub);1280 target.fire("test");1281 Y.Assert.areSame(2, count);1282 // Test that fire() resulted in immediate detach of once() sub1283 testEvent = events.test;1284 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1285 Y.Assert.isObject(testEvent.subscribers);1286 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1287 Y.Assert.areSame(0, keys(testEvent.afters).length);1288 Y.Assert.areSame(0, testEvent.hasSubs());1289 target.fire("test");1290 Y.Assert.areSame(2, count);1291 },1292 "test target.once(type, fn, obj)": function () {1293 var target = new Y.EventTarget(),1294 obj = {},1295 count = 0,1296 thisObj1, thisObj2, argCount, testEvent;1297 function callback() {1298 count++;1299 thisObj1 = this;1300 argCount = arguments.length;1301 }1302 target.once("test", callback, obj);1303 target.fire("test");1304 Y.Assert.areSame(1, count);1305 Y.Assert.areSame(obj, thisObj1);1306 Y.Assert.areSame(0, argCount);1307 // Subscriber should be detached, so count should not increment1308 target.fire("test");1309 Y.Assert.areSame(1, count);1310 target.once("test", function () {1311 thisObj2 = this;1312 });1313 target.fire("test");1314 Y.Assert.areSame(1, count);1315 Y.Assert.areSame(obj, thisObj1);1316 Y.Assert.areSame(target, thisObj2);1317 // Subscriber should be detached, so count should not increment1318 target.fire("test");1319 Y.Assert.areSame(1, count);1320 },1321 "test target.once(type, fn, obj, args)": function () {1322 var target = new Y.EventTarget(),1323 obj = {},1324 count = 0,1325 args = '',1326 argCount,1327 thisObj1, thisObj2, testEvent;1328 function callback() {1329 count++;1330 thisObj1 = this;1331 argCount = arguments.length;1332 for (var i = 0, len = argCount; i < len; ++i) {1333 args += arguments[i];1334 }1335 }1336 target.once("test", callback, obj, "A");1337 target.fire("test");1338 Y.Assert.areSame(1, count);1339 Y.Assert.areSame(obj, thisObj1);1340 Y.Assert.areSame("A", args);1341 // Subscriber should be detached, so count should not increment1342 target.fire("test");1343 Y.Assert.areSame(1, count);1344 target.once("test", function () {1345 thisObj2 = this;1346 });1347 target.fire("test");1348 Y.Assert.areSame(1, count);1349 Y.Assert.areSame(obj, thisObj1);1350 Y.Assert.areSame(target, thisObj2);1351 // Subscriber should be detached, so count should not increment1352 target.fire("test");1353 Y.Assert.areSame(1, count);1354 },1355 "test target.once([type], fn)": function () {1356 var target = new Y.EventTarget(),1357 events = target._yuievt.events,1358 handle, thisObj, fired, argCount, testEvent;1359 function callback() {1360 fired = true;1361 thisObj = this;1362 argCount = arguments.length;1363 }1364 handle = target.once(["test"], callback);1365 testEvent = events.test;1366 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1367 Y.Assert.isObject(testEvent.subscribers);1368 Y.Assert.areSame(1, keys(testEvent.subscribers).length);1369 Y.Assert.areSame(0, keys(testEvent.afters).length);1370 Y.Assert.areSame(1, testEvent.hasSubs());1371 Y.Assert.isInstanceOf(Y.EventHandle, handle);1372 Y.Assert.isArray(handle.evt);1373 Y.Assert.areSame(testEvent, handle.evt[0].evt);1374 Y.Assert.isUndefined(handle.sub);1375 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1376 target.fire("test");1377 Y.Assert.isTrue(fired);1378 Y.Assert.areSame(target, thisObj);1379 Y.Assert.areSame(0, argCount);1380 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1381 fired = false;1382 target.fire("test");1383 Y.Assert.isFalse(fired);1384 },1385 "test target.once([typeA, typeB], fn)": function () {1386 var target = new Y.EventTarget(),1387 events = target._yuievt.events,1388 count = 0,1389 handle, thisObj, testEvent1, testEvent2;1390 function callback() {1391 count++;1392 thisObj = this;1393 }1394 handle = target.once(["test1", "test2"], callback);1395 testEvent1 = events.test1;1396 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent1);1397 Y.Assert.isObject(testEvent1.subscribers);1398 Y.Assert.areSame(1, keys(testEvent1.subscribers).length);1399 Y.Assert.areSame(0, keys(testEvent1.afters).length);1400 Y.Assert.areSame(1, testEvent1.hasSubs());1401 testEvent2 = events.test2;1402 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent2);1403 Y.Assert.isObject(testEvent2.subscribers);1404 Y.Assert.areSame(1, keys(testEvent2.subscribers).length);1405 Y.Assert.areSame(0, keys(testEvent2.afters).length);1406 Y.Assert.areSame(1, testEvent2.hasSubs());1407 Y.Assert.isInstanceOf(Y.EventHandle, handle);1408 Y.Assert.isArray(handle.evt);1409 Y.Assert.areSame(testEvent1, handle.evt[0].evt);1410 Y.Assert.areSame(testEvent2, handle.evt[1].evt);1411 Y.Assert.areNotSame(testEvent1, testEvent2);1412 Y.Assert.isUndefined(handle.sub);1413 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1414 target.fire("test1");1415 Y.Assert.areSame(1, count);1416 Y.Assert.areSame(target, thisObj);1417 Y.Assert.areSame(0, keys(testEvent1.subscribers).length);1418 target.fire("test2");1419 Y.Assert.areSame(2, count);1420 Y.Assert.areSame(target, thisObj);1421 Y.Assert.areSame(0, keys(testEvent2.subscribers).length);1422 },1423 "test target.once([typeA, typeA], fn)": function () {1424 var target = new Y.EventTarget(),1425 events = target._yuievt.events,1426 count = 0,1427 handle, thisObj, testEvent;1428 function callback() {1429 count++;1430 thisObj = this;1431 }1432 handle = target.once(["test", "test"], callback);1433 testEvent = events.test;1434 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1435 Y.Assert.isObject(testEvent.subscribers);1436 Y.Assert.areSame(2, keys(testEvent.subscribers).length);1437 Y.Assert.areSame(0, keys(testEvent.afters).length);1438 Y.Assert.areSame(2, testEvent.hasSubs());1439 Y.Assert.isInstanceOf(Y.EventHandle, handle);1440 Y.Assert.isArray(handle.evt);1441 Y.Assert.areSame(testEvent, handle.evt[0].evt);1442 Y.Assert.areSame(testEvent, handle.evt[1].evt);1443 Y.Assert.isUndefined(handle.sub);1444 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1445 target.fire("test");1446 Y.Assert.areSame(2, count);1447 Y.Assert.areSame(target, thisObj);1448 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1449 },1450 "test target.once([], fn) does nothing": function () {1451 var target = new Y.EventTarget(),1452 events = target._yuievt.events,1453 count = 0,1454 handle, name, subs, i;1455 function callback() {1456 Y.Assert.fail("I don't know how this got called");1457 }1458 handle = target.once([], callback);1459 for (name in events) {1460 if (events.hasOwnProperty(name)) {1461 subs = events[name].subscribers;1462 for (i = subs.length - 1; i >= 0; --i) {1463 if (subs[i].fn === callback) {1464 Y.Assert.fail("subscription registered for '" + name + "' event");1465 }1466 }1467 }1468 }1469 Y.Assert.isInstanceOf(Y.EventHandle, handle);1470 Y.Assert.isArray(handle.evt);1471 Y.Assert.areSame(0, handle.evt.length);1472 Y.Assert.isUndefined(handle.sub);1473 },1474 "test target.once([{ fn: fn, context: obj }]) does nothing": function () {1475 var target = new Y.EventTarget(),1476 events = target._yuievt.events,1477 count = 0,1478 handle, name, subs, i;1479 function callback() {1480 Y.Assert.fail("I don't know how this got called");1481 }1482 handle = target.once([{ fn: callback, context: {} }]);1483 for (name in events) {1484 if (events.hasOwnProperty(name)) {1485 subs = events[name].subscribers;1486 for (i = subs.length - 1; i >= 0; --i) {1487 if (subs[i].fn === callback) {1488 Y.Assert.fail("subscription registered for '" + name + "' event");1489 }1490 }1491 }1492 }1493 Y.Assert.isInstanceOf(Y.EventHandle, handle);1494 Y.Assert.isArray(handle.evt);1495 Y.Assert.areSame(0, handle.evt.length);1496 Y.Assert.isUndefined(handle.sub);1497 },1498 "test target.once({ type: fn })": function () {1499 var target = new Y.EventTarget(),1500 events = target._yuievt.events,1501 handle, thisObj, fired, argCount, testEvent;1502 function callback() {1503 fired = true;1504 thisObj = this;1505 argCount = arguments.length;1506 }1507 handle = target.once({ "test1": callback });1508 testEvent = events.test1;1509 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1510 Y.Assert.isObject(testEvent.subscribers);1511 Y.Assert.areSame(1, keys(testEvent.subscribers).length);1512 Y.Assert.areSame(0, keys(testEvent.afters).length);1513 Y.Assert.areSame(1, testEvent.hasSubs());1514 Y.Assert.isInstanceOf(Y.EventHandle, handle);1515 Y.Assert.isArray(handle.evt);1516 Y.Assert.areSame(1, handle.evt.length);1517 Y.Assert.areSame(testEvent, handle.evt[0].evt);1518 Y.Assert.isUndefined(handle.sub);1519 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1520 target.fire("test1");1521 Y.Assert.isTrue(fired);1522 Y.Assert.areSame(target, thisObj);1523 Y.Assert.areSame(0, argCount);1524 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1525 handle = target.once({1526 "test2": callback,1527 "test3": callback1528 });1529 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);1530 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);1531 Y.Assert.areSame(1, keys(events.test2.subscribers).length);1532 Y.Assert.areSame(1, keys(events.test3.subscribers).length);1533 Y.Assert.isInstanceOf(Y.EventHandle, handle);1534 Y.Assert.isArray(handle.evt);1535 Y.Assert.areSame(2, handle.evt.length);1536 Y.Assert.areSame(events.test2, handle.evt[0].evt);1537 Y.Assert.areSame(events.test3, handle.evt[1].evt);1538 Y.Assert.isUndefined(handle.sub);1539 target.fire("test2");1540 Y.Assert.areSame(0, keys(events.test2.subscribers).length);1541 Y.Assert.areSame(1, keys(events.test3.subscribers).length);1542 target.fire("test3");1543 Y.Assert.areSame(0, keys(events.test2.subscribers).length);1544 Y.Assert.areSame(0, keys(events.test3.subscribers).length);1545 },1546 "test target.once({ type: true }, fn)": function () {1547 var target = new Y.EventTarget(),1548 events = target._yuievt.events,1549 handle, thisObj, fired, argCount, testEvent;1550 function callback() {1551 fired = true;1552 thisObj = this;1553 argCount = arguments.length;1554 }1555 handle = target.once({ "test1": true }, callback);1556 testEvent = events.test1;1557 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1558 Y.Assert.isObject(testEvent.subscribers);1559 Y.Assert.areSame(1, keys(testEvent.subscribers).length);1560 Y.Assert.areSame(0, keys(testEvent.afters).length);1561 Y.Assert.areSame(1, testEvent.hasSubs());1562 Y.Assert.isInstanceOf(Y.EventHandle, handle);1563 Y.Assert.isArray(handle.evt);1564 Y.Assert.areSame(1, handle.evt.length);1565 Y.Assert.areSame(testEvent, handle.evt[0].evt);1566 Y.Assert.isUndefined(handle.sub);