Skip to content

Commit

Permalink
Simplify the PushNotificationModuleTest (#354)
Browse files Browse the repository at this point in the history
* chore: make native eventbus configurable

* test: simplify PushNotificationModuleTest

* fix: commit stuff
  • Loading branch information
jkoenig134 authored Dec 2, 2024
1 parent 95dc437 commit 94bccd4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/app-runtime/test/lib/TestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class TestUtil {
public static async createRuntime(configOverride?: any, uiBridge: IUIBridge = new FakeUIBridge(), eventBus?: EventBus): Promise<AppRuntime> {
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);
Expand Down
12 changes: 8 additions & 4 deletions packages/app-runtime/test/lib/natives/FakeNativeBootstrapper.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand All @@ -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)
};
Expand Down
19 changes: 9 additions & 10 deletions packages/app-runtime/test/modules/PushNotification.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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();
Expand All @@ -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 () {
Expand All @@ -61,7 +61,6 @@ describe("PushNotificationModuleTest", function () {
})
);

const event = await TestUtil.awaitEvent(runtime, ExternalEventReceivedEvent);
expect(event).toBeDefined();
await expect(eventBus).toHavePublished(ExternalEventReceivedEvent);
});
});

0 comments on commit 94bccd4

Please sign in to comment.