@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.
57 lines • 2.83 kB
JavaScript
/**
* @module EventBus
*/
import { describe, vi, } from "vitest";
import {} from "../../../event-bus/contracts/_module.js";
import {} from "../../../execution-context/contracts/_module.js";
import { NoOpExecutionContextAdapter } from "../../../execution-context/implementations/adapters/no-op-execution-context-adapter/_module.js";
import { ExecutionContext } from "../../../execution-context/implementations/derivables/_module.js";
import { TimeSpan } from "../../../time-span/implementations/_module.js";
import { delay } from "../../../utilities/_module.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: beforeEach_, context = new ExecutionContext(new NoOpExecutionContextAdapter()), } = settings;
let adapter;
const TTL = TimeSpan.fromMilliseconds(50);
describe("IEventBusAdapter tests:", () => {
beforeEach_(async () => {
adapter = await createAdapter();
});
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(context, "event", handlerFn);
await delay(TTL);
expect(handlerFn).not.toHaveBeenCalled();
});
test("Should be TestEvent when listener added and event is triggered", async () => {
const handlerFn = vi.fn((_event) => { });
await adapter.addListener(context, "event", handlerFn);
const event = {
type: "event",
};
await adapter.dispatch(context, "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(context, "event", handlerFn);
await adapter.removeListener(context, "event", handlerFn);
const event = {
type: "event",
};
await adapter.dispatch(context, "event", event);
await delay(TTL);
expect(handlerFn).not.toHaveBeenCalled();
});
});
});
}
//# sourceMappingURL=event-bus-adapter.test-suite.js.map