UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

54 lines 2.24 kB
/** * @module EventBus */ import { describe, vi, } from "vitest"; import {} from "../../../utilities/_module-exports.js"; import { TimeSpan } from "../../../utilities/_module-exports.js"; import { LazyPromise } from "../../../async/_module-exports.js"; /** * The `eventBusAdapterTestSuite` function simplifies the process of testing your custom implementation of {@link IEventBusAdapter | `IEventBusAdapter`} with vitest. * * IMPORT_PATH: `"@daiso-tech/core/event-bus/test-utilities"` * @group TestUtilities */ export function eventBusAdapterTestSuite(settings) { const { expect, test, createAdapter, beforeEach } = settings; let adapter; beforeEach(async () => { adapter = await createAdapter(); }); async function delay(time) { await LazyPromise.delay(time); } const TTL = TimeSpan.fromMilliseconds(50); describe("method: addListener, removeListener, dispatch", () => { test("Should be null when listener added and event is not triggered", async () => { const handlerFn = vi.fn((_event) => { }); await adapter.addListener("event", handlerFn); expect(handlerFn).not.toHaveBeenCalled(); }); test("Should be TestEvent when listener added and event is triggered", async () => { const handlerFn = vi.fn((_event) => { }); await adapter.addListener("event", handlerFn); const event = { type: "event", }; await adapter.dispatch("event", event); await delay(TTL); expect(handlerFn).toHaveBeenCalledTimes(1); expect(handlerFn).toHaveBeenCalledWith(event); }); test("Should be null when listener removed and event is triggered", async () => { const handlerFn = vi.fn((_event) => { }); await adapter.addListener("event", handlerFn); await adapter.removeListener("event", handlerFn); const event = { type: "event", }; await adapter.dispatch("event", event); await delay(TTL); expect(handlerFn).not.toHaveBeenCalled(); }); }); } //# sourceMappingURL=event-bus-adapter.test-suite.js.map