Skip to content

Commit

Permalink
test(messaging): add transfer non-serializable data case
Browse files Browse the repository at this point in the history
  • Loading branch information
1natsu172 committed Nov 3, 2024
1 parent 5bccb4a commit 58b78d3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/messaging/src/__tests__/browser/page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ describe.each<
);
});

it('should throw an error if the responder sends non-serializable data', async () => {
interface MessageSchema {
test(data: string): { getName: () => string };
}
const messenger1 = defineTestMessaging<MessageSchema>();
const messenger2 = defineTestMessaging<MessageSchema>();
const invalidFn = () => 'testUser1';
const onMessage = vi.fn().mockReturnValue({ getName: invalidFn });

messenger2.onMessage('test', onMessage);

await expect(messenger1.sendMessage('test', 'data', ...sendArgs)).rejects.toThrowError(
`Failed to execute 'structuredClone' on 'Window': ${invalidFn.toString()} could not be cloned.`,
);
});

it('should throw an error if the sender sends non-serializable data', async () => {
interface MessageSchema {
test(data: { getName: () => string }): boolean;
}
const messenger1 = defineTestMessaging<MessageSchema>();
const messenger2 = defineTestMessaging<MessageSchema>();
const invalidFn = () => 'testUser1';

messenger2.onMessage('test', () => true);

await expect(
messenger1.sendMessage('test', { getName: invalidFn }, ...sendArgs),
).rejects.toThrowError(
`Failed to execute 'structuredClone' on 'Window': ${invalidFn.toString()} could not be cloned.`,
);
});

it('should be messaging for the same message type between different instances', async () => {
interface MessageSchema {
test(data: string): string;
Expand Down

0 comments on commit 58b78d3

Please sign in to comment.