From 14ef31483f9d891f3bd7920757c015bdd11da498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sun, 3 Feb 2019 13:56:36 +0100 Subject: [PATCH] throw error if missing callback in event bus --- web/static/src/ts/core/event_bus.ts | 3 +++ web/static/tests/core/{bus_test.ts => event_bus.test.ts} | 8 ++++++++ 2 files changed, 11 insertions(+) rename web/static/tests/core/{bus_test.ts => event_bus.test.ts} (83%) diff --git a/web/static/src/ts/core/event_bus.ts b/web/static/src/ts/core/event_bus.ts index 23b903ad..5a224f87 100644 --- a/web/static/src/ts/core/event_bus.ts +++ b/web/static/src/ts/core/event_bus.ts @@ -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] = []; } diff --git a/web/static/tests/core/bus_test.ts b/web/static/tests/core/event_bus.test.ts similarity index 83% rename from web/static/tests/core/bus_test.ts rename to web/static/tests/core/event_bus.test.ts index 45f6efe7..4cb74ae6 100644 --- a/web/static/tests/core/bus_test.ts +++ b/web/static/tests/core/event_bus.test.ts @@ -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", {}, undefined)).toThrow( + `Missing callback` + ); + }); + test("can unsubscribe", () => { const bus = new EventBus(); let notified = false;