How to use target method in fMBT

Best Python code snippet using fMBT_python

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);1567 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1568 target.fire("test1");1569 Y.Assert.isTrue(fired);1570 Y.Assert.areSame(target, thisObj);1571 Y.Assert.areSame(0, argCount);1572 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1573 handle = target.once({ "test2": 1, "test3": false }, callback);1574 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);1575 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);1576 Y.Assert.areSame(1, keys(events.test2.subscribers).length);1577 Y.Assert.areSame(1, keys(events.test3.subscribers).length);1578 Y.Assert.isInstanceOf(Y.EventHandle, handle);1579 Y.Assert.isArray(handle.evt);1580 Y.Assert.areSame(2, handle.evt.length);1581 Y.Assert.areSame(events.test2, handle.evt[0].evt);1582 Y.Assert.areSame(events.test3, handle.evt[1].evt);1583 Y.Assert.isUndefined(handle.sub);1584 target.fire("test2");1585 Y.Assert.areSame(0, keys(events.test2.subscribers).length);1586 Y.Assert.areSame(1, keys(events.test3.subscribers).length);1587 target.fire("test3");1588 Y.Assert.areSame(0, keys(events.test2.subscribers).length);1589 Y.Assert.areSame(0, keys(events.test3.subscribers).length);1590 },1591 "test target.once(type, { handleEvents: fn })": function () {1592 var target = new Y.EventTarget(),1593 events = target._yuievt.events,1594 obj, handle, thisObj, fired, argCount, testEvent;1595 function callback() {1596 fired = true;1597 thisObj = this;1598 argCount = arguments.length;1599 }1600 obj = { handleEvents: callback };1601 handle = target.once("test", obj);1602 testEvent = events.test;1603 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1604 Y.Assert.isObject(testEvent.subscribers);1605 Y.Assert.areSame(1, keys(testEvent.subscribers).length);1606 Y.Assert.areSame(0, keys(testEvent.afters).length);1607 Y.Assert.areSame(1, testEvent.hasSubs());1608 Y.Assert.isInstanceOf(Y.EventHandle, handle);1609 Y.Assert.areSame(testEvent, handle.evt);1610 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);1611 // Barring support, this is where the error will be thrown.1612 // ET.once() doesn't verify the second arg is a function, and1613 // Subscriber doesn't type check before treating it as a function.1614 target.fire("test");1615 Y.Assert.isTrue(fired);1616 Y.Assert.areSame(obj, thisObj);1617 Y.Assert.areSame(0, argCount);1618 // Fire should immediate detach the subscription1619 testEvent = events.test;1620 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1621 Y.Assert.isObject(testEvent.subscribers);1622 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1623 Y.Assert.areSame(0, keys(testEvent.afters).length);1624 Y.Assert.areSame(1, testEvent.hasSubs());1625 },1626 "test callback context": function () {1627 var target = new Y.EventTarget(),1628 events = target._yuievt.events,1629 targetCount = 0,1630 objCount = 0,1631 obj = {};1632 function isTarget() {1633 Y.Assert.areSame(target, this);1634 targetCount++;1635 }1636 function isObj() {1637 Y.Assert.areSame(obj, this);1638 objCount++;1639 }1640 target.once("test1", isTarget);1641 target.fire("test1");1642 Y.Assert.areSame(1, targetCount);1643 Y.Assert.areSame(0, objCount);1644 target.once("test2", isObj, obj);1645 target.fire("test2");1646 Y.Assert.areSame(1, targetCount);1647 Y.Assert.areSame(1, objCount);1648 target.once("test3", isObj, obj, {});1649 target.fire("test3");1650 Y.Assert.areSame(1, targetCount);1651 Y.Assert.areSame(2, objCount);1652 target.once("test4", isObj, obj, null, {}, {});1653 target.fire("test4");1654 Y.Assert.areSame(1, targetCount);1655 Y.Assert.areSame(3, objCount);1656 target.once("test5", isTarget, null, {});1657 target.fire("test5");1658 Y.Assert.areSame(2, targetCount);1659 Y.Assert.areSame(3, objCount);1660 target.once("prefix:test6", isTarget);1661 target.fire("prefix:test6", obj);1662 Y.Assert.areSame(3, targetCount);1663 Y.Assert.areSame(3, objCount);1664 target.once(["test7", "prefix:test8"], isObj, obj);1665 target.fire("test7");1666 target.fire("prefix:test8");1667 Y.Assert.areSame(3, targetCount);1668 Y.Assert.areSame(5, objCount);1669 target.once({ "test9": isObj }, null, obj);1670 target.fire("test9");1671 Y.Assert.areSame(3, targetCount);1672 Y.Assert.areSame(6, objCount);1673 target.once({1674 "test10": { fn: isTarget },1675 "test11": { fn: isObj, context: obj }1676 });1677 target.fire("test10");1678 target.fire("test11");1679 Y.Assert.areSame(4, targetCount);1680 Y.Assert.areSame(7, objCount);1681 target.once({1682 "test12": { fn: isObj },1683 "prefix:test13": { fn: isTarget, context: target }1684 }, null, obj);1685 target.fire("test12");1686 target.fire("prefix:test13");1687 Y.Assert.areSame(5, targetCount);1688 Y.Assert.areSame(8, objCount);1689 Y.Assert.areSame(0, keys(events.test1.subscribers).length);1690 Y.Assert.areSame(0, keys(events.test2.subscribers).length);1691 Y.Assert.areSame(0, keys(events.test3.subscribers).length);1692 Y.Assert.areSame(0, keys(events.test4.subscribers).length);1693 Y.Assert.areSame(0, keys(events.test5.subscribers).length);1694 Y.Assert.areSame(0, keys(events['prefix:test6'].subscribers).length);1695 Y.Assert.areSame(0, keys(events.test7.subscribers).length);1696 Y.Assert.areSame(0, keys(events['prefix:test8'].subscribers).length);1697 Y.Assert.areSame(0, keys(events.test9.subscribers).length);1698 Y.Assert.areSame(0, keys(events.test10.subscribers).length);1699 Y.Assert.areSame(0, keys(events.test11.subscribers).length);1700 Y.Assert.areSame(0, keys(events.test12.subscribers).length);1701 Y.Assert.areSame(0, keys(events['prefix:test13'].subscribers).length);1702 },1703 "test subscription bound args": function () {1704 var target = new Y.EventTarget(),1705 events = target._yuievt.events,1706 obj = {},1707 args;1708 function callback() {1709 args = Y.Array(arguments, 0, true);1710 }1711 target.once("test1", callback, {}, "a", 1, obj, null);1712 target.fire("test1");1713 Y.ArrayAssert.itemsAreSame(["a", 1, obj, null], args);1714 target.once(["test2", "test3"], callback, null, "a", 2.3, obj, null);1715 target.fire("test2");1716 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);1717 args = [];1718 target.fire("test3");1719 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);1720 // ugh, requiring two placeholders for (unused) fn and context is ooogly1721 target.once({1722 "test4": callback,1723 "test5": callback1724 }, null, null, "a", 4.5, obj, null);1725 target.fire("test4");1726 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);1727 args = [];1728 target.fire("test5");1729 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);1730 target.once({1731 "test6": true,1732 "test7": false1733 }, callback, {}, "a", 6.7, obj, null);1734 target.fire("test6");1735 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);1736 args = [];1737 target.fire("test7");1738 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);1739 Y.Assert.areSame(0, keys(events.test1.subscribers).length);1740 Y.Assert.areSame(0, keys(events.test2.subscribers).length);1741 Y.Assert.areSame(0, keys(events.test3.subscribers).length);1742 Y.Assert.areSame(0, keys(events.test4.subscribers).length);1743 Y.Assert.areSame(0, keys(events.test5.subscribers).length);1744 Y.Assert.areSame(0, keys(events.test6.subscribers).length);1745 Y.Assert.areSame(0, keys(events.test7.subscribers).length);1746 },1747 "test target.once('click', fn) registers custom event only": function () {1748 var target = new Y.EventTarget(),1749 events = target._yuievt.events,1750 fired = false;1751 target.once("click", function () {1752 fired = true;1753 // Not an emitFacade event, so there's no e to verify type1754 });1755 Y.Assert.isInstanceOf(Y.CustomEvent, events.click);1756 Y.Assert.isUndefined(events.click.domkey);1757 Y.Assert.areSame(1, keys(events.click.subscribers).length);1758 target.fire("click");1759 Y.Assert.isTrue(fired);1760 Y.Assert.areSame(0, keys(events.click.subscribers).length);1761 }1762}));1763baseSuite.add(new Y.Test.Case({1764 name: "target.onceAfter",1765 _should: {1766 ignore: {1767 // As of 3.4.1, creates a subscription to a custom event named1768 // "[object Object]"1769 "test target.onceAfter([{ fn: fn, context: obj }]) does nothing": true,1770 // Not (yet) implemented1771 "test target.onceAfter(type, { handleEvents: fn })": true1772 }1773 },1774 test_onceAfter: function () {1775 var a = new Y.EventTarget({ emitFacade: true, prefix: 'a' }),1776 result = '';1777 a.on('foo', function () { result += 'A'; });1778 a.once('foo', function () { result += 'B'; });1779 a.after('foo', function () { result += 'C'; });1780 a.onceAfter('foo', function () { result += 'D'; });1781 a.fire('foo');1782 a.fire('foo');1783 Y.Assert.areSame("ABCDAC", result);1784 },1785 "test auto-publish on subscribe": function () {1786 var target = new Y.EventTarget(),1787 events = target._yuievt.events,1788 publishCalled;1789 target.publish = (function (original) {1790 return function (type) {1791 if (type === 'test') {1792 publishCalled = true;1793 }1794 return original.apply(this, arguments);1795 };1796 })(target.publish);1797 Y.Assert.isUndefined(events.test);1798 target.onceAfter("test", function () {});1799 Y.Assert.isTrue(publishCalled);1800 Y.Assert.isObject(events.test);1801 Y.Assert.isInstanceOf(Y.CustomEvent, events.test);1802 },1803 "test target.onceAfter(type, fn)": function () {1804 var target = new Y.EventTarget(),1805 events = target._yuievt.events,1806 handle, thisObj, fired, argCount, testEvent;1807 function callback() {1808 fired = true;1809 thisObj = this;1810 argCount = arguments.length;1811 }1812 handle = target.onceAfter("test", callback);1813 testEvent = events.test;1814 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1815 Y.Assert.isObject(testEvent.afters);1816 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1817 Y.Assert.areSame(1, keys(testEvent.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 target.fire("test");1823 Y.Assert.isTrue(fired);1824 Y.Assert.areSame(target, thisObj);1825 Y.Assert.areSame(0, argCount);1826 // Test that fire() resulted in immediate detach of onceAfter() sub1827 testEvent = events.test;1828 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1829 Y.Assert.isObject(testEvent.afters);1830 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1831 Y.Assert.areSame(0, keys(testEvent.afters).length);1832 Y.Assert.areSame(0, testEvent.hasSubs());1833 },1834 "test target.onceAfter(type, fn) allows duplicate subs": function () {1835 var target = new Y.EventTarget(),1836 events = target._yuievt.events,1837 count = 0,1838 testEvent, handle1, handle2;1839 function callback() {1840 count++;1841 }1842 handle1 = target.onceAfter("test", callback);1843 handle2 = target.onceAfter("test", callback);1844 testEvent = events.test;1845 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1846 Y.Assert.isObject(testEvent.afters);1847 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1848 Y.Assert.areSame(2, keys(testEvent.afters).length);1849 Y.Assert.areSame(2, testEvent.hasSubs());1850 Y.Assert.areNotSame(handle1, handle2);1851 Y.Assert.areSame(testEvent, handle1.evt);1852 Y.Assert.areSame(handle1.evt, handle2.evt);1853 Y.Assert.areNotSame(handle1.sub, handle2.sub);1854 target.fire("test");1855 Y.Assert.areSame(2, count);1856 // Test that fire() resulted in immediate detach of onceAfter() sub1857 testEvent = events.test;1858 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1859 Y.Assert.isObject(testEvent.afters);1860 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1861 Y.Assert.areSame(0, keys(testEvent.afters).length);1862 Y.Assert.areSame(0, testEvent.hasSubs());1863 target.fire("test");1864 Y.Assert.areSame(2, count);1865 },1866 "test target.onceAfter(type, fn, obj)": function () {1867 var target = new Y.EventTarget(),1868 obj = {},1869 count = 0,1870 thisObj1, thisObj2, argCount, testEvent;1871 function callback() {1872 count++;1873 thisObj1 = this;1874 argCount = arguments.length;1875 }1876 target.onceAfter("test", callback, obj);1877 target.fire("test");1878 Y.Assert.areSame(1, count);1879 Y.Assert.areSame(obj, thisObj1);1880 Y.Assert.areSame(0, argCount);1881 // Subscriber should be detached, so count should not increment1882 target.fire("test");1883 Y.Assert.areSame(1, count);1884 target.onceAfter("test", function () {1885 thisObj2 = this;1886 });1887 target.fire("test");1888 Y.Assert.areSame(1, count);1889 Y.Assert.areSame(obj, thisObj1);1890 Y.Assert.areSame(target, thisObj2);1891 // Subscriber should be detached, so count should not increment1892 target.fire("test");1893 Y.Assert.areSame(1, count);1894 },1895 "test target.onceAfter(type, fn, obj, args)": function () {1896 var target = new Y.EventTarget(),1897 obj = {},1898 count = 0,1899 args = '',1900 argCount,1901 thisObj1, thisObj2, testEvent;1902 function callback() {1903 count++;1904 thisObj1 = this;1905 argCount = arguments.length;1906 for (var i = 0, len = argCount; i < len; ++i) {1907 args += arguments[i];1908 }1909 }1910 target.onceAfter("test", callback, obj, "A");1911 target.fire("test");1912 Y.Assert.areSame(1, count);1913 Y.Assert.areSame(obj, thisObj1);1914 Y.Assert.areSame("A", args);1915 // Subscriber should be detached, so count should not increment1916 target.fire("test");1917 Y.Assert.areSame(1, count);1918 target.onceAfter("test", function () {1919 thisObj2 = this;1920 });1921 target.fire("test");1922 Y.Assert.areSame(1, count);1923 Y.Assert.areSame(obj, thisObj1);1924 Y.Assert.areSame(target, thisObj2);1925 // Subscriber should be detached, so count should not increment1926 target.fire("test");1927 Y.Assert.areSame(1, count);1928 },1929 "test target.onceAfter([type], fn)": function () {1930 var target = new Y.EventTarget(),1931 events = target._yuievt.events,1932 handle, thisObj, fired, argCount, testEvent;1933 function callback() {1934 fired = true;1935 thisObj = this;1936 argCount = arguments.length;1937 }1938 handle = target.onceAfter(["test"], callback);1939 testEvent = events.test;1940 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);1941 Y.Assert.isObject(testEvent.afters);1942 Y.Assert.areSame(0, keys(testEvent.subscribers).length);1943 Y.Assert.areSame(1, keys(testEvent.afters).length);1944 Y.Assert.areSame(1, testEvent.hasSubs());1945 Y.Assert.isInstanceOf(Y.EventHandle, handle);1946 Y.Assert.isArray(handle.evt);1947 Y.Assert.areSame(testEvent, handle.evt[0].evt);1948 Y.Assert.isUndefined(handle.sub);1949 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1950 target.fire("test");1951 Y.Assert.isTrue(fired);1952 Y.Assert.areSame(target, thisObj);1953 Y.Assert.areSame(0, argCount);1954 Y.Assert.areSame(0, keys(testEvent.afters).length);1955 fired = false;1956 target.fire("test");1957 Y.Assert.isFalse(fired);1958 },1959 "test target.onceAfter([typeA, typeB], fn)": function () {1960 var target = new Y.EventTarget(),1961 events = target._yuievt.events,1962 count = 0,1963 handle, thisObj, testEvent1, testEvent2;1964 function callback() {1965 count++;1966 thisObj = this;1967 }1968 handle = target.onceAfter(["test1", "test2"], callback);1969 testEvent1 = events.test1;1970 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent1);1971 Y.Assert.isObject(testEvent1.afters);1972 Y.Assert.areSame(0, keys(testEvent1.subscribers).length);1973 Y.Assert.areSame(1, keys(testEvent1.afters).length);1974 Y.Assert.areSame(1, testEvent1.hasSubs());1975 testEvent2 = events.test2;1976 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent2);1977 Y.Assert.isObject(testEvent2.afters);1978 Y.Assert.areSame(0, keys(testEvent2.subscribers).length);1979 Y.Assert.areSame(1, keys(testEvent2.afters).length);1980 Y.Assert.areSame(1, testEvent1.hasSubs());1981 Y.Assert.isInstanceOf(Y.EventHandle, handle);1982 Y.Assert.isArray(handle.evt);1983 Y.Assert.areSame(testEvent1, handle.evt[0].evt);1984 Y.Assert.areSame(testEvent2, handle.evt[1].evt);1985 Y.Assert.areNotSame(testEvent1, testEvent2);1986 Y.Assert.isUndefined(handle.sub);1987 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);1988 target.fire("test1");1989 Y.Assert.areSame(1, count);1990 Y.Assert.areSame(target, thisObj);1991 Y.Assert.areSame(0, keys(testEvent1.afters).length);1992 target.fire("test2");1993 Y.Assert.areSame(2, count);1994 Y.Assert.areSame(target, thisObj);1995 Y.Assert.areSame(0, keys(testEvent2.afters).length);1996 },1997 "test target.onceAfter([typeA, typeA], fn)": function () {1998 var target = new Y.EventTarget(),1999 events = target._yuievt.events,2000 count = 0,2001 handle, thisObj, testEvent;2002 function callback() {2003 count++;2004 thisObj = this;2005 }2006 handle = target.onceAfter(["test", "test"], callback);2007 testEvent = events.test;2008 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2009 Y.Assert.isObject(testEvent.afters);2010 Y.Assert.areSame(0, keys(testEvent.subscribers).length);2011 Y.Assert.areSame(2, keys(testEvent.afters).length);2012 Y.Assert.areSame(2, testEvent.hasSubs());2013 Y.Assert.isInstanceOf(Y.EventHandle, handle);2014 Y.Assert.isArray(handle.evt);2015 Y.Assert.areSame(testEvent, handle.evt[0].evt);2016 Y.Assert.areSame(testEvent, handle.evt[1].evt);2017 Y.Assert.isUndefined(handle.sub);2018 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);2019 target.fire("test");2020 Y.Assert.areSame(2, count);2021 Y.Assert.areSame(target, thisObj);2022 Y.Assert.areSame(0, keys(testEvent.afters).length);2023 },2024 "test target.onceAfter([], fn) does nothing": function () {2025 var target = new Y.EventTarget(),2026 events = target._yuievt.events,2027 count = 0,2028 handle, name, subs, i;2029 function callback() {2030 Y.Assert.fail("I don't know how this got called");2031 }2032 handle = target.onceAfter([], callback);2033 for (name in events) {2034 if (events.hasOwnProperty(name)) {2035 subs = events[name].afters;2036 for (i = subs.length - 1; i >= 0; --i) {2037 if (subs[i].fn === callback) {2038 Y.Assert.fail("subscription registered for '" + name + "' event");2039 }2040 }2041 }2042 }2043 Y.Assert.isInstanceOf(Y.EventHandle, handle);2044 Y.Assert.isArray(handle.evt);2045 Y.Assert.areSame(0, handle.evt.length);2046 Y.Assert.isUndefined(handle.sub);2047 },2048 "test target.onceAfter([{ fn: fn, context: obj }]) does nothing": function () {2049 var target = new Y.EventTarget(),2050 events = target._yuievt.events,2051 count = 0,2052 handle, name, subs, i;2053 function callback() {2054 Y.Assert.fail("I don't know how this got called");2055 }2056 handle = target.onceAfter([{ fn: callback, context: {} }]);2057 for (name in events) {2058 if (events.hasOwnProperty(name)) {2059 subs = events[name].afters;2060 for (i = subs.length - 1; i >= 0; --i) {2061 if (subs[i].fn === callback) {2062 Y.Assert.fail("subscription registered for '" + name + "' event");2063 }2064 }2065 }2066 }2067 Y.Assert.isInstanceOf(Y.EventHandle, handle);2068 Y.Assert.isArray(handle.evt);2069 Y.Assert.areSame(0, handle.evt.length);2070 Y.Assert.isUndefined(handle.sub);2071 },2072 "test target.onceAfter({ type: fn })": function () {2073 var target = new Y.EventTarget(),2074 events = target._yuievt.events,2075 handle, thisObj, fired, argCount, testEvent;2076 function callback() {2077 fired = true;2078 thisObj = this;2079 argCount = arguments.length;2080 }2081 handle = target.onceAfter({ "test1": callback });2082 testEvent = events.test1;2083 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2084 Y.Assert.isObject(testEvent.afters);2085 Y.Assert.areSame(0, keys(testEvent.subscribers).length);2086 Y.Assert.areSame(1, keys(testEvent.afters).length);2087 Y.Assert.areSame(1, testEvent.hasSubs());2088 Y.Assert.isInstanceOf(Y.EventHandle, handle);2089 Y.Assert.isArray(handle.evt);2090 Y.Assert.areSame(1, handle.evt.length);2091 Y.Assert.areSame(testEvent, handle.evt[0].evt);2092 Y.Assert.isUndefined(handle.sub);2093 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);2094 target.fire("test1");2095 Y.Assert.isTrue(fired);2096 Y.Assert.areSame(target, thisObj);2097 Y.Assert.areSame(0, argCount);2098 Y.Assert.areSame(0, keys(testEvent.afters).length);2099 handle = target.onceAfter({2100 "test2": callback,2101 "test3": callback2102 });2103 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);2104 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);2105 Y.Assert.areSame(1, keys(events.test2.afters).length);2106 Y.Assert.areSame(1, keys(events.test3.afters).length);2107 Y.Assert.isInstanceOf(Y.EventHandle, handle);2108 Y.Assert.isArray(handle.evt);2109 Y.Assert.areSame(2, handle.evt.length);2110 Y.Assert.areSame(events.test2, handle.evt[0].evt);2111 Y.Assert.areSame(events.test3, handle.evt[1].evt);2112 Y.Assert.isUndefined(handle.sub);2113 target.fire("test2");2114 Y.Assert.areSame(0, keys(events.test2.afters).length);2115 Y.Assert.areSame(1, keys(events.test3.afters).length);2116 target.fire("test3");2117 Y.Assert.areSame(0, keys(events.test2.afters).length);2118 Y.Assert.areSame(0, keys(events.test3.afters).length);2119 },2120 "test target.onceAfter({ type: true }, fn)": function () {2121 var target = new Y.EventTarget(),2122 events = target._yuievt.events,2123 handle, thisObj, fired, argCount, testEvent;2124 function callback() {2125 fired = true;2126 thisObj = this;2127 argCount = arguments.length;2128 }2129 handle = target.onceAfter({ "test1": true }, callback);2130 testEvent = events.test1;2131 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2132 Y.Assert.isObject(testEvent.afters);2133 Y.Assert.areSame(0, keys(testEvent.subscribers).length);2134 Y.Assert.areSame(1, keys(testEvent.afters).length);2135 Y.Assert.areSame(1, testEvent.hasSubs());2136 Y.Assert.isInstanceOf(Y.EventHandle, handle);2137 Y.Assert.isArray(handle.evt);2138 Y.Assert.areSame(1, handle.evt.length);2139 Y.Assert.areSame(testEvent, handle.evt[0].evt);2140 Y.Assert.isUndefined(handle.sub);2141 Y.Assert.isInstanceOf(Y.Subscriber, handle.evt[0].sub);2142 target.fire("test1");2143 Y.Assert.isTrue(fired);2144 Y.Assert.areSame(target, thisObj);2145 Y.Assert.areSame(0, argCount);2146 Y.Assert.areSame(0, keys(testEvent.afters).length);2147 handle = target.onceAfter({ "test2": 1, "test3": false }, callback);2148 Y.Assert.isInstanceOf(Y.CustomEvent, events.test2);2149 Y.Assert.isInstanceOf(Y.CustomEvent, events.test3);2150 Y.Assert.areSame(1, keys(events.test2.afters).length);2151 Y.Assert.areSame(1, keys(events.test3.afters).length);2152 Y.Assert.isInstanceOf(Y.EventHandle, handle);2153 Y.Assert.isArray(handle.evt);2154 Y.Assert.areSame(2, handle.evt.length);2155 Y.Assert.areSame(events.test2, handle.evt[0].evt);2156 Y.Assert.areSame(events.test3, handle.evt[1].evt);2157 Y.Assert.isUndefined(handle.sub);2158 target.fire("test2");2159 Y.Assert.areSame(0, keys(events.test2.afters).length);2160 Y.Assert.areSame(1, keys(events.test3.afters).length);2161 target.fire("test3");2162 Y.Assert.areSame(0, keys(events.test2.afters).length);2163 Y.Assert.areSame(0, keys(events.test3.afters).length);2164 },2165 "test target.onceAfter(type, { handleEvents: fn })": function () {2166 var target = new Y.EventTarget(),2167 events = target._yuievt.events,2168 obj, handle, thisObj, fired, argCount, testEvent;2169 function callback() {2170 fired = true;2171 thisObj = this;2172 argCount = arguments.length;2173 }2174 obj = { handleEvents: callback };2175 handle = target.onceAfter("test", obj);2176 testEvent = events.test;2177 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2178 Y.Assert.isObject(testEvent.afters);2179 Y.Assert.areSame(0, keys(testEvent.subscribers).length);2180 Y.Assert.areSame(1, keys(testEvent.afters).length);2181 Y.Assert.areSame(1, testEvent.hasSubs());2182 Y.Assert.isInstanceOf(Y.EventHandle, handle);2183 Y.Assert.areSame(testEvent, handle.evt);2184 Y.Assert.isInstanceOf(Y.Subscriber, handle.sub);2185 // Barring support, this is where the error will be thrown.2186 // ET.onceAfter() doesn't verify the second arg is a function, and2187 // Subscriber doesn't type check before treating it as a function.2188 target.fire("test");2189 Y.Assert.isTrue(fired);2190 Y.Assert.areSame(obj, thisObj);2191 Y.Assert.areSame(0, argCount);2192 // Fire should immediate detach the subscription2193 testEvent = events.test;2194 Y.Assert.isInstanceOf(Y.CustomEvent, testEvent);2195 Y.Assert.isObject(testEvent.afters);2196 Y.Assert.areSame(0, keys(testEvent.subscribers).length);2197 Y.Assert.areSame(0, keys(testEvent.afters).length);2198 Y.Assert.areSame(0, testEvent.hasSubs());2199 },2200 "test callback context": function () {2201 var target = new Y.EventTarget(),2202 events = target._yuievt.events,2203 targetCount = 0,2204 objCount = 0,2205 obj = {};2206 function isTarget() {2207 Y.Assert.areSame(target, this);2208 targetCount++;2209 }2210 function isObj() {2211 Y.Assert.areSame(obj, this);2212 objCount++;2213 }2214 target.onceAfter("test1", isTarget);2215 target.fire("test1");2216 Y.Assert.areSame(1, targetCount);2217 Y.Assert.areSame(0, objCount);2218 target.onceAfter("test2", isObj, obj);2219 target.fire("test2");2220 Y.Assert.areSame(1, targetCount);2221 Y.Assert.areSame(1, objCount);2222 target.onceAfter("test3", isObj, obj, {});2223 target.fire("test3");2224 Y.Assert.areSame(1, targetCount);2225 Y.Assert.areSame(2, objCount);2226 target.onceAfter("test4", isObj, obj, null, {}, {});2227 target.fire("test4");2228 Y.Assert.areSame(1, targetCount);2229 Y.Assert.areSame(3, objCount);2230 target.onceAfter("test5", isTarget, null, {});2231 target.fire("test5");2232 Y.Assert.areSame(2, targetCount);2233 Y.Assert.areSame(3, objCount);2234 target.onceAfter("prefix:test6", isTarget);2235 target.fire("prefix:test6", obj);2236 Y.Assert.areSame(3, targetCount);2237 Y.Assert.areSame(3, objCount);2238 target.onceAfter(["test7", "prefix:test8"], isObj, obj);2239 target.fire("test7");2240 target.fire("prefix:test8");2241 Y.Assert.areSame(3, targetCount);2242 Y.Assert.areSame(5, objCount);2243 target.onceAfter({ "test9": isObj }, null, obj);2244 target.fire("test9");2245 Y.Assert.areSame(3, targetCount);2246 Y.Assert.areSame(6, objCount);2247 target.onceAfter({2248 "test10": { fn: isTarget },2249 "test11": { fn: isObj, context: obj }2250 });2251 target.fire("test10");2252 target.fire("test11");2253 Y.Assert.areSame(4, targetCount);2254 Y.Assert.areSame(7, objCount);2255 target.onceAfter({2256 "test12": { fn: isObj },2257 "prefix:test13": { fn: isTarget, context: target }2258 }, null, obj);2259 target.fire("test12");2260 target.fire("prefix:test13");2261 Y.Assert.areSame(5, targetCount);2262 Y.Assert.areSame(8, objCount);2263 Y.Assert.areSame(0, keys(events.test1.afters).length);2264 Y.Assert.areSame(0, keys(events.test2.afters).length);2265 Y.Assert.areSame(0, keys(events.test3.afters).length);2266 Y.Assert.areSame(0, keys(events.test4.afters).length);2267 Y.Assert.areSame(0, keys(events.test5.afters).length);2268 Y.Assert.areSame(0, keys(events['prefix:test6'].afters).length);2269 Y.Assert.areSame(0, keys(events.test7.afters).length);2270 Y.Assert.areSame(0, keys(events['prefix:test8'].afters).length);2271 Y.Assert.areSame(0, keys(events.test9.afters).length);2272 Y.Assert.areSame(0, keys(events.test10.afters).length);2273 Y.Assert.areSame(0, keys(events.test11.afters).length);2274 Y.Assert.areSame(0, keys(events.test12.afters).length);2275 Y.Assert.areSame(0, keys(events['prefix:test13'].afters).length);2276 },2277 "test subscription bound args": function () {2278 var target = new Y.EventTarget(),2279 events = target._yuievt.events,2280 obj = {},2281 args;2282 function callback() {2283 args = Y.Array(arguments, 0, true);2284 }2285 target.onceAfter("test1", callback, {}, "a", 1, obj, null);2286 target.fire("test1");2287 Y.ArrayAssert.itemsAreSame(["a", 1, obj, null], args);2288 target.onceAfter(["test2", "test3"], callback, null, "a", 2.3, obj, null);2289 target.fire("test2");2290 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);2291 args = [];2292 target.fire("test3");2293 Y.ArrayAssert.itemsAreSame(["a", 2.3, obj, null], args);2294 // ugh, requiring two placeholders for (unused) fn and context is ooogly2295 target.onceAfter({2296 "test4": callback,2297 "test5": callback2298 }, null, null, "a", 4.5, obj, null);2299 target.fire("test4");2300 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);2301 args = [];2302 target.fire("test5");2303 Y.ArrayAssert.itemsAreSame(["a", 4.5, obj, null], args);2304 target.onceAfter({2305 "test6": true,2306 "test7": false2307 }, callback, {}, "a", 6.7, obj, null);2308 target.fire("test6");2309 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);2310 args = [];2311 target.fire("test7");2312 Y.ArrayAssert.itemsAreSame(["a", 6.7, obj, null], args);2313 Y.Assert.areSame(0, keys(events.test1.afters).length);2314 Y.Assert.areSame(0, keys(events.test2.afters).length);2315 Y.Assert.areSame(0, keys(events.test3.afters).length);2316 Y.Assert.areSame(0, keys(events.test4.afters).length);2317 Y.Assert.areSame(0, keys(events.test5.afters).length);2318 Y.Assert.areSame(0, keys(events.test6.afters).length);2319 Y.Assert.areSame(0, keys(events.test7.afters).length);2320 },2321 "test target.onceAfter('click', fn) registers custom event only": function () {2322 var target = new Y.EventTarget(),2323 events = target._yuievt.events,2324 fired = false;2325 target.onceAfter("click", function () {2326 fired = true;2327 // Not an emitFacade event, so there's no e to verify type2328 });2329 Y.Assert.isInstanceOf(Y.CustomEvent, events.click);2330 Y.Assert.isUndefined(events.click.domkey);2331 Y.Assert.areSame(1, keys(events.click.afters).length);2332 target.fire("click");2333 Y.Assert.isTrue(fired);2334 Y.Assert.areSame(0, keys(events.click.afters).length);2335 }2336}));2337baseSuite.add(new Y.Test.Case({2338 name: "target.detach",2339 "test target.detach() with not subs is harmless": function () {2340 var target = new Y.EventTarget();2341 function fn() {}2342 target.detach('test');2343 target.detach('category|test');2344 target.detach('prefix:test');2345 target.detach('category|prefix:test');2346 target.detach('test', fn);2347 target.detach('category|test', fn);2348 target.detach('prefix:test', fn);2349 target.detach('category|prefix:test', fn);2350 target.detach();2351 Y.Assert.isTrue(true);2352 },2353 "test target.detachAll() with not subs is harmless": function () {2354 var target = new Y.EventTarget();2355 target.detachAll();2356 target.detachAll('test');2357 target.detachAll('prefix:test');2358 target.detachAll('category|test');2359 target.detachAll('category|prefix:test');2360 Y.Assert.isTrue(true);2361 },2362 "test target.on() + target.detach(type, fn)": function () {2363 var count = 0,2364 target = new Y.EventTarget();2365 function fn() {2366 count++;2367 }2368 target.on('test', fn);2369 target.fire('test');2370 Y.Assert.areSame(1, count);2371 target.detach('test', fn);2372 target.fire('test');2373 Y.Assert.areSame(1, count);2374 target.on('test', fn);2375 target.on('test', fn);2376 target.fire('test');2377 Y.Assert.areSame(3, count);2378 target.detach('test', fn);2379 target.fire('test');2380 Y.Assert.areSame(3, count);2381 },2382 "test target.on(type, fn, thisObj) + target.detach(type, fn)": function () {2383 var count = 0,2384 a = {},2385 b = {},2386 target = new Y.EventTarget();2387 function fn() {2388 count++;2389 }2390 target.on('test', fn, a);2391 target.fire('test');2392 Y.Assert.areSame(1, count);2393 target.detach('test', fn);2394 target.fire('test');2395 Y.Assert.areSame(1, count);2396 target.on('test', fn, a);2397 target.on('test', fn, b);2398 target.fire('test');2399 Y.Assert.areSame(3, count);2400 target.detach('test', fn);2401 target.fire('test');2402 Y.Assert.areSame(3, count);2403 },2404 "test target.on() + target.detach(type)": function () {2405 var count = 0,2406 target = new Y.EventTarget();2407 function fn() {2408 count++;2409 }2410 target.on('test', fn);2411 target.fire('test');2412 Y.Assert.areSame(1, count);2413 target.detach('test');2414 target.fire('test');2415 Y.Assert.areSame(1, count);2416 target.on('test', fn);2417 target.on('test', fn);2418 target.fire('test');2419 Y.Assert.areSame(3, count);2420 target.detach('test');2421 target.fire('test');2422 Y.Assert.areSame(3, count);2423 },2424 "test target.on() + target.detach()": function () {2425 var count = 0,2426 target = new Y.EventTarget();2427 function fn() {2428 count++;2429 }2430 target.on('test', fn);2431 target.fire('test');2432 Y.Assert.areSame(1, count);2433 target.detach();2434 target.fire('test');2435 Y.Assert.areSame(1, count);2436 target.on('test', fn);2437 target.on('test', fn);2438 target.fire('test');2439 Y.Assert.areSame(3, count);2440 target.detach();2441 target.fire('test');2442 Y.Assert.areSame(3, count);2443 },2444 "test target.on() + target.detachAll()": function () {2445 var count = 0,2446 target = new Y.EventTarget();2447 function fn() {2448 count++;2449 }2450 target.on('test', fn);2451 target.fire('test');2452 Y.Assert.areSame(1, count);2453 target.detachAll();2454 target.fire('test');2455 Y.Assert.areSame(1, count);2456 target.on('test', fn);2457 target.on('test', fn);2458 target.fire('test');2459 Y.Assert.areSame(3, count);2460 target.detachAll();2461 target.fire('test');2462 Y.Assert.areSame(3, count);2463 },2464 "test target.on() + handle.detach()": function () {2465 var count = 0,2466 target = new Y.EventTarget(),2467 sub;2468 function increment() {2469 count++;2470 }2471 sub = target.on('test', increment);2472 target.fire('test');2473 Y.Assert.areSame(1, count);2474 sub.detach();2475 target.fire('test');2476 Y.Assert.areSame(1, count);2477 },2478 "test target.on('cat|__', fn) + target.detach('cat|___')": function () {2479 var count = 0,2480 target = new Y.EventTarget();2481 function increment() {2482 count++;2483 }2484 target.on('cat|test', increment);2485 target.fire('test');2486 Y.Assert.areSame(1, count);2487 target.detach('cat|test');2488 target.fire('test');2489 Y.Assert.areSame(1, count);2490 },2491 "test target.on('cat|__', fn) + target.detach('cat|___', fn)": function () {2492 var count = 0,2493 target = new Y.EventTarget();2494 function increment() {2495 count++;2496 }2497 target.on('cat|test', increment);2498 target.fire('test');2499 Y.Assert.areSame(1, count);2500 target.detach('cat|test', increment);2501 target.fire('test');2502 Y.Assert.areSame(1, count);2503 },2504 "test target.on('cat|__', fn) + target.detach('cat|*')": function () {2505 var count = 0,2506 target = new Y.EventTarget();2507 function increment() {2508 count++;2509 }2510 target.on('cat|test', increment);2511 target.fire('test');2512 Y.Assert.areSame(1, count);2513 target.detach('cat|*');2514 target.fire('test');2515 Y.Assert.areSame(1, count);2516 },2517 "test target.on({...}) + target.detach(type)": function () {2518 var count = 0,2519 target = new Y.EventTarget();2520 function increment() {2521 count++;2522 }2523 target.on({2524 test1: increment,2525 test2: increment2526 });2527 target.fire('test1');2528 target.fire('test2');2529 Y.Assert.areSame(2, count);2530 target.detach('test1');2531 target.fire('test1');2532 Y.Assert.areSame(2, count);2533 target.fire('test2');2534 Y.Assert.areSame(3, count);2535 target.detach('test2');2536 target.fire('test1');2537 target.fire('test2');2538 Y.Assert.areSame(3, count);2539 },2540 "test target.on({...}) + target.detach(type, fn)": function () {2541 var count = 0,2542 target = new Y.EventTarget();2543 function increment() {2544 count++;2545 }2546 target.on({2547 test1: increment,2548 test2: increment2549 });2550 target.fire('test1');2551 target.fire('test2');2552 Y.Assert.areSame(2, count);2553 target.detach('test1', increment);2554 target.fire('test1');2555 Y.Assert.areSame(2, count);2556 target.fire('test2');2557 Y.Assert.areSame(3, count);2558 target.detach('test2', increment);2559 target.fire('test1');2560 target.fire('test2');2561 Y.Assert.areSame(3, count);2562 },2563 "test target.on({...}) + target.detachAll()": function () {2564 var count = 0,2565 target = new Y.EventTarget();2566 function increment() {2567 count++;2568 }2569 target.on({2570 test1: increment,2571 test2: increment2572 });2573 target.fire('test1');2574 target.fire('test2');2575 Y.Assert.areSame(2, count);2576 target.detachAll();2577 target.fire('test1');2578 Y.Assert.areSame(2, count);2579 target.fire('test2');2580 Y.Assert.areSame(2, count);2581 },2582 "test target.on({'cat|type': fn}) + target.detach(type, fn)": function () {2583 var count = 0,2584 target = new Y.EventTarget();2585 function increment() {2586 count++;2587 }2588 target.on({2589 'cat|test1': increment,2590 test2: increment2591 });2592 target.fire('test1');2593 target.fire('test2');2594 Y.Assert.areSame(2, count);2595 target.detach('test1');2596 target.fire('test1');2597 Y.Assert.areSame(2, count);2598 target.fire('test2');2599 Y.Assert.areSame(3, count);2600 },2601 "test target.on({'cat|type': fn}) + target.detach('cat|type')": function () {2602 var count = 0,2603 target = new Y.EventTarget();2604 function increment() {2605 count++;2606 }2607 target.on({2608 'cat|test1': increment,2609 test2: increment2610 });2611 target.fire('test1');2612 target.fire('test2');2613 Y.Assert.areSame(2, count);2614 target.detach('cat|test1');2615 target.fire('test1');2616 Y.Assert.areSame(2, count);2617 target.fire('test2');2618 Y.Assert.areSame(3, count);2619 },2620 "test target.on({'cat|type': fn}) + target.detach('cat|*')": function () {2621 var count = 0,2622 target = new Y.EventTarget();2623 function increment() {2624 count++;2625 }2626 target.on({2627 'cat|test1': increment,2628 'cat|test2': increment,2629 test3: increment2630 });2631 target.fire('test1');2632 target.fire('test2');2633 target.fire('test3');2634 Y.Assert.areSame(3, count);2635 target.detach('cat|*');2636 target.fire('test1');2637 Y.Assert.areSame(3, count);2638 target.fire('test2');2639 Y.Assert.areSame(3, count);2640 target.fire('test3');2641 Y.Assert.areSame(4, count);2642 },2643 "test target.on([type], fn) + target.detach(type, fn)": function () {2644 var count = 0,2645 target = new Y.EventTarget();2646 function increment() {2647 count++;2648 }2649 target.on(['test'], increment);2650 target.fire('test');2651 Y.Assert.areSame(1, count);2652 target.detach('test', increment);2653 target.fire('test');2654 Y.Assert.areSame(1, count);2655 },2656 "test target.on([type], fn) + target.detach(type)": function () {2657 var count = 0,2658 target = new Y.EventTarget();2659 function increment() {2660 count++;2661 }2662 target.on(['test'], increment);2663 target.fire('test');2664 Y.Assert.areSame(1, count);2665 target.detach('test');2666 target.fire('test');2667 Y.Assert.areSame(1, count);2668 },2669 "test target.on([typeA, typeB], fn) + target.detach(typeA)": function () {2670 var count = 0,2671 target = new Y.EventTarget();2672 function increment() {2673 count++;2674 }2675 target.on(['test1', 'test2'], increment);2676 target.fire('test1');2677 target.fire('test2');2678 Y.Assert.areSame(2, count);2679 target.detach('test1');2680 target.fire('test1');2681 target.fire('test2');2682 Y.Assert.areSame(3, count);2683 target.detach('test2');2684 target.fire('test1');2685 target.fire('test2');2686 Y.Assert.areSame(3, count);2687 },2688 "test target.on([typeA, typeB], fn) + target.detach()": function () {2689 var count = 0,2690 target = new Y.EventTarget();2691 function increment() {2692 count++;2693 }2694 target.on(['test1', 'test2'], increment);2695 target.fire('test1');2696 target.fire('test2');2697 Y.Assert.areSame(2, count);2698 target.detach();2699 target.fire('test1');2700 target.fire('test2');2701 Y.Assert.areSame(2, count);2702 },2703 "test target.on({}) + target.detach() is harmless": function () {2704 var count = 0,2705 target = new Y.EventTarget();2706 target.on({});2707 target.detach();2708 Y.Assert.areSame(0, count);2709 },2710 "test target.on([], fn) + target.detach() is harmless": function () {2711 var count = 0,2712 target = new Y.EventTarget();2713 function increment() {2714 count++;2715 }2716 target.on([], increment);2717 target.detach();2718 Y.Assert.areSame(0, count);2719 },2720 "test target.on({}) + handle.detach() is harmless": function () {2721 var target = new Y.EventTarget(),2722 handle;2723 handle = target.on({});2724 handle.detach();2725 Y.Assert.isTrue(true);2726 },2727 "test target.on([], fn) + handle.detach() is harmless": function () {2728 var target = new Y.EventTarget(),2729 handle;2730 handle = target.on([], function () {});2731 handle.detach();2732 Y.Assert.isTrue(true);2733 },2734 "test target.on({...}) + handle.detach()": function () {2735 var count = 0,2736 target = new Y.EventTarget(),2737 handle;2738 function increment() {2739 count++;2740 }2741 handle = target.on({2742 test1: increment,2743 test2: increment2744 });2745 target.fire('test1');2746 target.fire('test2');2747 Y.Assert.areSame(2, count);2748 handle.detach();2749 target.fire('test1');2750 target.fire('test2');2751 Y.Assert.areSame(2, count);2752 },2753 "test target.on([typeA, typeB], fn) + handle.detach()": function () {2754 var count = 0,2755 target = new Y.EventTarget(),2756 handle;2757 function increment() {2758 count++;2759 }2760 handle = target.on(['test1', 'test2'], increment);2761 target.fire('test1');2762 target.fire('test2');2763 Y.Assert.areSame(2, count);2764 handle.detach();2765 target.fire('test1');2766 target.fire('test2');2767 Y.Assert.areSame(2, count);2768 },2769 "test target.on([typeA, typeA], fn) + handle.detach()": function () {2770 var count = 0,2771 target = new Y.EventTarget(),2772 handle;2773 function increment() {2774 count++;2775 }2776 handle = target.on(['test1', 'test2'], increment);2777 target.fire('test1');2778 target.fire('test2');2779 Y.Assert.areSame(2, count);2780 handle.detach();2781 target.fire('test1');2782 target.fire('test2');2783 Y.Assert.areSame(2, count);2784 },2785 "test target.on(type) + target.detach(prefix:type)": function () {2786 var target = new Y.EventTarget({ prefix: 'pre' }),2787 count = 0;2788 function increment() {2789 count++;2790 }2791 target.on('test', increment);2792 target.fire('test');2793 Y.Assert.areSame(1, count);2794 target.detach('test');2795 target.fire('test');2796 Y.Assert.areSame(1, count);2797 target.on('test', increment);2798 target.fire('test');2799 Y.Assert.areSame(2, count);2800 target.detach('pre:test');2801 target.fire('test');2802 Y.Assert.areSame(2, count);2803 },2804 "test target.after() + target.detach(type, fn)": function () {2805 var count = 0,2806 target = new Y.EventTarget();2807 function fn() {2808 count++;2809 }2810 target.after('test', fn);2811 target.fire('test');2812 Y.Assert.areSame(1, count);2813 target.detach('test', fn);2814 target.fire('test');2815 Y.Assert.areSame(1, count);2816 target.after('test', fn);2817 target.after('test', fn);2818 target.fire('test');2819 Y.Assert.areSame(3, count);2820 target.detach('test', fn);2821 target.fire('test');2822 Y.Assert.areSame(3, count);2823 },2824 "test target.after(type, fn, thisObj) + target.detach(type, fn)": function () {2825 var count = 0,2826 a = {},2827 b = {},2828 target = new Y.EventTarget();2829 function fn() {2830 count++;2831 }2832 target.after('test', fn, a);2833 target.fire('test');2834 Y.Assert.areSame(1, count);2835 target.detach('test', fn);2836 target.fire('test');2837 Y.Assert.areSame(1, count);2838 target.after('test', fn, a);2839 target.after('test', fn, b);2840 target.fire('test');2841 Y.Assert.areSame(3, count);2842 target.detach('test', fn);2843 target.fire('test');2844 Y.Assert.areSame(3, count);2845 },2846 "test target.after() + target.detach(type)": function () {2847 var count = 0,2848 target = new Y.EventTarget();2849 function fn() {2850 count++;2851 }2852 target.after('test', fn);2853 target.fire('test');2854 Y.Assert.areSame(1, count);2855 target.detach('test');2856 target.fire('test');2857 Y.Assert.areSame(1, count);2858 target.after('test', fn);2859 target.after('test', fn);2860 target.fire('test');2861 Y.Assert.areSame(3, count);2862 target.detach('test');2863 target.fire('test');2864 Y.Assert.areSame(3, count);2865 },2866 "test target.after() + target.detach()": function () {2867 var count = 0,2868 target = new Y.EventTarget();2869 function fn() {2870 count++;2871 }2872 target.after('test', fn);2873 target.fire('test');2874 Y.Assert.areSame(1, count);2875 target.detach();2876 target.fire('test');2877 Y.Assert.areSame(1, count);2878 target.after('test', fn);2879 target.after('test', fn);2880 target.fire('test');2881 Y.Assert.areSame(3, count);2882 target.detach();2883 target.fire('test');2884 Y.Assert.areSame(3, count);2885 },2886 "test target.after() + target.detachAll()": function () {2887 var count = 0,2888 target = new Y.EventTarget();2889 function fn() {2890 count++;2891 }2892 target.after('test', fn);2893 target.fire('test');2894 Y.Assert.areSame(1, count);2895 target.detachAll();2896 target.fire('test');2897 Y.Assert.areSame(1, count);2898 target.after('test', fn);2899 target.after('test', fn);2900 target.fire('test');2901 Y.Assert.areSame(3, count);2902 target.detachAll();2903 target.fire('test');2904 Y.Assert.areSame(3, count);2905 },2906 "test target.after() + handle.detach()": function () {2907 var count = 0,2908 target = new Y.EventTarget(),2909 sub;2910 function increment() {2911 count++;2912 }2913 sub = target.after('test', increment);2914 target.fire('test');2915 Y.Assert.areSame(1, count);2916 sub.detach();2917 target.fire('test');2918 Y.Assert.areSame(1, count);2919 },2920 "test target.after('cat|__', fn) + target.detach('cat|___')": function () {2921 var count = 0,2922 target = new Y.EventTarget();2923 function increment() {2924 count++;2925 }2926 target.after('cat|test', increment);2927 target.fire('test');2928 Y.Assert.areSame(1, count);2929 target.detach('cat|test');2930 target.fire('test');2931 Y.Assert.areSame(1, count);2932 },2933 "test target.after('cat|__', fn) + target.detach('cat|___', fn)": function () {2934 var count = 0,2935 target = new Y.EventTarget();2936 function increment() {2937 count++;2938 }2939 target.after('cat|test', increment);2940 target.fire('test');2941 Y.Assert.areSame(1, count);2942 target.detach('cat|test', increment);2943 target.fire('test');2944 Y.Assert.areSame(1, count);2945 },2946 "test target.after('cat|__', fn) + target.detach('cat|*')": function () {2947 var count = 0,2948 target = new Y.EventTarget();2949 function increment() {2950 count++;2951 }2952 target.after('cat|test', increment);2953 target.fire('test');2954 Y.Assert.areSame(1, count);2955 target.detach('cat|*');2956 target.fire('test');2957 Y.Assert.areSame(1, count);2958 },2959 "test target.after({...}) + target.detach(type)": function () {2960 var count = 0,2961 target = new Y.EventTarget();2962 function increment() {2963 count++;2964 }2965 target.after({2966 test1: increment,2967 test2: increment2968 });2969 target.fire('test1');2970 target.fire('test2');2971 Y.Assert.areSame(2, count);2972 target.detach('test1');2973 target.fire('test1');2974 Y.Assert.areSame(2, count);2975 target.fire('test2');2976 Y.Assert.areSame(3, count);2977 target.detach('test2');2978 target.fire('test1');2979 target.fire('test2');2980 Y.Assert.areSame(3, count);2981 },2982 "test target.after({...}) + target.detach(type, fn)": function () {2983 var count = 0,2984 target = new Y.EventTarget();2985 function increment() {2986 count++;2987 }2988 target.after({2989 test1: increment,2990 test2: increment2991 });2992 target.fire('test1');2993 target.fire('test2');2994 Y.Assert.areSame(2, count);2995 target.detach('test1', increment);2996 target.fire('test1');2997 Y.Assert.areSame(2, count);2998 target.fire('test2');2999 Y.Assert.areSame(3, count);3000 target.detach('test2', increment);3001 target.fire('test1');3002 target.fire('test2');3003 Y.Assert.areSame(3, count);3004 },3005 "test target.after({...}) + target.detachAll()": function () {3006 var count = 0,3007 target = new Y.EventTarget();3008 function increment() {3009 count++;3010 }3011 target.after({3012 test1: increment,3013 test2: increment3014 });3015 target.fire('test1');3016 target.fire('test2');3017 Y.Assert.areSame(2, count);3018 target.detachAll();3019 target.fire('test1');3020 Y.Assert.areSame(2, count);3021 target.fire('test2');3022 Y.Assert.areSame(2, count);3023 },3024 "test target.after({'cat|type': fn}) + target.detach(type, fn)": function () {3025 var count = 0,3026 target = new Y.EventTarget();3027 function increment() {3028 count++;3029 }3030 target.after({3031 'cat|test1': increment,3032 test2: increment3033 });3034 target.fire('test1');3035 target.fire('test2');3036 Y.Assert.areSame(2, count);3037 target.detach('test1');3038 target.fire('test1');3039 Y.Assert.areSame(2, count);3040 target.fire('test2');3041 Y.Assert.areSame(3, count);3042 },3043 "test target.after({'cat|type': fn}) + target.detach('cat|type')": function () {3044 var count = 0,3045 target = new Y.EventTarget();3046 function increment() {3047 count++;3048 }3049 target.after({3050 'cat|test1': increment,3051 test2: increment3052 });3053 target.fire('test1');3054 target.fire('test2');3055 Y.Assert.areSame(2, count);3056 target.detach('cat|test1');3057 target.fire('test1');3058 Y.Assert.areSame(2, count);3059 target.fire('test2');3060 Y.Assert.areSame(3, count);3061 },3062 "test target.after({'cat|type': fn}) + target.detach('cat|*')": function () {3063 var count = 0,3064 target = new Y.EventTarget();3065 function increment() {3066 count++;3067 }3068 target.after({3069 'cat|test1': increment,3070 'cat|test2': increment,3071 test3: increment3072 });3073 target.fire('test1');3074 target.fire('test2');3075 target.fire('test3');3076 Y.Assert.areSame(3, count);3077 target.detach('cat|*');3078 target.fire('test1');3079 Y.Assert.areSame(3, count);3080 target.fire('test2');3081 Y.Assert.areSame(3, count);3082 target.fire('test3');3083 Y.Assert.areSame(4, count);3084 },3085 "test target.after([type], fn) + target.detach(type, fn)": function () {3086 var count = 0,3087 target = new Y.EventTarget();3088 function increment() {3089 count++;3090 }3091 target.after(['test'], increment);3092 target.fire('test');3093 Y.Assert.areSame(1, count);3094 target.detach('test', increment);3095 target.fire('test');3096 Y.Assert.areSame(1, count);3097 },3098 "test target.after([type], fn) + target.detach(type)": function () {3099 var count = 0,3100 target = new Y.EventTarget();3101 function increment() {3102 count++;3103 }3104 target.after(['test'], increment);3105 target.fire('test');3106 Y.Assert.areSame(1, count);3107 target.detach('test');3108 target.fire('test');3109 Y.Assert.areSame(1, count);3110 },3111 "test target.after([typeA, typeB], fn) + target.detach(typeA)": function () {3112 var count = 0,3113 target = new Y.EventTarget();3114 function increment() {3115 count++;3116 }3117 target.after(['test1', 'test2'], increment);3118 target.fire('test1');3119 target.fire('test2');3120 Y.Assert.areSame(2, count);3121 target.detach('test1');3122 target.fire('test1');3123 target.fire('test2');3124 Y.Assert.areSame(3, count);3125 target.detach('test2');3126 target.fire('test1');3127 target.fire('test2');3128 Y.Assert.areSame(3, count);3129 },3130 "test target.after([typeA, typeB], fn) + target.detach()": function () {3131 var count = 0,3132 target = new Y.EventTarget();3133 function increment() {3134 count++;3135 }3136 target.after(['test1', 'test2'], increment);3137 target.fire('test1');3138 target.fire('test2');3139 Y.Assert.areSame(2, count);3140 target.detach();3141 target.fire('test1');3142 target.fire('test2');3143 Y.Assert.areSame(2, count);3144 },3145 "test target.after({}) + target.detach() is harmless": function () {3146 var count = 0,3147 target = new Y.EventTarget();3148 target.after({});3149 target.detach();3150 Y.Assert.areSame(0, count);3151 },3152 "test target.after([], fn) + target.detach() is harmless": function () {3153 var count = 0,3154 target = new Y.EventTarget();3155 function increment() {3156 count++;3157 }3158 target.after([], increment);3159 target.detach();3160 Y.Assert.areSame(0, count);3161 },3162 "test target.after({}) + handle.detach() is harmless": function () {3163 var target = new Y.EventTarget(),3164 handle;3165 handle = target.after({});3166 handle.detach();3167 Y.Assert.isTrue(true);3168 },3169 "test target.after([], fn) + handle.detach() is harmless": function () {3170 var target = new Y.EventTarget(),3171 handle;3172 handle = target.after([], function () {});3173 handle.detach();3174 Y.Assert.isTrue(true);3175 },3176 "test target.after({...}) + handle.detach()": function () {3177 var count = 0,3178 target = new Y.EventTarget(),3179 handle;3180 function increment() {3181 count++;3182 }3183 handle = target.after({3184 test1: increment,3185 test2: increment3186 });3187 target.fire('test1');3188 target.fire('test2');3189 Y.Assert.areSame(2, count);3190 handle.detach();3191 target.fire('test1');3192 target.fire('test2');3193 Y.Assert.areSame(2, count);3194 },3195 "test target.after([typeA, typeB], fn) + handle.detach()": function () {3196 var count = 0,3197 target = new Y.EventTarget(),3198 handle;3199 function increment() {3200 count++;3201 }3202 handle = target.after(['test1', 'test2'], increment);3203 target.fire('test1');3204 target.fire('test2');3205 Y.Assert.areSame(2, count);3206 handle.detach();3207 target.fire('test1');3208 target.fire('test2');3209 Y.Assert.areSame(2, count);3210 },3211 "test target.after([typeA, typeA], fn) + handle.detach()": function () {3212 var count = 0,3213 target = new Y.EventTarget(),3214 handle;3215 function increment() {3216 count++;3217 }3218 handle = target.after(['test1', 'test2'], increment);3219 target.fire('test1');3220 target.fire('test2');3221 Y.Assert.areSame(2, count);3222 handle.detach();3223 target.fire('test1');3224 target.fire('test2');3225 Y.Assert.areSame(2, count);3226 },3227 "test target.after(type) + target.detach(prefix:type)": function () {3228 var target = new Y.EventTarget({ prefix: 'pre' }),3229 count = 0;3230 function increment() {3231 count++;3232 }3233 target.after('test', increment);3234 target.fire('test');3235 Y.Assert.areSame(1, count);3236 target.detach('test');3237 target.fire('test');3238 Y.Assert.areSame(1, count);3239 target.after('test', increment);3240 target.fire('test');3241 Y.Assert.areSame(2, count);3242 target.detach('pre:test');3243 target.fire('test');3244 Y.Assert.areSame(2, count);3245 },3246 "test target.on() + target.after() + target.detach(type) detaches both": function () {3247 var target = new Y.EventTarget(),3248 count = 0;3249 function incrementOn() {3250 count++;3251 }3252 function incrementAfter() {3253 count++;3254 }3255 target.on('test', incrementOn);3256 target.after('test', incrementAfter);3257 target.fire('test');3258 Y.Assert.areSame(2, count);3259 target.detach('test');3260 target.fire('test');3261 Y.Assert.areSame(2, count);3262 },3263 "test target.detach('~AFTER~')": function () {3264 var target = new Y.EventTarget(),3265 count = 0;3266 target.after('test', function () {3267 count++;3268 });3269 target.detach('~AFTER~');3270 target.fire('test');3271 Y.Assert.areSame(1, count);3272 }3273}));3274baseSuite.add(new Y.Test.Case({3275 name: "target.fire",3276 "test target.fire() with no subscribers": function () {3277 var target = new Y.EventTarget();3278 target.fire("test1");3279 target.fire("test2", "a");3280 target.fire("test3", {});3281 target.fire("foo:test4");3282 target.fire("*:test5");3283 target.fire(":test6");3284 target.fire("|test7");3285 target.fire("~AFTER~test8");3286 target.fire("test9 test10");3287 target.fire("test11_fire");3288 Y.Assert.isTrue(true);3289 },3290 "test on() and fire() argument aggregation": function () {3291 var target = new Y.EventTarget(),3292 args;3293 function callback () {3294 args = Y.Array(arguments, 0, true);3295 }3296 target.on("test1", callback);3297 target.fire("test1");3298 Y.ArrayAssert.itemsAreSame([], args);3299 target.on("test2", callback, {});3300 target.fire("test2");3301 Y.ArrayAssert.itemsAreSame([], args);3302 target.on("test2", callback, {}, "x");3303 target.fire("test2");3304 Y.ArrayAssert.itemsAreSame(["x"], args);3305 target.on("test3", callback, {}, "x", false, null);3306 target.fire("test3");3307 Y.ArrayAssert.itemsAreSame(["x", false, null], args);3308 target.on("test4", callback);3309 target.fire("test4", "a");3310 Y.ArrayAssert.itemsAreSame(["a"], args);3311 target.on("test5", callback);3312 target.fire("test5", "a", false, null);3313 Y.ArrayAssert.itemsAreSame(["a", false, null], args);3314 target.on("test6", callback, {}, "x", false, null);3315 target.fire("test6", "a", true, target);3316 Y.ArrayAssert.itemsAreSame(["a", true, target, "x", false, null], args);3317 target.on("test6", callback, null, "x", false, null);3318 target.fire("test6", "a", true, target);3319 Y.ArrayAssert.itemsAreSame(["a", true, target, "x", false, null], args);3320 },3321 "test target.fire(*) arg is passed as is": function () {3322 var target = new Y.EventTarget(),3323 values = [3324 "a", 1.7, true, {k:"v"}, ["val"], /abc/, new Date(),3325 "", 0, false, {}, [], null, undefined],3326 received = [],3327 i, len;3328 function callback () {3329 received.push(arguments[0]);3330 }3331 target.on("test1", callback);3332 for (i = 0, len = values.length; i < len; ++i) {3333 target.fire("test1", values[i]);3334 }3335 Y.ArrayAssert.itemsAreSame(values, received);3336 received = [];3337 target.on("test2", callback, {});3338 for (i = 0, len = values.length; i < len; ++i) {3339 target.fire("test2", values[i]);3340 }3341 Y.ArrayAssert.itemsAreSame(values, received);3342 received = [];3343 target.on("test3", callback, {}, "x");3344 for (i = 0, len = values.length; i < len; ++i) {3345 target.fire("test3", values[i]);3346 }3347 Y.ArrayAssert.itemsAreSame(values, received);3348 },3349 // TODO: break out facade logic to event-custom-complex.js3350 "test broadcast": function() {3351 var o = new Y.EventTarget(), s1, s2, s3, s4;3352 o.publish('y:foo2', {3353 emitFacade: true,3354 broadcast: 13355 });3356 Y.on('y:foo2', function() {3357 //Y.log('Y foo2 executed');3358 s1 = 1;3359 });3360 Y.Global.on('y:foo2', function() {3361 //Y.log('GLOBAL foo2 executed');3362 s2 = 1;3363 });3364 o.fire('y:foo2');3365 Y.Assert.areEqual(1, s1);3366 Y.Assert.areNotEqual(1, s2);3367 s1 = 0;3368 s2 = 0;3369 o.publish('y:bar', {3370 emitFacade: true,3371 broadcast: 23372 });3373 Y.on('y:bar', function() {3374 //Y.log('Y bar executed');3375 s3 = 1;3376 });3377 Y.Global.on('y:bar', function() {3378 //Y.log('GLOBAL bar executed');3379 s4 = 1;3380 });3381 o.fire('y:bar');3382 Y.Assert.areEqual(1, s3);3383 Y.Assert.areEqual(1, s4);3384 Y.Global.on('y:bar', function(e) {3385 Y.Assert.areEqual(0, e.stopped);3386 // Y.Assert.areEqual(0, e._event.stopped);3387 //Y.log('GLOBAL bar executed');3388 e.stopPropagation();3389 });3390 o.fire('y:bar');3391 o.fire('y:bar');3392 Y.Global.detachAll();3393 }3394 // on/after lifecycle3395 // on/after prevention with return false3396}));3397baseSuite.add(new Y.Test.Case({3398 name: "target.publish()",3399 // broadcast3400 // monitored3401 // context3402 // contextFn3403 // details3404 // fireOnce3405 // async3406 // queuable3407 // silent3408 // type3409 // default configs from ET constructor3410 test_fire_once: function() {3411 var notified = 0;3412 Y.publish('fireonce', {3413 fireOnce: true3414 });3415 Y.fire('fireonce', 'foo', 'bar');3416 Y.on('fireonce', function(arg1, arg2) {3417 notified++;3418 Y.Assert.areEqual('foo', arg1, 'arg1 not correct for lazy fireOnce listener');3419 Y.Assert.areEqual('bar', arg2, 'arg2 not correct for lazy fireOnce listener');3420 });3421 Y.fire('fireonce', 'foo2', 'bar2');3422 Y.fire('fireonce', 'foo3', 'bar3');3423 global_notified = false;3424 Y.on('fireonce', function(arg1, arg2) {3425 //Y.log('the notification is asynchronous, so I need to wait for this test');3426 Y.Assert.areEqual(1, notified, 'listener notified more than once.');3427 global_notified = true;3428 });3429 // it is no longer asynchronous3430 // Y.Assert.isFalse(global_notified, 'notification was not asynchronous');3431 },3432 test_async_fireonce: function() {3433 Y.Assert.isTrue(global_notified, 'asynchronous notification did not seem to work.');3434 }3435 // node.fire("click") does not fire click subscribers3436}));3437Y.Test.Runner.add(baseSuite);3438var suite = new Y.Test.Suite("AOP");3439suite.add(new Y.Test.Case({3440 name: "Y.Do",3441 _should: {3442 fail: {3443 //"test before/after with falsy context binds args": "ticket pending"3444 },3445 ignore: {3446 "test before/after with falsy context binds args": "ticket pending", //No Asserts3447 // Trac ticket noted as value3448 "test originalRetVal not overwritten by nested call": 25300303449 }3450 },3451 "test before/after with falsy context binds args": function () {3452 //Y.Do.before(fn, obj, "method", null, "a", obj, null);3453 },3454 "test Y.AlterReturn": function() {3455 var et = new Y.EventTarget(), count = 0;3456 et.after(function() {3457 count++;3458 Y.Assert.isTrue(Y.Do.originalRetVal);3459 Y.Assert.isTrue(Y.Do.currentRetVal);3460 return new Y.Do.AlterReturn("altered return", "altered");3461 }, et, 'fire');3462 et.after(function() {3463 count++;3464 Y.Assert.isTrue(Y.Do.originalRetVal);3465 Y.Assert.areEqual("altered", Y.Do.currentRetVal);3466 }, et, 'fire');3467 et.fire('yay');3468 Y.Assert.areEqual(2, count);3469 },3470 "test originalRetVal not overwritten by nested call": function () {3471 var obj = {3472 a: function () {3473 this.b();3474 return 'A';3475 },3476 b: function () {3477 return 'B';3478 }3479 };3480 Y.Do.after(function () {3481 return Y.Do.originalRetVal.toLowerCase();3482 }, obj, 'a');3483 Y.Do.after(function () {3484 // It doesn't matter what happens here, but for example, we3485 // don't interfere with the return value3486 }, obj, 'b');3487 Y.Assert.areSame('a', obj.a());3488 }3489}));3490suite.add(new Y.Test.Case({3491 name: "EventTarget on/before/after",3492 "test target.on(fn, host, methodName)": function () {3493 var target = new Y.EventTarget(),3494 called = [],3495 handle, before;3496 before = Y.Do.before;3497 Y.Do.before = function () {3498 called.push("before");3499 return before.apply(this, arguments);3500 };3501 function callback() {3502 called.push("callback");3503 }3504 function method() {3505 called.push("method");3506 }3507 target.method = method;3508 // awkward that you have to pass the target, and even more so3509 // because this means every EventTarget is effectively an alias3510 // for Y.Do3511 handle = target.on(callback, target, "method");3512 Y.Assert.areNotSame(method, target.method);3513 Y.Assert.isObject(handle);3514 Y.Assert.isInstanceOf(Y.Do.Method, handle.evt);3515 target.method();3516 Y.ArrayAssert.itemsAreSame(["before", "callback", "method"], called);3517 // restore the method for other tests3518 Y.Do.before = before;3519 },3520 "test target.on(fn, host, methodName, context)": function () {3521 var target = new Y.EventTarget(),3522 called = [],3523 handle, before;3524 before = Y.Do.before;3525 Y.Do.before = function () {3526 called.push("before");3527 return before.apply(this, arguments);3528 };3529 function callback() {3530 called.push("callback");3531 }3532 function method() {3533 called.push("method");3534 }3535 target.method = method;3536 // awkward that you have to pass the target, and even more so3537 // because this means every EventTarget is effectively an alias3538 // for Y.Do3539 handle = target.on(callback, target, "method");3540 Y.Assert.areNotSame(method, target.method);3541 Y.Assert.isObject(handle);3542 Y.Assert.isInstanceOf(Y.Do.Method, handle.evt);3543 target.method();3544 Y.ArrayAssert.itemsAreSame(["before", "callback", "method"], called);3545 // restore the method for other tests3546 Y.Do.before = before;3547 },3548 "test target.on(fn, host, noSuchMethod)": function () {3549 },3550 "test target.on([fnA, fnB, fnC], host, noSuchMethod)": function () {3551 },3552 "test target.before() is an alias for target.on()": function () {3553 },3554 _should: {3555 ignore: {3556 "test target.on(fn, host, noSuchMethod)": true,3557 "test target.on([fnA, fnB, fnC], host, noSuchMethod)": true,3558 "test target.before() is an alias for target.on()": true3559 }3560 }3561}));3562Y.Test.Runner.add(suite);...

Full Screen

Full Screen

body.js

Source:body.js Github

copy

Full Screen

1module.exports = `2type contentfulAdvertorialType2BodyRichTextNode implements Node @derivedTypes @dontInfer {3 content: [contentfulAdvertorialType2BodyRichTextNodeContent]4 nodeType: String5 body: String6 }7 8 type contentfulAdvertorialType2BodyRichTextNodeContent @derivedTypes {9 data: contentfulAdvertorialType2BodyRichTextNodeContentData10 content: [contentfulAdvertorialType2BodyRichTextNodeContentContent]11 nodeType: String12 }13 14 type contentfulAdvertorialType2BodyRichTextNodeContentData @derivedTypes {15 target: contentfulAdvertorialType2BodyRichTextNodeContentDataTarget16 }17 18 type contentfulAdvertorialType2BodyRichTextNodeContentDataTarget @derivedTypes {19 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSys20 fields: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFields21 }22 23 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSys @derivedTypes {24 space: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysSpace25 id: String26 type: String27 createdAt: Date @dateformat28 updatedAt: Date @dateformat29 environment: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysEnvironment30 revision: Int31 contentType: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysContentType32 contentful_id: String33 }34 35 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysSpace @derivedTypes {36 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysSpaceSys37 }38 39 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysSpaceSys {40 type: String41 linkType: String42 id: String43 contentful_id: String44 }45 46 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysEnvironment @derivedTypes {47 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysEnvironmentSys48 }49 50 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysEnvironmentSys {51 id: String52 type: String53 linkType: String54 contentful_id: String55 }56 57 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysContentType @derivedTypes {58 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysContentTypeSys59 }60 61 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetSysContentTypeSys {62 type: String63 linkType: String64 id: String65 contentful_id: String66 }67 68 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFields @derivedTypes {69 image: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImage70 content: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContent71 caption: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaption72 }73 74 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImage @derivedTypes {75 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_US @proxy(from: "en-US")76 }77 78 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_US @derivedTypes {79 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSys80 fields: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFields81 }82 83 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSys @derivedTypes {84 space: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSysSpace85 id: String86 type: String87 createdAt: Date @dateformat88 updatedAt: Date @dateformat89 environment: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSysEnvironment90 revision: Int91 contentful_id: String92 }93 94 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSysSpace @derivedTypes {95 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSysSpaceSys96 }97 98 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSysSpaceSys {99 type: String100 linkType: String101 id: String102 contentful_id: String103 }104 105 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSysEnvironment @derivedTypes {106 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSysEnvironmentSys107 }108 109 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USSysEnvironmentSys {110 id: String111 type: String112 linkType: String113 contentful_id: String114 }115 116 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFields @derivedTypes {117 title: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsTitle118 file: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsFile119 }120 121 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsTitle {122 en_US: String @proxy(from: "en-US")123 }124 125 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsFile @derivedTypes {126 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsFileEn_US @proxy(from: "en-US")127 }128 129 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsFileEn_US @derivedTypes {130 url: String131 details: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsFileEn_USDetails132 fileName: String133 contentType: String134 }135 136 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsFileEn_USDetails @derivedTypes {137 size: Int138 image: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsFileEn_USDetailsImage139 }140 141 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsImageEn_USFieldsFileEn_USDetailsImage {142 width: Int143 height: Int144 }145 146 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContent @derivedTypes {147 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_US @proxy(from: "en-US")148 }149 150 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_US @derivedTypes {151 nodeType: String152 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContent]153 }154 155 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContent @derivedTypes {156 nodeType: String157 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContent]158 data: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentData159 }160 161 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContent @derivedTypes {162 nodeType: String163 value: String164 marks: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentMarks]165 data: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentData166 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentContent]167 }168 169 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentMarks {170 type: String171 }172 173 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentData @derivedTypes {174 target: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTarget175 uri: String176 }177 178 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTarget @derivedTypes {179 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSys180 fields: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFields181 }182 183 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSys @derivedTypes {184 space: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysSpace185 id: String186 type: String187 createdAt: Date @dateformat188 updatedAt: Date @dateformat189 environment: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysEnvironment190 revision: Int191 contentType: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysContentType192 contentful_id: String193 }194 195 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysSpace @derivedTypes {196 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysSpaceSys197 }198 199 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysSpaceSys {200 type: String201 linkType: String202 id: String203 contentful_id: String204 }205 206 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysEnvironment @derivedTypes {207 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysEnvironmentSys208 }209 210 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysEnvironmentSys {211 id: String212 type: String213 linkType: String214 contentful_id: String215 }216 217 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysContentType @derivedTypes {218 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysContentTypeSys219 }220 221 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetSysContentTypeSys {222 type: String223 linkType: String224 id: String225 contentful_id: String226 }227 228 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFields @derivedTypes {229 text: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsText230 colour: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsColour231 backgroundColour: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsBackgroundColour232 }233 234 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsText @derivedTypes {235 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsTextEn_US @proxy(from: "en-US")236 }237 238 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsTextEn_US @derivedTypes {239 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsTextEn_USContent]240 nodeType: String241 }242 243 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsTextEn_USContent @derivedTypes {244 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsTextEn_USContentContent]245 nodeType: String246 }247 248 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsTextEn_USContentContent @derivedTypes {249 marks: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsTextEn_USContentContentMarks]250 value: String251 nodeType: String252 }253 254 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsTextEn_USContentContentMarks {255 type: String256 }257 258 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsColour {259 en_US: String @proxy(from: "en-US")260 }261 262 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentDataTargetFieldsBackgroundColour {263 en_US: String @proxy(from: "en-US")264 }265 266 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentContent @derivedTypes {267 nodeType: String268 value: String269 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentContentContent]270 }271 272 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentContentContent @derivedTypes {273 nodeType: String274 value: String275 marks: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentContentContentMarks]276 }277 278 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentContentContentContentMarks {279 type: String280 }281 282 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentData @derivedTypes {283 target: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTarget284 }285 286 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTarget @derivedTypes {287 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSys288 fields: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFields289 }290 291 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSys @derivedTypes {292 space: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysSpace293 id: String294 type: String295 createdAt: Date @dateformat296 updatedAt: Date @dateformat297 environment: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysEnvironment298 revision: Int299 contentType: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysContentType300 contentful_id: String301 }302 303 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysSpace @derivedTypes {304 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysSpaceSys305 }306 307 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysSpaceSys {308 type: String309 linkType: String310 id: String311 contentful_id: String312 }313 314 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysEnvironment @derivedTypes {315 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysEnvironmentSys316 }317 318 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysEnvironmentSys {319 id: String320 type: String321 linkType: String322 contentful_id: String323 }324 325 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysContentType @derivedTypes {326 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysContentTypeSys327 }328 329 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetSysContentTypeSys {330 type: String331 linkType: String332 id: String333 contentful_id: String334 }335 336 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFields @derivedTypes {337 icon: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIcon338 body: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBody339 colour: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsColour340 image: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImage341 }342 343 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIcon @derivedTypes {344 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_US @proxy(from: "en-US")345 }346 347 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_US @derivedTypes {348 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSys349 fields: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFields350 }351 352 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSys @derivedTypes {353 space: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSysSpace354 id: String355 type: String356 createdAt: Date @dateformat357 updatedAt: Date @dateformat358 environment: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSysEnvironment359 revision: Int360 contentful_id: String361 }362 363 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSysSpace @derivedTypes {364 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSysSpaceSys365 }366 367 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSysSpaceSys {368 type: String369 linkType: String370 id: String371 contentful_id: String372 }373 374 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSysEnvironment @derivedTypes {375 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSysEnvironmentSys376 }377 378 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USSysEnvironmentSys {379 id: String380 type: String381 linkType: String382 contentful_id: String383 }384 385 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFields @derivedTypes {386 title: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsTitle387 file: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsFile388 }389 390 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsTitle {391 en_US: String @proxy(from: "en-US")392 }393 394 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsFile @derivedTypes {395 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsFileEn_US @proxy(from: "en-US")396 }397 398 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsFileEn_US @derivedTypes {399 url: String400 details: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsFileEn_USDetails401 fileName: String402 contentType: String403 }404 405 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsFileEn_USDetails @derivedTypes {406 size: Int407 image: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsFileEn_USDetailsImage408 }409 410 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsIconEn_USFieldsFileEn_USDetailsImage {411 width: Int412 height: Int413 }414 415 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBody @derivedTypes {416 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_US @proxy(from: "en-US")417 }418 419 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_US @derivedTypes {420 nodeType: String421 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContent]422 }423 424 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContent @derivedTypes {425 nodeType: String426 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContent]427 }428 429 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContent @derivedTypes {430 nodeType: String431 value: String432 marks: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentMarks]433 data: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentData434 }435 436 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentMarks {437 type: String438 }439 440 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentData @derivedTypes {441 target: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTarget442 }443 444 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTarget @derivedTypes {445 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSys446 fields: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFields447 }448 449 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSys @derivedTypes {450 space: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysSpace451 id: String452 type: String453 createdAt: Date @dateformat454 updatedAt: Date @dateformat455 environment: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysEnvironment456 revision: Int457 contentType: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysContentType458 contentful_id: String459 }460 461 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysSpace @derivedTypes {462 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysSpaceSys463 }464 465 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysSpaceSys {466 type: String467 linkType: String468 id: String469 contentful_id: String470 }471 472 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysEnvironment @derivedTypes {473 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysEnvironmentSys474 }475 476 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysEnvironmentSys {477 id: String478 type: String479 linkType: String480 contentful_id: String481 }482 483 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysContentType @derivedTypes {484 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysContentTypeSys485 }486 487 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetSysContentTypeSys {488 type: String489 linkType: String490 id: String491 contentful_id: String492 }493 494 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFields @derivedTypes {495 text: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsText496 colour: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsColour497 }498 499 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsText @derivedTypes {500 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsTextEn_US @proxy(from: "en-US")501 }502 503 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsTextEn_US @derivedTypes {504 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsTextEn_USContent]505 nodeType: String506 }507 508 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsTextEn_USContent @derivedTypes {509 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsTextEn_USContentContent]510 nodeType: String511 }512 513 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsTextEn_USContentContent @derivedTypes {514 marks: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsTextEn_USContentContentMarks]515 value: String516 nodeType: String517 }518 519 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsTextEn_USContentContentMarks {520 type: String521 }522 523 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsBodyEn_USContentContentDataTargetFieldsColour {524 en_US: String @proxy(from: "en-US")525 }526 527 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsColour {528 en_US: String @proxy(from: "en-US")529 }530 531 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImage @derivedTypes {532 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_US @proxy(from: "en-US")533 }534 535 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_US @derivedTypes {536 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSys537 fields: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFields538 }539 540 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSys @derivedTypes {541 space: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSysSpace542 id: String543 type: String544 createdAt: Date @dateformat545 updatedAt: Date @dateformat546 environment: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSysEnvironment547 revision: Int548 contentful_id: String549 }550 551 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSysSpace @derivedTypes {552 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSysSpaceSys553 }554 555 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSysSpaceSys {556 type: String557 linkType: String558 id: String559 contentful_id: String560 }561 562 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSysEnvironment @derivedTypes {563 sys: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSysEnvironmentSys564 }565 566 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USSysEnvironmentSys {567 id: String568 type: String569 linkType: String570 contentful_id: String571 }572 573 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFields @derivedTypes {574 title: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsTitle575 file: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsFile576 }577 578 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsTitle {579 en_US: String @proxy(from: "en-US")580 }581 582 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsFile @derivedTypes {583 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsFileEn_US @proxy(from: "en-US")584 }585 586 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsFileEn_US @derivedTypes {587 url: String588 details: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsFileEn_USDetails589 fileName: String590 contentType: String591 }592 593 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsFileEn_USDetails @derivedTypes {594 size: Int595 image: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsFileEn_USDetailsImage596 }597 598 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsContentEn_USContentDataTargetFieldsImageEn_USFieldsFileEn_USDetailsImage {599 width: Int600 height: Int601 }602 603 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaption @derivedTypes {604 en_US: contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaptionEn_US @proxy(from: "en-US")605 }606 607 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaptionEn_US @derivedTypes {608 nodeType: String609 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaptionEn_USContent]610 }611 612 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaptionEn_USContent @derivedTypes {613 nodeType: String614 content: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaptionEn_USContentContent]615 }616 617 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaptionEn_USContentContent @derivedTypes {618 nodeType: String619 value: String620 marks: [contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaptionEn_USContentContentMarks]621 }622 623 type contentfulAdvertorialType2BodyRichTextNodeContentDataTargetFieldsCaptionEn_USContentContentMarks {624 type: String625 }626 627 type contentfulAdvertorialType2BodyRichTextNodeContentContent {628 value: String629 nodeType: String630 }631 ...

Full Screen

Full Screen

engine_target.js

Source:engine_target.js Github

copy

Full Screen

1const test = require('tap').test;2const Target = require('../../src/engine/target');3const Variable = require('../../src/engine/variable');4const adapter = require('../../src/engine/adapter');5const Runtime = require('../../src/engine/runtime');6const events = require('../fixtures/events.json');7test('spec', t => {8 const target = new Target(new Runtime());9 t.type(Target, 'function');10 t.type(target, 'object');11 t.ok(target instanceof Target);12 t.type(target.id, 'string');13 t.type(target.blocks, 'object');14 t.type(target.variables, 'object');15 t.type(target.comments, 'object');16 t.type(target._customState, 'object');17 t.type(target.createVariable, 'function');18 t.type(target.renameVariable, 'function');19 t.end();20});21// Create Variable tests.22test('createVariable', t => {23 const target = new Target(new Runtime());24 target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);25 const variables = target.variables;26 t.equal(Object.keys(variables).length, 1);27 const variable = variables[Object.keys(variables)[0]];28 t.equal(variable.id, 'foo');29 t.equal(variable.name, 'bar');30 t.equal(variable.type, Variable.SCALAR_TYPE);31 t.equal(variable.value, 0);32 t.equal(variable.isCloud, false);33 t.end();34});35// Create Same Variable twice.36test('createVariable2', t => {37 const target = new Target(new Runtime());38 target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);39 target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);40 const variables = target.variables;41 t.equal(Object.keys(variables).length, 1);42 t.end();43});44// Create a list45test('createListVariable creates a list', t => {46 const target = new Target(new Runtime());47 target.createVariable('foo', 'bar', Variable.LIST_TYPE);48 const variables = target.variables;49 t.equal(Object.keys(variables).length, 1);50 const variable = variables[Object.keys(variables)[0]];51 t.equal(variable.id, 'foo');52 t.equal(variable.name, 'bar');53 t.equal(variable.type, Variable.LIST_TYPE);54 t.assert(variable.value instanceof Array, true);55 t.equal(variable.value.length, 0);56 t.equal(variable.isCloud, false);57 t.end();58});59test('createVariable calls cloud io device\'s requestCreateVariable', t => {60 const runtime = new Runtime();61 // Mock the requestCreateVariable function62 let requestCreateCloudWasCalled = false;63 runtime.ioDevices.cloud.requestCreateVariable = () => {64 requestCreateCloudWasCalled = true;65 };66 const target = new Target(runtime);67 target.isStage = true;68 target.createVariable('foo', 'bar', Variable.SCALAR_TYPE, true /* isCloud */);69 const variables = target.variables;70 t.equal(Object.keys(variables).length, 1);71 const variable = variables[Object.keys(variables)[0]];72 t.equal(variable.id, 'foo');73 t.equal(variable.name, 'bar');74 t.equal(variable.type, Variable.SCALAR_TYPE);75 t.equal(variable.value, 0);76 t.equal(variable.isCloud, true);77 t.equal(requestCreateCloudWasCalled, true);78 t.end();79});80test('createVariable does not call cloud io device\'s requestCreateVariable if target is not stage', t => {81 const runtime = new Runtime();82 // Mock the requestCreateVariable function83 let requestCreateCloudWasCalled = false;84 runtime.ioDevices.cloud.requestCreateVariable = () => {85 requestCreateCloudWasCalled = true;86 };87 const target = new Target(runtime);88 target.isStage = false;89 target.createVariable('foo', 'bar', Variable.SCALAR_TYPE, true /* isCloud */);90 const variables = target.variables;91 t.equal(Object.keys(variables).length, 1);92 const variable = variables[Object.keys(variables)[0]];93 t.equal(variable.id, 'foo');94 t.equal(variable.name, 'bar');95 t.equal(variable.type, Variable.SCALAR_TYPE);96 t.equal(variable.value, 0);97 // isCloud flag doesn't get set if the target is not the stage98 t.equal(variable.isCloud, false);99 t.equal(requestCreateCloudWasCalled, false);100 t.end();101});102test('createVariable throws when given invalid type', t => {103 const target = new Target(new Runtime());104 t.throws(105 (() => target.createVariable('foo', 'bar', 'baz')),106 new Error('Invalid variable type: baz')107 );108 t.end();109});110// Rename Variable tests.111test('renameVariable', t => {112 const target = new Target(new Runtime());113 target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);114 target.renameVariable('foo', 'bar2');115 const variables = target.variables;116 t.equal(Object.keys(variables).length, 1);117 const variable = variables[Object.keys(variables)[0]];118 t.equal(variable.id, 'foo');119 t.equal(variable.name, 'bar2');120 t.equal(variable.value, 0);121 t.equal(variable.isCloud, false);122 t.end();123});124// Rename Variable that doesn't exist.125test('renameVariable2', t => {126 const target = new Target(new Runtime());127 target.renameVariable('foo', 'bar2');128 const variables = target.variables;129 t.equal(Object.keys(variables).length, 0);130 t.end();131});132// Rename Variable that with id that exists as another variable's name.133// Expect no change.134test('renameVariable3', t => {135 const target = new Target(new Runtime());136 target.createVariable('foo1', 'foo', Variable.SCALAR_TYPE);137 target.renameVariable('foo', 'bar2');138 const variables = target.variables;139 t.equal(Object.keys(variables).length, 1);140 const variable = variables[Object.keys(variables)[0]];141 t.equal(variable.id, 'foo1');142 t.equal(variable.name, 'foo');143 t.end();144});145test('renameVariable calls cloud io device\'s requestRenameVariable function', t => {146 const runtime = new Runtime();147 let requestRenameVariableWasCalled = false;148 runtime.ioDevices.cloud.requestRenameVariable = () => {149 requestRenameVariableWasCalled = true;150 };151 const target = new Target(runtime);152 target.isStage = true;153 const mockCloudVar = new Variable('foo', 'bar', Variable.SCALAR_TYPE, true);154 target.variables[mockCloudVar.id] = mockCloudVar;155 runtime.addTarget(target);156 target.renameVariable('foo', 'bar2');157 const variables = target.variables;158 t.equal(Object.keys(variables).length, 1);159 const variable = variables[Object.keys(variables)[0]];160 t.equal(variable.id, 'foo');161 t.equal(variable.name, 'bar2');162 t.equal(variable.value, 0);163 t.equal(variable.isCloud, true);164 t.equal(requestRenameVariableWasCalled, true);165 t.end();166});167test('renameVariable does not call cloud io device\'s requestRenameVariable function if target is not stage', t => {168 const runtime = new Runtime();169 let requestRenameVariableWasCalled = false;170 runtime.ioDevices.cloud.requestRenameVariable = () => {171 requestRenameVariableWasCalled = true;172 };173 const target = new Target(runtime);174 const mockCloudVar = new Variable('foo', 'bar', Variable.SCALAR_TYPE, true);175 target.variables[mockCloudVar.id] = mockCloudVar;176 runtime.addTarget(target);177 target.renameVariable('foo', 'bar2');178 const variables = target.variables;179 t.equal(Object.keys(variables).length, 1);180 const variable = variables[Object.keys(variables)[0]];181 t.equal(variable.id, 'foo');182 t.equal(variable.name, 'bar2');183 t.equal(variable.value, 0);184 t.equal(variable.isCloud, true);185 t.equal(requestRenameVariableWasCalled, false);186 t.end();187});188// Delete Variable tests.189test('deleteVariable', t => {190 const target = new Target(new Runtime());191 target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);192 target.deleteVariable('foo');193 const variables = target.variables;194 t.equal(Object.keys(variables).length, 0);195 t.end();196});197// Delete Variable that doesn't exist.198test('deleteVariable2', t => {199 const target = new Target(new Runtime());200 target.deleteVariable('foo');201 const variables = target.variables;202 t.equal(Object.keys(variables).length, 0);203 t.end();204});205test('deleteVariable calls cloud io device\'s requestRenameVariable function', t => {206 const runtime = new Runtime();207 let requestDeleteVariableWasCalled = false;208 runtime.ioDevices.cloud.requestDeleteVariable = () => {209 requestDeleteVariableWasCalled = true;210 };211 const target = new Target(runtime);212 target.isStage = true;213 const mockCloudVar = new Variable('foo', 'bar', Variable.SCALAR_TYPE, true);214 target.variables[mockCloudVar.id] = mockCloudVar;215 runtime.addTarget(target);216 target.deleteVariable('foo');217 const variables = target.variables;218 t.equal(Object.keys(variables).length, 0);219 t.equal(requestDeleteVariableWasCalled, true);220 t.end();221});222test('deleteVariable calls cloud io device\'s requestRenameVariable function', t => {223 const runtime = new Runtime();224 let requestDeleteVariableWasCalled = false;225 runtime.ioDevices.cloud.requestDeleteVariable = () => {226 requestDeleteVariableWasCalled = true;227 };228 const target = new Target(runtime);229 const mockCloudVar = new Variable('foo', 'bar', Variable.SCALAR_TYPE, true);230 target.variables[mockCloudVar.id] = mockCloudVar;231 runtime.addTarget(target);232 target.deleteVariable('foo');233 const variables = target.variables;234 t.equal(Object.keys(variables).length, 0);235 t.equal(requestDeleteVariableWasCalled, false);236 t.end();237});238test('duplicateVariable creates a new variable with a new ID by default', t => {239 const target = new Target(new Runtime());240 target.createVariable('a var ID', 'foo', Variable.SCALAR_TYPE);241 t.equal(Object.keys(target.variables).length, 1);242 const originalVariable = target.variables['a var ID'];243 originalVariable.value = 10;244 const newVariable = target.duplicateVariable('a var ID');245 // Duplicating a variable should not add the variable to the current target246 t.equal(Object.keys(target.variables).length, 1);247 // Duplicate variable should have a different ID from the original unless specified to keep the original ID.248 t.notEqual(newVariable.id, 'a var ID');249 t.type(target.variables[newVariable.id], 'undefined');250 // Duplicate variable should start out with the same value as the original variable251 t.equal(newVariable.value, originalVariable.value);252 // Modifying one variable should not modify the other253 newVariable.value = 15;254 t.notEqual(newVariable.value, originalVariable.value);255 t.equal(originalVariable.value, 10);256 t.end();257});258test('duplicateVariable creates new array reference for list variable.value', t => {259 const target = new Target(new Runtime());260 const arr = [1, 2, 3];261 target.createVariable('a var ID', 'arr', Variable.LIST_TYPE);262 const originalVariable = target.variables['a var ID'];263 originalVariable.value = arr;264 const newVariable = target.duplicateVariable('a var ID');265 // Values are deeply equal but not the same object266 t.deepEqual(originalVariable.value, newVariable.value);267 t.notEqual(originalVariable.value, newVariable.value);268 t.end();269});270test('duplicateVariable creates a new variable with a original ID if specified', t => {271 const target = new Target(new Runtime());272 target.createVariable('a var ID', 'foo', Variable.SCALAR_TYPE);273 t.equal(Object.keys(target.variables).length, 1);274 const originalVariable = target.variables['a var ID'];275 originalVariable.value = 10;276 const newVariable = target.duplicateVariable('a var ID', true);277 // Duplicating a variable should not add the variable to the current target278 t.equal(Object.keys(target.variables).length, 1);279 // Duplicate variable should have the same ID as the original when specified280 t.equal(newVariable.id, 'a var ID');281 // Duplicate variable should start out with the same value as the original variable282 t.equal(newVariable.value, originalVariable.value);283 // Modifying one variable should not modify the other284 newVariable.value = 15;285 t.notEqual(newVariable.value, originalVariable.value);286 t.equal(originalVariable.value, 10);287 // The target should still have the original variable with the original value288 t.equal(target.variables['a var ID'].value, 10);289 t.end();290});291test('duplicateVariable returns null if variable with specified ID does not exist', t => {292 const target = new Target(new Runtime());293 const variable = target.duplicateVariable('a var ID');294 t.equal(variable, null);295 t.equal(Object.keys(target.variables).length, 0);296 target.createVariable('var id', 'foo', Variable.SCALAR_TYPE);297 t.equal(Object.keys(target.variables).length, 1);298 const anotherVariable = target.duplicateVariable('another var ID');299 t.equal(anotherVariable, null);300 t.equal(Object.keys(target.variables).length, 1);301 t.type(target.variables['another var ID'], 'undefined');302 t.type(target.variables['var id'], 'object');303 t.notEqual(target.variables['var id'], null);304 t.end();305});306test('duplicateVariables duplicates all variables', t => {307 const target = new Target(new Runtime());308 target.createVariable('var ID 1', 'var1', Variable.SCALAR_TYPE);309 target.createVariable('var ID 2', 'var2', Variable.SCALAR_TYPE);310 t.equal(Object.keys(target.variables).length, 2);311 const var1 = target.variables['var ID 1'];312 const var2 = target.variables['var ID 2'];313 var1.value = 3;314 var2.value = 'foo';315 const duplicateVariables = target.duplicateVariables();316 // Duplicating a target's variables should not change the target's own variables.317 t.equal(Object.keys(target.variables).length, 2);318 t.equal(Object.keys(duplicateVariables).length, 2);319 // Should be able to find original var IDs in both this target's variables and320 // the duplicate variables since a blocks container was not specified.321 t.equal(target.variables.hasOwnProperty('var ID 1'), true);322 t.equal(target.variables.hasOwnProperty('var ID 2'), true);323 t.equal(duplicateVariables.hasOwnProperty('var ID 1'), true);324 t.equal(duplicateVariables.hasOwnProperty('var ID 1'), true);325 // Values of the duplicate varaiables should match the value of the original values at the time of duplication326 t.equal(target.variables['var ID 1'].value, duplicateVariables['var ID 1'].value);327 t.equal(duplicateVariables['var ID 1'].value, 3);328 t.equal(target.variables['var ID 2'].value, duplicateVariables['var ID 2'].value);329 t.equal(duplicateVariables['var ID 2'].value, 'foo');330 // The two sets of variables should still be distinct, modifying the target's variables331 // should not affect the duplicated variables, and vice-versa332 var1.value = 10;333 t.equal(target.variables['var ID 1'].value, 10);334 t.equal(duplicateVariables['var ID 1'].value, 3); // should remain unchanged from initial value335 duplicateVariables['var ID 2'].value = 'bar';336 t.equal(target.variables['var ID 2'].value, 'foo');337 // Deleting a variable on the target should not change the duplicated variables338 target.deleteVariable('var ID 1');339 t.equal(Object.keys(target.variables).length, 1);340 t.equal(Object.keys(duplicateVariables).length, 2);341 t.type(duplicateVariables['var ID 1'], 'object');342 t.notEqual(duplicateVariables['var ID 1'], null);343 t.end();344});345test('duplicateVariables re-IDs variables when a block container is provided', t => {346 const target = new Target(new Runtime());347 target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);348 target.createVariable('another var id', 'var2', Variable.SCALAR_TYPE);349 // Create a block on the target which references the variable with id 'mock var id'350 target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);351 t.type(target.blocks.getBlock('a block'), 'object');352 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');353 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');354 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.value, 'a mock variable');355 // Deep clone this target's blocks to pass in to 'duplicateVariables'356 const copiedBlocks = target.blocks.duplicate();357 // The copied block should still have the same ID, and its VARIABLE field should still refer to358 // the original variable id359 t.type(copiedBlocks.getBlock('a block'), 'object');360 t.type(copiedBlocks.getBlock('a block').fields.VARIABLE, 'object');361 t.equal(copiedBlocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');362 t.equal(copiedBlocks.getBlock('a block').fields.VARIABLE.value, 'a mock variable');363 const duplicateVariables = target.duplicateVariables(copiedBlocks);364 // Duplicate variables should have new IDs365 t.equal(Object.keys(duplicateVariables).length, 2);366 t.type(duplicateVariables['mock var id'], 'undefined');367 t.type(duplicateVariables['another var id'], 'undefined');368 // Duplicate variables still have the same names..369 const dupes = Object.values(duplicateVariables);370 const dupeVarNames = dupes.map(v => v.name);371 t.notEqual(dupeVarNames.indexOf('a mock variable'), -1);372 t.notEqual(dupeVarNames.indexOf('var2'), -1);373 // Duplicating variables should not change blocks on current target374 t.type(target.blocks.getBlock('a block'), 'object');375 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');376 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.value, 'a mock variable');377 // The copied blocks passed into duplicateVariables should now reference the new378 // variable ID379 const mockVariableDupe = dupes[dupeVarNames.indexOf('a mock variable')];380 const mockVarDupeID = mockVariableDupe.id;381 t.type(copiedBlocks.getBlock('a block'), 'object');382 t.equal(copiedBlocks.getBlock('a block').fields.VARIABLE.id, mockVarDupeID);383 t.equal(copiedBlocks.getBlock('a block').fields.VARIABLE.value, 'a mock variable');384 t.end();385});386test('lookupOrCreateList creates a list if var with given id or var with given name does not exist', t => {387 const target = new Target(new Runtime());388 const variables = target.variables;389 t.equal(Object.keys(variables).length, 0);390 const listVar = target.lookupOrCreateList('foo', 'bar');391 t.equal(Object.keys(variables).length, 1);392 t.equal(listVar.id, 'foo');393 t.equal(listVar.name, 'bar');394 t.end();395});396test('lookupOrCreateList returns list if one with given id exists', t => {397 const target = new Target(new Runtime());398 const variables = target.variables;399 t.equal(Object.keys(variables).length, 0);400 target.createVariable('foo', 'bar', Variable.LIST_TYPE);401 t.equal(Object.keys(variables).length, 1);402 const listVar = target.lookupOrCreateList('foo', 'bar');403 t.equal(Object.keys(variables).length, 1);404 t.equal(listVar.id, 'foo');405 t.equal(listVar.name, 'bar');406 t.end();407});408test('lookupOrCreateList succeeds in finding list if id is incorrect but name matches', t => {409 const target = new Target(new Runtime());410 const variables = target.variables;411 t.equal(Object.keys(variables).length, 0);412 target.createVariable('foo', 'bar', Variable.LIST_TYPE);413 t.equal(Object.keys(variables).length, 1);414 const listVar = target.lookupOrCreateList('not foo', 'bar');415 t.equal(Object.keys(variables).length, 1);416 t.equal(listVar.id, 'foo');417 t.equal(listVar.name, 'bar');418 t.end();419});420test('lookupBroadcastMsg returns the var with given id if exists', t => {421 const target = new Target(new Runtime());422 const variables = target.variables;423 t.equal(Object.keys(variables).length, 0);424 target.createVariable('foo', 'bar', Variable.BROADCAST_MESSAGE_TYPE);425 t.equal(Object.keys(variables).length, 1);426 const broadcastMsg = target.lookupBroadcastMsg('foo', 'bar');427 t.equal(Object.keys(variables).length, 1);428 t.equal(broadcastMsg.id, 'foo');429 t.equal(broadcastMsg.name, 'bar');430 t.end();431});432test('createComment adds a comment to the target', t => {433 const target = new Target(new Runtime());434 const comments = target.comments;435 t.equal(Object.keys(comments).length, 0);436 target.createComment('a comment', null, 'some comment text',437 10, 20, 200, 300, true);438 t.equal(Object.keys(comments).length, 1);439 const comment = comments['a comment'];440 t.notEqual(comment, null);441 t.equal(comment.blockId, null);442 t.equal(comment.text, 'some comment text');443 t.equal(comment.x, 10);444 t.equal(comment.y, 20);445 t.equal(comment.width, 200);446 t.equal(comment.height, 300);447 t.equal(comment.minimized, true);448 t.end();449});450test('creating comment with id that already exists does not change existing comment', t => {451 const target = new Target(new Runtime());452 const comments = target.comments;453 t.equal(Object.keys(comments).length, 0);454 target.createComment('a comment', null, 'some comment text',455 10, 20, 200, 300, true);456 t.equal(Object.keys(comments).length, 1);457 target.createComment('a comment', null,458 'some new comment text', 40, 50, 300, 400, false);459 const comment = comments['a comment'];460 t.notEqual(comment, null);461 // All of the comment properties should remain unchanged from the first462 // time createComment was called463 t.equal(comment.blockId, null);464 t.equal(comment.text, 'some comment text');465 t.equal(comment.x, 10);466 t.equal(comment.y, 20);467 t.equal(comment.width, 200);468 t.equal(comment.height, 300);469 t.equal(comment.minimized, true);470 t.end();471});472test('creating a comment with a blockId also updates the comment property on the block', t => {473 const target = new Target(new Runtime());474 const comments = target.comments;475 // Create a mock block on the target476 target.blocks = {477 'a mock block': {478 id: 'a mock block'479 }480 };481 // Mock the getBlock function that's used in commentCreate482 target.blocks.getBlock = id => target.blocks[id];483 t.equal(Object.keys(comments).length, 0);484 target.createComment('a comment', 'a mock block', 'some comment text',485 10, 20, 200, 300, true);486 t.equal(Object.keys(comments).length, 1);487 const comment = comments['a comment'];488 t.equal(comment.blockId, 'a mock block');489 t.equal(target.blocks.getBlock('a mock block').comment, 'a comment');490 t.end();491});492test('fixUpVariableReferences fixes sprite global var conflicting with project global var', t => {493 const runtime = new Runtime();494 const stage = new Target(runtime);495 stage.isStage = true;496 const target = new Target(runtime);497 target.isStage = false;498 runtime.targets = [stage, target];499 // Create a global variable500 stage.createVariable('pre-existing global var id', 'a mock variable', Variable.SCALAR_TYPE);501 target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);502 t.equal(Object.keys(target.variables).length, 0);503 t.equal(Object.keys(stage.variables).length, 1);504 t.type(target.blocks.getBlock('a block'), 'object');505 t.type(target.blocks.getBlock('a block').fields, 'object');506 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');507 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');508 target.fixUpVariableReferences();509 t.equal(Object.keys(target.variables).length, 0);510 t.equal(Object.keys(stage.variables).length, 1);511 t.type(target.blocks.getBlock('a block'), 'object');512 t.type(target.blocks.getBlock('a block').fields, 'object');513 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');514 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'pre-existing global var id');515 t.end();516});517test('fixUpVariableReferences fixes sprite local var conflicting with project global var', t => {518 const runtime = new Runtime();519 const stage = new Target(runtime);520 stage.isStage = true;521 const target = new Target(runtime);522 target.isStage = false;523 target.getName = () => 'Target';524 runtime.targets = [stage, target];525 // Create a global variable526 stage.createVariable('pre-existing global var id', 'a mock variable', Variable.SCALAR_TYPE);527 target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);528 target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);529 t.equal(Object.keys(target.variables).length, 1);530 t.equal(Object.keys(stage.variables).length, 1);531 t.type(target.blocks.getBlock('a block'), 'object');532 t.type(target.blocks.getBlock('a block').fields, 'object');533 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');534 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');535 t.equal(target.variables['mock var id'].name, 'a mock variable');536 target.fixUpVariableReferences();537 t.equal(Object.keys(target.variables).length, 1);538 t.equal(Object.keys(stage.variables).length, 1);539 t.type(target.blocks.getBlock('a block'), 'object');540 t.type(target.blocks.getBlock('a block').fields, 'object');541 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');542 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');543 t.equal(target.variables['mock var id'].name, 'Target: a mock variable');544 t.end();545});546test('fixUpVariableReferences fixes conflicting sprite local var without blocks referencing var', t => {547 const runtime = new Runtime();548 const stage = new Target(runtime);549 stage.isStage = true;550 const target = new Target(runtime);551 target.isStage = false;552 target.getName = () => 'Target';553 runtime.targets = [stage, target];554 // Create a global variable555 stage.createVariable('pre-existing global var id', 'a mock variable', Variable.SCALAR_TYPE);556 target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);557 t.equal(Object.keys(target.variables).length, 1);558 t.equal(Object.keys(stage.variables).length, 1);559 t.equal(target.variables['mock var id'].name, 'a mock variable');560 target.fixUpVariableReferences();561 t.equal(Object.keys(target.variables).length, 1);562 t.equal(Object.keys(stage.variables).length, 1);563 t.equal(target.variables['mock var id'].name, 'Target: a mock variable');564 t.end();565});566test('fixUpVariableReferences fixes sprite global var conflicting with other sprite\'s local var', t => {567 const runtime = new Runtime();568 const stage = new Target(runtime);569 stage.isStage = true;570 const target = new Target(runtime);571 target.isStage = false;572 const existingTarget = new Target(runtime);573 existingTarget.isStage = false;574 runtime.targets = [stage, target, existingTarget];575 // Create a local variable on the pre-existing target576 existingTarget.createVariable('pre-existing local var id', 'a mock variable', Variable.SCALAR_TYPE);577 target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);578 t.equal(Object.keys(existingTarget.variables).length, 1);579 const existingVariable = Object.values(existingTarget.variables)[0];580 t.equal(existingVariable.name, 'a mock variable');581 t.equal(Object.keys(target.variables).length, 0);582 t.equal(Object.keys(stage.variables).length, 0);583 t.type(target.blocks.getBlock('a block'), 'object');584 t.type(target.blocks.getBlock('a block').fields, 'object');585 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');586 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');587 target.fixUpVariableReferences();588 t.equal(Object.keys(existingTarget.variables).length, 1);589 t.equal(existingVariable.name, 'a mock variable');590 t.equal(Object.keys(target.variables).length, 0);591 t.equal(Object.keys(stage.variables).length, 1);592 t.type(target.blocks.getBlock('a block'), 'object');593 t.type(target.blocks.getBlock('a block').fields, 'object');594 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');595 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');596 const newGlobal = stage.variables[Object.keys(stage.variables)[0]];597 t.equal(newGlobal.name, 'a mock variable2');598 t.end();599});600test('fixUpVariableReferences does not change variable name if there is no variable conflict', t => {601 const runtime = new Runtime();602 const stage = new Target(runtime);603 stage.isStage = true;604 const target = new Target(runtime);605 target.isStage = false;606 target.getName = () => 'Target';607 runtime.targets = [stage, target];608 // Create a global variable609 stage.createVariable('pre-existing global var id', 'a variable', Variable.SCALAR_TYPE);610 stage.createVariable('pre-existing global list id', 'a mock variable', Variable.LIST_TYPE);611 target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);612 target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);613 t.equal(Object.keys(target.variables).length, 1);614 t.equal(Object.keys(stage.variables).length, 2);615 t.type(target.blocks.getBlock('a block'), 'object');616 t.type(target.blocks.getBlock('a block').fields, 'object');617 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');618 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');619 t.equal(target.variables['mock var id'].name, 'a mock variable');620 target.fixUpVariableReferences();621 t.equal(Object.keys(target.variables).length, 1);622 t.equal(Object.keys(stage.variables).length, 2);623 t.type(target.blocks.getBlock('a block'), 'object');624 t.type(target.blocks.getBlock('a block').fields, 'object');625 t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');626 t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');627 t.equal(target.variables['mock var id'].name, 'a mock variable');628 t.end();...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful