diff --git a/packages/app-runtime/test/lib/TestUtil.ts b/packages/app-runtime/test/lib/TestUtil.ts index aca739d1c..2cebf2259 100644 --- a/packages/app-runtime/test/lib/TestUtil.ts +++ b/packages/app-runtime/test/lib/TestUtil.ts @@ -24,7 +24,7 @@ export class TestUtil { public static async createRuntime(configOverride?: any, uiBridge: IUIBridge = new FakeUIBridge(), eventBus?: EventBus): Promise { const config = this.createAppConfig(configOverride); - const nativeBootstrapper = new FakeNativeBootstrapper(); + const nativeBootstrapper = new FakeNativeBootstrapper(eventBus); await nativeBootstrapper.init(); const runtime = await AppRuntime.create(nativeBootstrapper, config, eventBus); runtime.registerUIBridge(uiBridge); diff --git a/packages/app-runtime/test/lib/natives/FakeNativeBootstrapper.ts b/packages/app-runtime/test/lib/natives/FakeNativeBootstrapper.ts index bf2580247..4b93b17d0 100644 --- a/packages/app-runtime/test/lib/natives/FakeNativeBootstrapper.ts +++ b/packages/app-runtime/test/lib/natives/FakeNativeBootstrapper.ts @@ -1,4 +1,4 @@ -import { EventEmitter2EventBus, Result } from "@js-soft/ts-utils"; +import { EventBus, EventEmitter2EventBus, Result } from "@js-soft/ts-utils"; import { WebLoggerFactory } from "@js-soft/web-logger"; import { INativeBootstrapper, INativeEnvironment } from "../../../src"; import { FakeNativeConfigAccess } from "./FakeNativeConfigAccess"; @@ -7,6 +7,8 @@ import { FakeNativeDeviceInfoAccess } from "./FakeNativeDeviceInfoAccess"; import { FakeNativeNotificationAccess } from "./FakeNativeNotificationAccess"; export class FakeNativeBootstrapper implements INativeBootstrapper { + public constructor(private readonly eventBus?: EventBus) {} + private _nativeEnvironment: INativeEnvironment; public get nativeEnvironment(): INativeEnvironment { return this._nativeEnvironment; @@ -24,9 +26,11 @@ export class FakeNativeBootstrapper implements INativeBootstrapper { configAccess: new FakeNativeConfigAccess(), databaseFactory: new FakeNativeDatabaseFactory(), deviceInfoAccess: new FakeNativeDeviceInfoAccess(), - eventBus: new EventEmitter2EventBus(() => { - // noop - }), + eventBus: + this.eventBus ?? + new EventEmitter2EventBus(() => { + // noop + }), loggerFactory, notificationAccess: new FakeNativeNotificationAccess(nativeLogger) }; diff --git a/packages/app-runtime/test/modules/PushNotification.test.ts b/packages/app-runtime/test/modules/PushNotification.test.ts index 329759aca..bde22218f 100644 --- a/packages/app-runtime/test/modules/PushNotification.test.ts +++ b/packages/app-runtime/test/modules/PushNotification.test.ts @@ -1,15 +1,16 @@ -import { sleep } from "@js-soft/ts-utils"; import { DatawalletSynchronizedEvent } from "@nmshd/runtime"; import { AppRuntime, ExternalEventReceivedEvent, LocalAccountSession, RemoteNotificationEvent, RemoteNotificationRegistrationEvent } from "../../src"; -import { TestUtil } from "../lib"; +import { MockEventBus, TestUtil } from "../lib"; describe("PushNotificationModuleTest", function () { + const eventBus = new MockEventBus(); + let runtime: AppRuntime; let session: LocalAccountSession; let devicePushIdentifier = "dummy value"; beforeAll(async function () { - runtime = await TestUtil.createRuntime(); + runtime = await TestUtil.createRuntime(undefined, undefined, eventBus); await runtime.start(); const accounts = await TestUtil.provideAccounts(runtime, 1); @@ -20,12 +21,12 @@ describe("PushNotificationModuleTest", function () { await runtime.stop(); }); + afterEach(() => eventBus.reset()); + test("should persist push identifier", async function () { runtime.nativeEnvironment.eventBus.publish(new RemoteNotificationRegistrationEvent("handleLongerThan10Characters")); - // wait for the registration to finish - // there is no event to wait for, so we just wait for a second - await sleep(1000); + await eventBus.waitForRunningEventHandlers(); const account = await runtime.accountServices.getAccount(session.account.id); expect(account.devicePushIdentifier).toBeDefined(); @@ -45,8 +46,7 @@ describe("PushNotificationModuleTest", function () { }) ); - const event = await TestUtil.awaitEvent(runtime, DatawalletSynchronizedEvent); - expect(event).toBeDefined(); + await expect(eventBus).toHavePublished(DatawalletSynchronizedEvent); }); test("should do a sync everything when ExternalEventCreated is received", async function () { @@ -61,7 +61,6 @@ describe("PushNotificationModuleTest", function () { }) ); - const event = await TestUtil.awaitEvent(runtime, ExternalEventReceivedEvent); - expect(event).toBeDefined(); + await expect(eventBus).toHavePublished(ExternalEventReceivedEvent); }); });