Skip to content

Commit

Permalink
appease the coverage gods
Browse files Browse the repository at this point in the history
  • Loading branch information
suddjian committed Feb 12, 2022
1 parent 855e615 commit 955766b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ describe('comms', () => {
console.error = originalConsoleError;
});

it('constructs with defaults', () => {
const sb = new Switchboard({ port: new MessageChannel().port1 });
expect(sb).not.toBeNull();
expect(sb).toHaveProperty('name');
expect(sb).toHaveProperty('debugMode');
});

describe('emit', () => {
it('triggers the method', async () => {
const channel = new MessageChannel();
Expand Down Expand Up @@ -248,6 +255,22 @@ describe('comms', () => {
'[theirs] Method "failing" threw an error',
);
});

it('handles receiving an unexpected non-reply, non-error response', async () => {
const { port1, port2 } = new MessageChannel();
const ours = new Switchboard({ port: port1, name: 'ours' });
// This test is required for 100% coverage. But there's no way to set up these conditions
// within the switchboard interface, so we gotta hack together the ports directly.
port2.addEventListener('message', event => {
const { messageId } = event.data;
port1.dispatchEvent({ data: { messageId } } as MessageEvent);
});
port2.start();

expect(ours.get('someMethod')).rejects.toThrowError(
'Unexpected response message',
);
});
});

it('logs in debug mode', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ export class Switchboard {
this.port.removeEventListener('message', listener);
if (isReply(message)) {
resolve(message.result);
} else if (isError(message)) {
reject(new Error(message.error));
} else {
const errStr = isError(message)
? message.error
: 'Unexpected response message';
reject(new Error(errStr));
}
};
this.port.addEventListener('message', listener);
Expand Down

0 comments on commit 955766b

Please sign in to comment.