diff --git a/extension/background.ts b/extension/background.ts index c99e249c6..3321ad28a 100644 --- a/extension/background.ts +++ b/extension/background.ts @@ -41,6 +41,9 @@ chrome.runtime.onMessage.addListener(async (request, sender, response) => { break; case 'xsearch': console.log('xsearch'); + if (request.text === '') { + break; + } response(rcxMain.search(request.text, request.dictOption)); break; case 'resetDict': diff --git a/extension/rikaichan.ts b/extension/rikaichan.ts index 7822fb2c3..0b8466aa8 100644 --- a/extension/rikaichan.ts +++ b/extension/rikaichan.ts @@ -263,7 +263,6 @@ class RcxMain { const m = this.showMode; let e: DictEntryData | null = null; - do { switch (this.showMode) { case 0: diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index 7e72d01c2..d03b240b5 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -6,7 +6,6 @@ import sinon from 'sinon'; import sinonChai from 'sinon-chai'; use(sinonChai); - let rcxMain: RcxMain; describe('background.ts', function () { @@ -66,24 +65,72 @@ describe('background.ts', function () { ); }); }); + + describe('xsearch', function () { + it('should call response callback with search correct values', async function () { + rcxMain.search = sinon + .stub() + .returns({ text: 'theText', dictOptions: '0' }); + const response = sinon.spy(); + + await sendMessageToBackground({ + tabId: 0, + type: 'xsearch', + text: 'A non empty string', + responseCallback: response, + }); + + expect(response).to.have.been.calledWithMatch({ + text: 'theText', + dictOptions: sinon.match.any, + }); + expect(response).to.have.been.calledOnce; + }); + + it('should not search if request.text is an empty string', 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; diff --git a/package.json b/package.json index f616c97f3..10825f583 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "test": "wtr \"extension/test/*_test.ts\"", "test:small": "wtr --files \"extension/test/background_test.ts\" \"extension/test/data_test.ts\" \"extension/test/docs-annotate-canvas_test.ts\" \"extension/test/rikaicontent_test.ts\"", "test:browserstack": "npm run test -- --browserstack", + "test:tiny": "wtr --files \"extension/test/background_test.ts\"", "test:watch": "npm run test -- --watch", "test:update-baselines": "npm run test -- --update-visual-baseline", "update-db": "node --loader ts-node/esm utils/update-db.ts"