2015-10-27 21:40:52 +00:00
|
|
|
define(["Test", "Tone/core/Emitter"], function (Test, Emitter) {
|
2015-08-18 20:32:08 +00:00
|
|
|
|
2015-10-27 21:40:52 +00:00
|
|
|
describe("Emitter", function(){
|
2015-08-18 20:32:08 +00:00
|
|
|
|
2017-12-30 16:26:29 +00:00
|
|
|
it("can be created and disposed", function(){
|
2015-10-27 21:40:52 +00:00
|
|
|
var emitter = new Emitter();
|
2015-08-18 20:32:08 +00:00
|
|
|
emitter.dispose();
|
|
|
|
Test.wasDisposed(emitter);
|
|
|
|
});
|
|
|
|
|
2017-12-30 16:26:29 +00:00
|
|
|
it("can bind events", function(done){
|
2015-10-27 21:40:52 +00:00
|
|
|
var emitter = new Emitter();
|
2015-08-18 20:32:08 +00:00
|
|
|
emitter.on("something", function(){
|
|
|
|
done();
|
|
|
|
emitter.dispose();
|
|
|
|
});
|
2016-09-19 00:07:00 +00:00
|
|
|
emitter.emit("something");
|
2017-12-15 16:11:14 +00:00
|
|
|
emitter.dispose();
|
2015-08-18 20:32:08 +00:00
|
|
|
});
|
|
|
|
|
2017-12-30 16:26:29 +00:00
|
|
|
it("can unbind events", function(){
|
2015-10-27 21:40:52 +00:00
|
|
|
var emitter = new Emitter();
|
2015-08-18 20:32:08 +00:00
|
|
|
var callback = function(){
|
|
|
|
throw new Error("should call this");
|
|
|
|
};
|
|
|
|
emitter.on("something", callback);
|
|
|
|
emitter.off("something", callback);
|
2016-09-19 00:07:00 +00:00
|
|
|
emitter.emit("something");
|
2015-08-18 20:32:08 +00:00
|
|
|
emitter.dispose();
|
|
|
|
});
|
|
|
|
|
2017-12-30 16:26:29 +00:00
|
|
|
it("removes all events when no callback is given", function(){
|
2015-10-27 21:40:52 +00:00
|
|
|
var emitter = new Emitter();
|
2015-09-05 19:07:06 +00:00
|
|
|
emitter.on("something", function(){
|
2017-12-15 16:11:14 +00:00
|
|
|
throw new Error("should call this");
|
2015-09-05 19:07:06 +00:00
|
|
|
});
|
|
|
|
emitter.on("something", function(){
|
2017-12-15 16:11:14 +00:00
|
|
|
throw new Error("should call this");
|
2015-09-05 19:07:06 +00:00
|
|
|
});
|
|
|
|
emitter.off("something");
|
2016-09-19 00:07:00 +00:00
|
|
|
emitter.emit("something");
|
2017-12-15 16:11:14 +00:00
|
|
|
emitter.off("something-else");
|
|
|
|
emitter.dispose();
|
|
|
|
});
|
|
|
|
|
2017-12-30 16:26:29 +00:00
|
|
|
it("can remove an event while emitting", function(done){
|
2017-12-15 16:11:14 +00:00
|
|
|
var emitter = new Emitter();
|
|
|
|
emitter.on("something", function(){
|
2017-12-30 16:26:29 +00:00
|
|
|
emitter.off("something");
|
2017-12-15 16:11:14 +00:00
|
|
|
});
|
|
|
|
emitter.on("something-else", function(){
|
|
|
|
emitter.dispose();
|
2017-12-30 16:26:29 +00:00
|
|
|
done();
|
2017-12-15 16:11:14 +00:00
|
|
|
});
|
|
|
|
emitter.emit("something");
|
|
|
|
emitter.emit("something-else");
|
|
|
|
});
|
|
|
|
|
2017-12-30 16:26:29 +00:00
|
|
|
it("can invoke an event once", function(){
|
2017-12-15 16:11:14 +00:00
|
|
|
var emitter = new Emitter();
|
|
|
|
emitter.once("something", function(val){
|
2017-12-30 16:26:29 +00:00
|
|
|
expect(val).to.equal(1);
|
2017-12-15 16:11:14 +00:00
|
|
|
});
|
|
|
|
emitter.emit("something", 1);
|
|
|
|
emitter.emit("something", 2);
|
2015-09-05 19:07:06 +00:00
|
|
|
emitter.dispose();
|
|
|
|
});
|
|
|
|
|
2017-12-30 16:26:29 +00:00
|
|
|
it("can pass arguments to the callback", function(done){
|
2015-10-27 21:40:52 +00:00
|
|
|
var emitter = new Emitter();
|
2015-08-18 20:32:08 +00:00
|
|
|
emitter.on("something", function(arg0, arg1){
|
|
|
|
expect(arg0).to.equal("A");
|
|
|
|
expect(arg1).to.equal("B");
|
|
|
|
emitter.dispose();
|
|
|
|
done();
|
|
|
|
});
|
2016-09-19 00:07:00 +00:00
|
|
|
emitter.emit("something", "A", "B");
|
2015-08-18 20:32:08 +00:00
|
|
|
});
|
|
|
|
|
2017-12-30 16:26:29 +00:00
|
|
|
it("can mixin its methods to another object", function(done){
|
2015-08-18 20:32:08 +00:00
|
|
|
var emitter = {};
|
2015-10-27 21:40:52 +00:00
|
|
|
Emitter.mixin(emitter);
|
2015-08-18 20:32:08 +00:00
|
|
|
emitter.on("test", done);
|
2016-09-19 00:07:00 +00:00
|
|
|
emitter.emit("test");
|
2015-08-18 20:32:08 +00:00
|
|
|
});
|
|
|
|
});
|
2017-12-15 16:11:14 +00:00
|
|
|
});
|