Skip to content

Commit

Permalink
fix: onramp listener function bug (#1728)
Browse files Browse the repository at this point in the history
Co-authored-by: Alissa Crane <[email protected]>
  • Loading branch information
abcrane123 and alissacrane-cb authored Dec 13, 2024
1 parent f7f3ff4 commit cb9535e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
50 changes: 30 additions & 20 deletions src/fund/utils/subscribeToWindowMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 2 additions & 9 deletions src/fund/utils/subscribeToWindowMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -36,12 +29,12 @@ export function subscribeToWindowMessage({
allowedOrigin: string;
onValidateOrigin?: (origin: string) => Promise<boolean>;
}) {
const handleMessage = (event: MessageEvent<PostMessageData>) => {
const handleMessage = (event: MessageEvent<string>) => {
if (!isAllowedOrigin({ event, allowedOrigin })) {
return;
}

const { eventName, data } = event.data;
const { eventName, data } = JSON.parse(event.data);

if (eventName === 'event') {
(async () => {
Expand Down

0 comments on commit cb9535e

Please sign in to comment.