diff --git a/src/fund/utils/subscribeToWindowMessage.test.ts b/src/fund/utils/subscribeToWindowMessage.test.ts index 2f3be349dc..fd0b99db26 100644 --- a/src/fund/utils/subscribeToWindowMessage.test.ts +++ b/src/fund/utils/subscribeToWindowMessage.test.ts @@ -26,10 +26,12 @@ describe('subscribeToWindowMessage', () => { allowedOrigin: DEFAULT_ORIGIN, }); - const event = mockMessageEvent({ - eventName: MessageCodes.Event, - data: { key: 'value' }, - }); + const event = mockMessageEvent( + JSON.stringify({ + eventName: MessageCodes.Event, + data: { key: 'value' }, + }), + ); window.dispatchEvent(event); //wait for the async code to run @@ -45,10 +47,12 @@ describe('subscribeToWindowMessage', () => { allowedOrigin: 'https://not.allowed.origin', }); - const event = mockMessageEvent({ - eventName: MessageCodes.Event, - data: { key: 'value' }, - }); + const event = mockMessageEvent( + JSON.stringify({ + eventName: MessageCodes.Event, + data: { key: 'value' }, + }), + ); window.dispatchEvent(event); //wait for the async code to run @@ -66,10 +70,12 @@ describe('subscribeToWindowMessage', () => { onValidateOrigin, }); - const event = mockMessageEvent({ - eventName: MessageCodes.Event, - data: { key: 'value' }, - }); + const event = mockMessageEvent( + JSON.stringify({ + eventName: MessageCodes.Event, + data: { key: 'value' }, + }), + ); window.dispatchEvent(event); //wait for the async code to run @@ -88,10 +94,12 @@ describe('subscribeToWindowMessage', () => { onValidateOrigin, }); - const event = mockMessageEvent({ - eventName: MessageCodes.Event, - data: { key: 'value' }, - }); + const event = mockMessageEvent( + JSON.stringify({ + eventName: MessageCodes.Event, + data: { key: 'value' }, + }), + ); window.dispatchEvent(event); //wait for the async code to run @@ -108,10 +116,12 @@ describe('subscribeToWindowMessage', () => { allowedOrigin: DEFAULT_ORIGIN, }); - const event = mockMessageEvent({ - eventName: 'not-event', - data: { key: 'value' }, - }); + const event = mockMessageEvent( + JSON.stringify({ + eventName: 'not-event', + data: { key: 'value' }, + }), + ); window.dispatchEvent(event); //wait for the async code to run diff --git a/src/fund/utils/subscribeToWindowMessage.ts b/src/fund/utils/subscribeToWindowMessage.ts index 55edd8edaf..a0399f1090 100644 --- a/src/fund/utils/subscribeToWindowMessage.ts +++ b/src/fund/utils/subscribeToWindowMessage.ts @@ -10,15 +10,8 @@ export enum MessageCodes { Event = 'event', } -type MessageCode = `${MessageCodes}`; - type MessageData = JsonObject; -type PostMessageData = { - eventName: MessageCode; - data?: MessageData; -}; - /** * Subscribes to a message from the parent window. * @param messageCode A message code to subscribe to. @@ -36,12 +29,12 @@ export function subscribeToWindowMessage({ allowedOrigin: string; onValidateOrigin?: (origin: string) => Promise; }) { - const handleMessage = (event: MessageEvent) => { + const handleMessage = (event: MessageEvent) => { if (!isAllowedOrigin({ event, allowedOrigin })) { return; } - const { eventName, data } = event.data; + const { eventName, data } = JSON.parse(event.data); if (eventName === 'event') { (async () => {