From 64a533d63bab3005c3de9e73e66c80857d73ee2e Mon Sep 17 00:00:00 2001 From: tora-pan Date: Tue, 14 Mar 2023 06:09:05 -0700 Subject: [PATCH 1/2] fix: Return early if search string is empty --- extension/background.ts | 4 ++ extension/test/background_test.ts | 61 +++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/extension/background.ts b/extension/background.ts index c99e249c6..dc19730c4 100644 --- a/extension/background.ts +++ b/extension/background.ts @@ -40,6 +40,10 @@ chrome.runtime.onMessage.addListener(async (request, sender, response) => { rcxMain.onTabSelect(sender.tab.id); break; case 'xsearch': + if (request.text === '') { + console.log('returning early due to empty string search'); + break; + } console.log('xsearch'); response(rcxMain.search(request.text, request.dictOption)); break; diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index 7e72d01c2..65f04cac1 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -66,24 +66,71 @@ describe('background.ts', function () { ); }); }); + describe('xsearch', function () { + it('should call response callback with search results', async function () { + rcxMain.search = sinon + .stub() + .returns({ type: 'theType', text: 'theText' }); + const response = sinon.spy(); + + await sendMessageToBackground({ + tabId: 0, + type: 'xsearch', + text: 'A non empty string', + responseCallback: response, + }); + + expect(response).to.have.been.calledWithMatch({ + type: 'theType', + text: 'theText', + }); + expect(response).to.have.been.calledOnce; + }); + + it('should not search if text is empty', async function () { + const response = sinon.spy(); + + await sendMessageToBackground({ + tabId: 0, + type: 'xsearch', + text: '', + responseCallback: response, + }); + + expect(response.called).to.be.false; + }); + }); }); +type Payload = { + tabId?: number; + text?: string; + type: string; + responseCallback?: (response: unknown) => void; +}; + async function sendMessageToBackground({ tabId = 0, type, + text, responseCallback = () => { // Do nothing by default. }, -}: { - tabId?: number; - type: string; - responseCallback?: (response: unknown) => void; -}): Promise { +}: Payload): Promise { + const request: { type: string; text?: string } = { + type, + }; + const sender = { + tab: { id: tabId }, + }; + if (text !== undefined) { + request['text'] = text; + } // In background.ts, a promise is passed to `addListener` so we can await it here. // eslint-disable-next-line @typescript-eslint/await-thenable await chrome.runtime.onMessage.addListener.yield( - { type: type }, - { tab: { id: tabId } }, + request, + sender, responseCallback ); return; From b1b741acbdc39a5a20174ffc777e2a98643176d5 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Tue, 14 Mar 2023 06:21:00 -0700 Subject: [PATCH 2/2] fix: Update test names to be more clear --- extension/test/background_test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index 65f04cac1..e0abd903e 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -67,10 +67,10 @@ describe('background.ts', function () { }); }); describe('xsearch', function () { - it('should call response callback with search results', async function () { + it('should call response callback with search string and dictOptions value', async function () { rcxMain.search = sinon .stub() - .returns({ type: 'theType', text: 'theText' }); + .returns({ text: 'theText', dictOptions: '0' }); const response = sinon.spy(); await sendMessageToBackground({ @@ -81,13 +81,13 @@ describe('background.ts', function () { }); expect(response).to.have.been.calledWithMatch({ - type: 'theType', text: 'theText', + dictOptions: sinon.match.any, }); expect(response).to.have.been.calledOnce; }); - it('should not search if text is empty', async function () { + it('should not search if request.text is an empty string', async function () { const response = sinon.spy(); await sendMessageToBackground({