From 955766bc25c6f22e7c52957b2ba39e306ccf3a7b Mon Sep 17 00:00:00 2001 From: David Aaron Suddjian Date: Fri, 11 Feb 2022 17:26:52 -0800 Subject: [PATCH] appease the coverage gods --- .../src/switchboard.test.ts | 23 +++++++++++++++++++ .../src/switchboard.ts | 7 ++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/superset-frontend/packages/superset-ui-switchboard/src/switchboard.test.ts b/superset-frontend/packages/superset-ui-switchboard/src/switchboard.test.ts index 4e749936c4c01..859769e0e082b 100644 --- a/superset-frontend/packages/superset-ui-switchboard/src/switchboard.test.ts +++ b/superset-frontend/packages/superset-ui-switchboard/src/switchboard.test.ts @@ -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(); @@ -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 () => { diff --git a/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts b/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts index 1f11979c41cf6..b65ca13586377 100644 --- a/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts +++ b/superset-frontend/packages/superset-ui-switchboard/src/switchboard.ts @@ -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);