|  | 
| 1 | 1 | import { batched, EventBus, htmlEscape, markup } from "../src/runtime/utils"; | 
| 2 |  | -import { nextMicroTick } from "./helpers"; | 
|  | 2 | +import { makeTestFixture, nextMicroTick } from "./helpers"; | 
|  | 3 | +import { getCurrent } from "../src/runtime/component_node"; | 
|  | 4 | +import { Component, mount, xml } from "../src"; | 
| 3 | 5 | 
 | 
| 4 | 6 | describe("event bus behaviour", () => { | 
| 5 | 7 |   test("can subscribe and be notified", () => { | 
| @@ -33,6 +35,66 @@ describe("event bus behaviour", () => { | 
| 33 | 35 |     bus.addEventListener("event", (ev: any) => expect(ev.detail).toBe("hello world")); | 
| 34 | 36 |     bus.trigger("event", "hello world"); | 
| 35 | 37 |   }); | 
|  | 38 | + | 
|  | 39 | +  test("events are not validated if the bus is created outside of dev mode", async () => { | 
|  | 40 | +    let bus_empty: EventBus | null = null; | 
|  | 41 | +    class Root extends Component { | 
|  | 42 | +      static template = xml`<div/>`; | 
|  | 43 | + | 
|  | 44 | +      setup() { | 
|  | 45 | +        getCurrent(); // checks that we're in a component context | 
|  | 46 | + | 
|  | 47 | +        bus_empty = new EventBus([]); | 
|  | 48 | +      } | 
|  | 49 | +    } | 
|  | 50 | +    await mount(Root, makeTestFixture()); | 
|  | 51 | + | 
|  | 52 | +    bus_empty!.addEventListener("a", () => {}); | 
|  | 53 | +    bus_empty!.trigger("a"); | 
|  | 54 | +    bus_empty!.dispatchEvent(new CustomEvent("a")); | 
|  | 55 | +  }); | 
|  | 56 | +  test("events are validated if the bus is created in dev mode & events are provided", async () => { | 
|  | 57 | +    let bus: EventBus | null = null; | 
|  | 58 | +    let bus_empty: EventBus | null = null; | 
|  | 59 | +    let bbus_no_validation: EventBus | null = null; | 
|  | 60 | +    class Root extends Component { | 
|  | 61 | +      static template = xml`<div/>`; | 
|  | 62 | + | 
|  | 63 | +      setup() { | 
|  | 64 | +        getCurrent(); // checks that we're in a component context | 
|  | 65 | + | 
|  | 66 | +        bus = new EventBus(["a", "b"]); | 
|  | 67 | +        bus_empty = new EventBus([]); | 
|  | 68 | +        bbus_no_validation = new EventBus(); | 
|  | 69 | +      } | 
|  | 70 | +    } | 
|  | 71 | + | 
|  | 72 | +    await mount(Root, makeTestFixture(), { test: true }); | 
|  | 73 | + | 
|  | 74 | +    bbus_no_validation!.addEventListener("c", () => {}); | 
|  | 75 | +    bbus_no_validation!.trigger("c"); | 
|  | 76 | +    bbus_no_validation!.dispatchEvent(new CustomEvent("c")); | 
|  | 77 | + | 
|  | 78 | +    bus!.addEventListener("a", () => {}); | 
|  | 79 | +    bus!.trigger("a"); | 
|  | 80 | +    bus!.dispatchEvent(new CustomEvent("a")); | 
|  | 81 | + | 
|  | 82 | +    expect(() => bus!.addEventListener("c", () => {})).toThrow( | 
|  | 83 | +      "EventBus: subscribing to unknown event 'c'" | 
|  | 84 | +    ); | 
|  | 85 | +    expect(() => bus!.trigger("c")).toThrow("EventBus: triggering unknown event 'c'"); | 
|  | 86 | +    expect(() => bus!.dispatchEvent(new CustomEvent("c"))).toThrow( | 
|  | 87 | +      "EventBus: dispatching unknown event 'c'" | 
|  | 88 | +    ); | 
|  | 89 | + | 
|  | 90 | +    expect(() => bus_empty!.addEventListener("a", () => {})).toThrow( | 
|  | 91 | +      "EventBus: subscribing to unknown event 'a'" | 
|  | 92 | +    ); | 
|  | 93 | +    expect(() => bus_empty!.trigger("a")).toThrow("EventBus: triggering unknown event 'a'"); | 
|  | 94 | +    expect(() => bus_empty!.dispatchEvent(new CustomEvent("a"))).toThrow( | 
|  | 95 | +      "EventBus: dispatching unknown event 'a'" | 
|  | 96 | +    ); | 
|  | 97 | +  }); | 
| 36 | 98 | }); | 
| 37 | 99 | 
 | 
| 38 | 100 | describe("batched", () => { | 
|  | 
0 commit comments