2019-07-14 22:18:00 +02:00
|
|
|
import { EventBus } from "../../src/core/event_bus";
|
2019-01-24 10:24:59 +01:00
|
|
|
|
|
|
|
|
describe("event bus behaviour", () => {
|
|
|
|
|
test("can subscribe and be notified", () => {
|
2019-01-24 13:17:40 +01:00
|
|
|
const bus = new EventBus();
|
2019-01-24 10:24:59 +01:00
|
|
|
let notified = false;
|
|
|
|
|
bus.on("event", {}, () => (notified = true));
|
|
|
|
|
expect(notified).toBe(false);
|
|
|
|
|
bus.trigger("event");
|
|
|
|
|
expect(notified).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-24 10:58:46 +01:00
|
|
|
test("callbacks are called with proper 'this'", () => {
|
|
|
|
|
expect.assertions(1);
|
2019-01-24 13:17:40 +01:00
|
|
|
const bus = new EventBus();
|
2019-01-24 10:58:46 +01:00
|
|
|
const owner = {};
|
2020-04-21 15:08:53 +02:00
|
|
|
bus.on("event", owner, function (this: any) {
|
2019-01-24 10:58:46 +01:00
|
|
|
expect(this).toBe(owner);
|
|
|
|
|
});
|
|
|
|
|
bus.trigger("event");
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-03 13:56:36 +01:00
|
|
|
test("throw error if callback is undefined", () => {
|
|
|
|
|
expect.assertions(1);
|
|
|
|
|
const bus = new EventBus();
|
2019-06-28 10:32:03 +02:00
|
|
|
expect(() => bus.on("event", {}, <any>undefined)).toThrow(`Missing callback`);
|
2019-02-03 13:56:36 +01:00
|
|
|
});
|
|
|
|
|
|
2019-01-24 10:24:59 +01:00
|
|
|
test("can unsubscribe", () => {
|
2019-01-24 13:17:40 +01:00
|
|
|
const bus = new EventBus();
|
2019-01-24 10:24:59 +01:00
|
|
|
let notified = false;
|
|
|
|
|
let owner = {};
|
|
|
|
|
bus.on("event", owner, () => (notified = true));
|
|
|
|
|
bus.off("event", owner);
|
|
|
|
|
bus.trigger("event");
|
|
|
|
|
expect(notified).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("arguments are properly propagated", () => {
|
|
|
|
|
expect.assertions(1);
|
2019-01-24 13:17:40 +01:00
|
|
|
const bus = new EventBus();
|
2019-01-24 10:24:59 +01:00
|
|
|
bus.on("event", {}, (arg: any) => expect(arg).toBe("hello world"));
|
|
|
|
|
bus.trigger("event", "hello world");
|
|
|
|
|
});
|
|
|
|
|
});
|