Skip to content

Instantly share code, notes, and snippets.

@keblodev
Created June 1, 2015 00:46
Show Gist options
  • Save keblodev/8c7e53e5a560622c93f3 to your computer and use it in GitHub Desktop.
Save keblodev/8c7e53e5a560622c93f3 to your computer and use it in GitHub Desktop.
Tests for EventEmmiterClass
//////////////////////// EventEmitter tests
console.info(" --- TESTING START \n");
//textObjClass
var SomeObjClass = function() {
'use strict';
var _someObjClass = {
events: {
someEvent: 'someEvent'
},
init: function() {
}
};
_someObjClass.init.call(_someObjClass);
return _someObjClass;
};
//simpleExtend
var extend = function(targetObj, extendWithObj) {
'use strict';
for (var key in extendWithObj) {
if (extendWithObj.hasOwnProperty(key)) {
targetObj[key] = extendWithObj[key];
}
}
return targetObj;
};
var someObjClassInstance = extend(new EventEmitterClass(), new SomeObjClass());
var thatContext = {
testProp: 2
}
var fun1 = function() {
console.log('fun1');
},
fun2 = function() {
console.log('fun2');
},
funWithContext = function() {
console.log('funWithContext');
console.log(' testProp = ' + this.testProp);
};
someObjClassInstance.addEventListener(someObjClassInstance.events.someEvent, fun1);
someObjClassInstance.addEventListener(someObjClassInstance.events.someEvent, fun2);
someObjClassInstance.addEventListener(someObjClassInstance.events.someEvent, funWithContext, thatContext);
console.log(" --- Triggering event test START");
someObjClassInstance.trigger(someObjClassInstance.events.someEvent);
console.log(" --- Triggering event test END\n");
console.log(" --- Triggering event after one listener removed test START");
someObjClassInstance.removeEventListener(fun1);
someObjClassInstance.trigger(someObjClassInstance.events.someEvent);
console.log(" --- Triggering event after one listener removed test END\n");
console.log(" --- Triggering event after all listeners removed test START");
someObjClassInstance.removeEventListener(someObjClassInstance.events.someEvent, true);
someObjClassInstance.trigger(someObjClassInstance.events.someEvent);
console.info(" here should nothing, but THIS message");
console.log(" --- Triggering event after all listeners removed test END\n");
console.log(" --- Triggering event after one listener removed by event test START");
someObjClassInstance.addEventListener(someObjClassInstance.events.someEvent, fun1);
someObjClassInstance.trigger(someObjClassInstance.events.someEvent);
someObjClassInstance.removeEventListener(someObjClassInstance.events.someEvent, fun1);
someObjClassInstance.trigger(someObjClassInstance.events.someEvent);
console.log(" --- Triggering event after one listener removed by event test END\n");
console.log(" --- Passing nothing to removeEventListener START");
someObjClassInstance.removeEventListener();
console.log(" --- Passing nothing to removeEventListener END\n");
console.log(" --- Passing invalid argument to removeEventListener START");
try {
someObjClassInstance.removeEventListener({});
} catch(e) {
console.error(e);
}
console.log(" --- Passing nothing to removeEventListener END\n");
console.log(" --- Passing no-binded to event callback to removeEventListener START");
try {
someObjClassInstance.removeEventListener(fun1);
} catch(e) {
console.error(e);
}
console.log(" --- Passing no-binded to event callback to removeEventListener END\n");
console.log(" --- Passing just event name to removeEventListener START");
try {
someObjClassInstance.removeEventListener(someObjClassInstance.events.someEvent);
} catch(e) {
console.error(e);
}
console.log(" --- Passing just event name to removeEventListener END\n");
console.log(" --- Passing event name with no-binded to event callback to removeEventListener START");
someObjClassInstance.removeEventListener(someObjClassInstance.events.someEvent, fun1);
console.log(" --- Passing event name with no-binded to event callback to removeEventListener END\n");
console.log(" --- Passing just event name to addEventListener START");
try {
someObjClassInstance.addEventListener(someObjClassInstance.events.someEvent);
} catch(e) {
console.error(e);
}
console.log(" --- Passing just event name to addEventListener END\n");
console.log(" --- Passing just callback to addEventListener START");
try {
someObjClassInstance.addEventListener(fun1);
} catch(e) {
console.error(e);
}
console.log(" --- Passing just callback to addEventListener END\n");
console.info(" --- TESTING FINISH \n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment