Tone.js/test/core/Emitter.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

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();
});
emitter.emit("something");
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);
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(){
throw new Error("should call this");
2015-09-05 19:07:06 +00:00
});
emitter.on("something", function(){
throw new Error("should call this");
2015-09-05 19:07:06 +00:00
});
emitter.off("something");
emitter.emit("something");
emitter.off("something-else");
emitter.dispose();
});
2017-12-30 16:26:29 +00:00
it("can remove an event while emitting", function(done){
var emitter = new Emitter();
emitter.on("something", function(){
2017-12-30 16:26:29 +00:00
emitter.off("something");
});
emitter.on("something-else", function(){
emitter.dispose();
2017-12-30 16:26:29 +00:00
done();
});
emitter.emit("something");
emitter.emit("something-else");
});
2017-12-30 16:26:29 +00:00
it("can invoke an event once", function(){
var emitter = new Emitter();
emitter.once("something", function(val){
2017-12-30 16:26:29 +00:00
expect(val).to.equal(1);
});
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();
});
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);
emitter.emit("test");
2015-08-18 20:32:08 +00:00
});
});
});