Merge pull request #50 from saleor/extra-test-and-debug
Add additional test and fix debug message
This commit is contained in:
commit
88119aa515
2 changed files with 36 additions and 2 deletions
|
@ -18,7 +18,7 @@ Object.defineProperty(window, "location", {
|
||||||
});
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
import { actions, DispatchResponseEvent, HandshakeEvent, AppBridge } from ".";
|
import { actions, DispatchResponseEvent, HandshakeEvent, AppBridge, ThemeEvent } from ".";
|
||||||
|
|
||||||
const handshakeEvent: HandshakeEvent = {
|
const handshakeEvent: HandshakeEvent = {
|
||||||
payload: {
|
payload: {
|
||||||
|
@ -28,6 +28,18 @@ const handshakeEvent: HandshakeEvent = {
|
||||||
type: "handshake",
|
type: "handshake",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const themeEvent: ThemeEvent = {
|
||||||
|
type: "theme",
|
||||||
|
payload: {
|
||||||
|
theme: "light",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const delay = (timeout: number) =>
|
||||||
|
new Promise((res) => {
|
||||||
|
setTimeout(res, timeout);
|
||||||
|
});
|
||||||
|
|
||||||
describe("AppBridge", () => {
|
describe("AppBridge", () => {
|
||||||
let appBridge = new AppBridge();
|
let appBridge = new AppBridge();
|
||||||
|
|
||||||
|
@ -109,6 +121,28 @@ describe("AppBridge", () => {
|
||||||
expect(appBridge.getState().token).toEqual("123");
|
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", () => {
|
it("persists domain", () => {
|
||||||
expect(appBridge.getState().domain).toEqual(domain);
|
expect(appBridge.getState().domain).toEqual(domain);
|
||||||
});
|
});
|
||||||
|
|
|
@ -254,9 +254,9 @@ export class AppBridge {
|
||||||
|
|
||||||
if (EventType[type]) {
|
if (EventType[type]) {
|
||||||
Object.getOwnPropertySymbols(this.subscribeMap[type]).forEach((key) => {
|
Object.getOwnPropertySymbols(this.subscribeMap[type]).forEach((key) => {
|
||||||
|
debug("Executing listener for event: %s and payload %j", type, payload);
|
||||||
// @ts-ignore fixme
|
// @ts-ignore fixme
|
||||||
this.subscribeMap[type][key](payload);
|
this.subscribeMap[type][key](payload);
|
||||||
debug("Setting listener for event: %s and payload %j", type, payload);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue