From 423495970bafadc57257dbc2377ba27d0254edcb Mon Sep 17 00:00:00 2001 From: Lukasz Ostrowski Date: Tue, 6 Sep 2022 16:02:24 +0200 Subject: [PATCH] Add additional test and fix debug message --- src/app-bridge/app-bridge.test.ts | 36 ++++++++++++++++++++++++++++++- src/app-bridge/app-bridge.ts | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/app-bridge/app-bridge.test.ts b/src/app-bridge/app-bridge.test.ts index 87fffcd..845fa46 100644 --- a/src/app-bridge/app-bridge.test.ts +++ b/src/app-bridge/app-bridge.test.ts @@ -18,7 +18,7 @@ Object.defineProperty(window, "location", { }); // eslint-disable-next-line -import { actions, DispatchResponseEvent, HandshakeEvent, AppBridge } from "."; +import { actions, DispatchResponseEvent, HandshakeEvent, AppBridge, ThemeEvent } from "."; const handshakeEvent: HandshakeEvent = { payload: { @@ -28,6 +28,18 @@ const handshakeEvent: HandshakeEvent = { type: "handshake", }; +const themeEvent: ThemeEvent = { + type: "theme", + payload: { + theme: "light", + }, +}; + +const delay = (timeout: number) => + new Promise((res) => { + setTimeout(res, timeout); + }); + describe("AppBridge", () => { let appBridge = new AppBridge(); @@ -109,6 +121,28 @@ describe("AppBridge", () => { expect(appBridge.getState().token).toEqual("123"); }); + it("Subscribes to theme change event and runs callback with new value after delay", async () => { + expect.assertions(2); + const callback = vi.fn(); + + const unsubscribe = appBridge.subscribe("theme", callback); + + await delay(200); + + fireEvent( + window, + new MessageEvent("message", { + data: themeEvent, + origin, + }) + ); + + expect(callback).toHaveBeenCalledOnce(); + expect(callback).toHaveBeenCalledWith({ theme: "light" }); + + unsubscribe(); + }); + it("persists domain", () => { expect(appBridge.getState().domain).toEqual(domain); }); diff --git a/src/app-bridge/app-bridge.ts b/src/app-bridge/app-bridge.ts index 4900f32..6c417eb 100644 --- a/src/app-bridge/app-bridge.ts +++ b/src/app-bridge/app-bridge.ts @@ -254,9 +254,9 @@ export class AppBridge { if (EventType[type]) { Object.getOwnPropertySymbols(this.subscribeMap[type]).forEach((key) => { + debug("Executing listener for event: %s and payload %j", type, payload); // @ts-ignore fixme this.subscribeMap[type][key](payload); - debug("Setting listener for event: %s and payload %j", type, payload); }); } }