throw error if missing callback in event bus

This commit is contained in:
Géry Debongnie
2019-02-03 13:56:36 +01:00
parent bf7070016d
commit 14ef31483f
2 changed files with 11 additions and 0 deletions
+3
View File
@@ -35,6 +35,9 @@ export class EventBus {
* listener.
*/
on(eventType: string, owner: any, callback: Callback) {
if (!callback) {
throw new Error("Missing callback");
}
if (!this.subscriptions[eventType]) {
this.subscriptions[eventType] = [];
}
@@ -20,6 +20,14 @@ describe("event bus behaviour", () => {
bus.trigger("event");
});
test("throw error if callback is undefined", () => {
expect.assertions(1);
const bus = new EventBus();
expect(() => bus.on("event", {}, <any>undefined)).toThrow(
`Missing callback`
);
});
test("can unsubscribe", () => {
const bus = new EventBus();
let notified = false;