Add additional test and fix debug message

This commit is contained in:
Lukasz Ostrowski 2022-09-06 16:02:24 +02:00
parent 3757f9abaa
commit 423495970b
2 changed files with 36 additions and 2 deletions

View file

@ -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);
});

View file

@ -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);
});
}
}