From d2a52a41a8619c9282e9a995b25cb5f1a2e52ec9 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Thu, 9 Mar 2023 05:05:00 -0800 Subject: [PATCH 01/13] fix: Add logic to return early if hover returns empty string --- extension/rikaichan.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/extension/rikaichan.ts b/extension/rikaichan.ts index 7822fb2c3..6367b2000 100644 --- a/extension/rikaichan.ts +++ b/extension/rikaichan.ts @@ -263,7 +263,10 @@ class RcxMain { const m = this.showMode; let e: DictEntryData | null = null; - + if (text === '') { + console.log('search received empty string as text param'); + return; + } do { switch (this.showMode) { case 0: From 94f9c7d672c0932af8ec1109a3727b3db10d1a9e Mon Sep 17 00:00:00 2001 From: tora-pan Date: Sat, 11 Mar 2023 21:33:42 -0800 Subject: [PATCH 02/13] fix: Move fix to background. Add tests to cover 98% --- extension/background.ts | 10 +- extension/test/background_test.ts | 258 +++++++++++++++++++++++++++--- 2 files changed, 239 insertions(+), 29 deletions(-) diff --git a/extension/background.ts b/extension/background.ts index c99e249c6..36fb7bfa5 100644 --- a/extension/background.ts +++ b/extension/background.ts @@ -33,26 +33,24 @@ chrome.runtime.onMessage.addListener(async (request, sender, response) => { const rcxMain = await rcxMainPromise; switch (request.type) { case 'enable?': - console.log('enable?'); if (sender.tab === undefined) { throw TypeError('sender.tab is always defined here.'); } rcxMain.onTabSelect(sender.tab.id); break; case 'xsearch': - console.log('xsearch'); + if (request.text === '') { + break; + } response(rcxMain.search(request.text, request.dictOption)); break; case 'resetDict': - console.log('resetDict'); rcxMain.resetDict(); break; case 'translate': - console.log('translate'); response(rcxMain.dict.translate(request.title)); break; case 'makehtml': - console.log('makehtml'); response(rcxMain.dict.makeHtml(request.entry)); break; case 'switchOnlyReading': @@ -62,11 +60,9 @@ chrome.runtime.onMessage.addListener(async (request, sender, response) => { }); break; case 'copyToClip': - console.log('copyToClip'); rcxMain.copyToClip(sender.tab, request.entry); break; case 'playTTS': - console.log('playTTS'); tts.play(request.text); break; default: diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index 7e72d01c2..f48e49f7d 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -1,14 +1,36 @@ import { Config } from '../configuration'; import { RcxMain } from '../rikaichan'; import { expect, use } from '@esm-bundle/chai'; +import { tts } from '../texttospeech'; import chrome from 'sinon-chrome'; import sinon from 'sinon'; import sinonChai from 'sinon-chai'; use(sinonChai); - let rcxMain: RcxMain; +// used for stubbing data +const entry = + '小さい [ちいさい] /(adj-i) (1) small/little/tiny\ + /(adj-i) (2) slight/below average (in degree, amount, etc.)\ + /minor/small/(adj-i) (3) low (e.g. sound)/soft (e.g. voice)\ + /(adj-i) (4) unimportant/petty/insignificant/trifling/trivial\ + /(adj-i) (5) young/juvenile/(P)/'; +const entryStub = { + kanji: '', + onkun: '', + nanori: '', + bushumei: '', + misc: {} as Record, + eigo: '', + hasNames: false, + data: [{ entry, reason: '' }], + hasMore: false, + title: '', + index: 0, + matchLen: 0, +}; + describe('background.ts', function () { // Increase timeout from 2000ms since data tests can take longer. // Make it relative to current timeout so config level changes are taken @@ -65,26 +87,218 @@ describe('background.ts', function () { { config: rcxMain.config } ); }); + + it('should throw typeError if tab is undefined', async function () { + // }).to.throw(TypeError, 'sender.tab is always defined here.'); + }); + }); + + describe('xsearch', function () { + it('should return search results', async function () { + const searchStub = sinon.stub().returns(['to eat', 'verb']); + rcxMain.search = searchStub; + const request = { + type: 'xsearch', + text: 'た', + dictOption: '2', + }; + const sender = { tab: { id: 0 } }; + const response = sinon.spy(); + + // eslint-disable-next-line @typescript-eslint/await-thenable + await chrome.runtime.onMessage.addListener.callArgWith( + 0, + request, + sender, + response + ); + + expect(searchStub.calledWith(request.text, request.dictOption)).to.be + .true; + + expect(response.called).to.be.true; + }); + + it('should not search if text is empty', async function () { + const searchStub = sinon.stub().returns(['to eat', 'verb']); + rcxMain.search = searchStub; + const request = { type: 'xsearch', text: '', dictOption: '2' }; + const sender = { tab: { id: 0 } }; + const response = sinon.spy(); + + // eslint-disable-next-line @typescript-eslint/await-thenable + await chrome.runtime.onMessage.addListener.callArgWith( + 0, + request, + sender, + response + ); + + expect(searchStub).to.not.be.called; + expect(response.called).to.be.false; + }); + }); + + describe('resetDict', function () { + it('should reset rcxMain.showMode to 0', async function () { + const resetStub = sinon.stub(); + rcxMain.resetDict = resetStub; + const request = { type: 'resetDict' }; + const sender = { tab: { id: 0 } }; + const response = sinon.spy(); + + // eslint-disable-next-line @typescript-eslint/await-thenable + await chrome.runtime.onMessage.addListener.callArgWith( + 0, + request, + sender, + response + ); + + expect(resetStub).to.be.called; + expect(response.called).to.be.false; + }); + }); + describe('translate', function () { + it('should call translate method with title text', async function () { + const translateStub = sinon.stub().returns(['translated']); + rcxMain.dict.translate = translateStub; + const request = { + type: 'translate', + title: 'た', + dictOption: '2', + }; + const sender = { tab: { id: 0 } }; + const response = sinon.spy(); + + // eslint-disable-next-line @typescript-eslint/await-thenable + await chrome.runtime.onMessage.addListener.callArgWith( + 0, + request, + sender, + response + ); + + expect(translateStub.calledWith(request.title)).to.be.true; + expect(response.called).to.be.true; + }); }); -}); -async function sendMessageToBackground({ - tabId = 0, - type, - responseCallback = () => { - // Do nothing by default. - }, -}: { - tabId?: number; - type: string; - responseCallback?: (response: unknown) => void; -}): Promise { - // 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 } }, - responseCallback - ); - return; -} + describe('makeHtml', function () { + it('should call makeHtml method', async function () { + const makeHtmlStub = sinon.stub().returns(['makingHtml']); + rcxMain.dict.makeHtml = makeHtmlStub; + const request = { + type: 'makehtml', + entry: entryStub, + dictOption: '2', + }; + const sender = { tab: { id: 0 } }; + const response = sinon.spy(); + + // eslint-disable-next-line @typescript-eslint/await-thenable + await chrome.runtime.onMessage.addListener.callArgWith( + 0, + request, + sender, + response + ); + + expect(makeHtmlStub.calledWith(entryStub)).to.be.true; + expect(response.called).to.be.true; + }); + }); + describe('switchOnlyReading', function () { + it('should toggle the value of onlyReading to sync', async function () { + // eslint-disable-next-line @typescript-eslint/await-thenable + await sendMessageToBackground({ tabId: 0, type: 'switchOnlyReading' }); + + expect(chrome.storage.sync.set).to.be.calledWithMatch( + sinon.match({ onlyreading: sinon.match.bool }) + ); + }); + }); + describe('copyToClip', function () { + it('should call copyToClip on correct tab with correct entry', async function () { + const copyStub = sinon.stub(); + rcxMain.copyToClip = copyStub; + const request = { + type: 'copyToClip', + entry: entryStub, + }; + const sender = { tab: { id: 0 } }; + + // eslint-disable-next-line @typescript-eslint/await-thenable + await chrome.runtime.onMessage.addListener.callArgWith( + 0, + request, + sender + ); + + expect(copyStub).to.be.called; + expect(copyStub.calledWith(sender.tab, entryStub)).to.be.true; + }); + }); + describe('playTTS', function () { + it('should play text to speech', async function () { + const playTTSStub = sinon.stub(); + tts.play = playTTSStub; + const request = { + type: 'playTTS', + text: 'textToPlay', + }; + const sender = { tab: { id: 0 } }; + + // eslint-disable-next-line @typescript-eslint/await-thenable + await chrome.runtime.onMessage.addListener.callArgWith( + 0, + request, + sender + ); + + expect(playTTSStub.calledWith(request.text)).to.be.true; + }); + }); + + describe('default', function () { + it('should return void with console.log if no cases are matched', async function () { + const logger = sinon.stub(console, 'log'); + const request = { + type: 'noMatch', + }; + const sender = { tab: { id: 0 } }; + + // eslint-disable-next-line @typescript-eslint/await-thenable + await chrome.runtime.onMessage.addListener.callArgWith( + 0, + request, + sender + ); + + expect(logger.calledWith({ type: 'noMatch' })).to.be.true; + logger.restore(); + }); + }); + + async function sendMessageToBackground({ + tabId = 0, + type, + responseCallback = () => { + // Do nothing by default. + }, + }: { + tabId?: number; + type: string; + // text?: string; + responseCallback?: (response: unknown) => void; + }): Promise { + // 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 } }, + responseCallback + ); + return; + } +}); From 61b8bcca58456cc3a6f29db4045587642694b964 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Sun, 12 Mar 2023 09:53:12 -0700 Subject: [PATCH 03/13] fix: Revert to previous for separation of concerns --- extension/rikaichan.ts | 4 - extension/test/background_test.ts | 142 ------------------------------ package.json | 1 + 3 files changed, 1 insertion(+), 146 deletions(-) diff --git a/extension/rikaichan.ts b/extension/rikaichan.ts index 6367b2000..0b8466aa8 100644 --- a/extension/rikaichan.ts +++ b/extension/rikaichan.ts @@ -263,10 +263,6 @@ class RcxMain { const m = this.showMode; let e: DictEntryData | null = null; - if (text === '') { - console.log('search received empty string as text param'); - return; - } do { switch (this.showMode) { case 0: diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index f48e49f7d..99f92d0e4 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -139,147 +139,6 @@ describe('background.ts', function () { }); }); - describe('resetDict', function () { - it('should reset rcxMain.showMode to 0', async function () { - const resetStub = sinon.stub(); - rcxMain.resetDict = resetStub; - const request = { type: 'resetDict' }; - const sender = { tab: { id: 0 } }; - const response = sinon.spy(); - - // eslint-disable-next-line @typescript-eslint/await-thenable - await chrome.runtime.onMessage.addListener.callArgWith( - 0, - request, - sender, - response - ); - - expect(resetStub).to.be.called; - expect(response.called).to.be.false; - }); - }); - describe('translate', function () { - it('should call translate method with title text', async function () { - const translateStub = sinon.stub().returns(['translated']); - rcxMain.dict.translate = translateStub; - const request = { - type: 'translate', - title: 'た', - dictOption: '2', - }; - const sender = { tab: { id: 0 } }; - const response = sinon.spy(); - - // eslint-disable-next-line @typescript-eslint/await-thenable - await chrome.runtime.onMessage.addListener.callArgWith( - 0, - request, - sender, - response - ); - - expect(translateStub.calledWith(request.title)).to.be.true; - expect(response.called).to.be.true; - }); - }); - - describe('makeHtml', function () { - it('should call makeHtml method', async function () { - const makeHtmlStub = sinon.stub().returns(['makingHtml']); - rcxMain.dict.makeHtml = makeHtmlStub; - const request = { - type: 'makehtml', - entry: entryStub, - dictOption: '2', - }; - const sender = { tab: { id: 0 } }; - const response = sinon.spy(); - - // eslint-disable-next-line @typescript-eslint/await-thenable - await chrome.runtime.onMessage.addListener.callArgWith( - 0, - request, - sender, - response - ); - - expect(makeHtmlStub.calledWith(entryStub)).to.be.true; - expect(response.called).to.be.true; - }); - }); - describe('switchOnlyReading', function () { - it('should toggle the value of onlyReading to sync', async function () { - // eslint-disable-next-line @typescript-eslint/await-thenable - await sendMessageToBackground({ tabId: 0, type: 'switchOnlyReading' }); - - expect(chrome.storage.sync.set).to.be.calledWithMatch( - sinon.match({ onlyreading: sinon.match.bool }) - ); - }); - }); - describe('copyToClip', function () { - it('should call copyToClip on correct tab with correct entry', async function () { - const copyStub = sinon.stub(); - rcxMain.copyToClip = copyStub; - const request = { - type: 'copyToClip', - entry: entryStub, - }; - const sender = { tab: { id: 0 } }; - - // eslint-disable-next-line @typescript-eslint/await-thenable - await chrome.runtime.onMessage.addListener.callArgWith( - 0, - request, - sender - ); - - expect(copyStub).to.be.called; - expect(copyStub.calledWith(sender.tab, entryStub)).to.be.true; - }); - }); - describe('playTTS', function () { - it('should play text to speech', async function () { - const playTTSStub = sinon.stub(); - tts.play = playTTSStub; - const request = { - type: 'playTTS', - text: 'textToPlay', - }; - const sender = { tab: { id: 0 } }; - - // eslint-disable-next-line @typescript-eslint/await-thenable - await chrome.runtime.onMessage.addListener.callArgWith( - 0, - request, - sender - ); - - expect(playTTSStub.calledWith(request.text)).to.be.true; - }); - }); - - describe('default', function () { - it('should return void with console.log if no cases are matched', async function () { - const logger = sinon.stub(console, 'log'); - const request = { - type: 'noMatch', - }; - const sender = { tab: { id: 0 } }; - - // eslint-disable-next-line @typescript-eslint/await-thenable - await chrome.runtime.onMessage.addListener.callArgWith( - 0, - request, - sender - ); - - expect(logger.calledWith({ type: 'noMatch' })).to.be.true; - logger.restore(); - }); - }); - async function sendMessageToBackground({ tabId = 0, type, @@ -289,7 +148,6 @@ describe('background.ts', function () { }: { tabId?: number; type: string; - // text?: string; responseCallback?: (response: unknown) => void; }): Promise { // In background.ts, a promise is passed to `addListener` so we can await it here. diff --git a/package.json b/package.json index d01480382..1b77fddf6 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" From 2d5755a26d05dcb447b98d955be2fd18d8c03c4c Mon Sep 17 00:00:00 2001 From: tora-pan Date: Mon, 13 Mar 2023 21:01:53 -0700 Subject: [PATCH 04/13] fix: Remove unused variables --- extension/test/background_test.ts | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index 99f92d0e4..aca6e56a7 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -1,7 +1,6 @@ import { Config } from '../configuration'; import { RcxMain } from '../rikaichan'; import { expect, use } from '@esm-bundle/chai'; -import { tts } from '../texttospeech'; import chrome from 'sinon-chrome'; import sinon from 'sinon'; import sinonChai from 'sinon-chai'; @@ -9,28 +8,6 @@ import sinonChai from 'sinon-chai'; use(sinonChai); let rcxMain: RcxMain; -// used for stubbing data -const entry = - '小さい [ちいさい] /(adj-i) (1) small/little/tiny\ - /(adj-i) (2) slight/below average (in degree, amount, etc.)\ - /minor/small/(adj-i) (3) low (e.g. sound)/soft (e.g. voice)\ - /(adj-i) (4) unimportant/petty/insignificant/trifling/trivial\ - /(adj-i) (5) young/juvenile/(P)/'; -const entryStub = { - kanji: '', - onkun: '', - nanori: '', - bushumei: '', - misc: {} as Record, - eigo: '', - hasNames: false, - data: [{ entry, reason: '' }], - hasMore: false, - title: '', - index: 0, - matchLen: 0, -}; - describe('background.ts', function () { // Increase timeout from 2000ms since data tests can take longer. // Make it relative to current timeout so config level changes are taken From b07b34f22cff8cb78abacb29078c5b31b59afc91 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Mon, 13 Mar 2023 21:03:58 -0700 Subject: [PATCH 05/13] fix: Remove temp script from package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index cfdcd8dbf..20210005a 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "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" From 936c6c1aeee5ad7531a221944d62adeb5ca4c94f Mon Sep 17 00:00:00 2001 From: tora-pan Date: Mon, 13 Mar 2023 21:06:37 -0700 Subject: [PATCH 06/13] fix: Replace removed white space --- extension/rikaichan.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/extension/rikaichan.ts b/extension/rikaichan.ts index 0b8466aa8..7822fb2c3 100644 --- a/extension/rikaichan.ts +++ b/extension/rikaichan.ts @@ -263,6 +263,7 @@ class RcxMain { const m = this.showMode; let e: DictEntryData | null = null; + do { switch (this.showMode) { case 0: From 9f3d5959452f1e0c3a2ed82c4465760e9fafd9c9 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Mon, 13 Mar 2023 21:08:14 -0700 Subject: [PATCH 07/13] fix: Remove commented out test --- extension/test/background_test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index aca6e56a7..0f49677a9 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -64,10 +64,6 @@ describe('background.ts', function () { { config: rcxMain.config } ); }); - - it('should throw typeError if tab is undefined', async function () { - // }).to.throw(TypeError, 'sender.tab is always defined here.'); - }); }); describe('xsearch', function () { From dcc90a32bf96131ed6858e777a46cabfe1ff4215 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Mon, 13 Mar 2023 21:32:20 -0700 Subject: [PATCH 08/13] fix: Resolve requested changes --- extension/test/background_test.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index 0f49677a9..ec17beff5 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -67,12 +67,12 @@ describe('background.ts', function () { }); describe('xsearch', function () { - it('should return search results', async function () { + it('should call response callback with search results', async function () { const searchStub = sinon.stub().returns(['to eat', 'verb']); rcxMain.search = searchStub; const request = { type: 'xsearch', - text: 'た', + text: 'anything that is not empty', dictOption: '2', }; const sender = { tab: { id: 0 } }; @@ -86,15 +86,13 @@ describe('background.ts', function () { response ); - expect(searchStub.calledWith(request.text, request.dictOption)).to.be - .true; + expect(searchStub.calledWith(request.text, sinon.match.any)).to.be.true; expect(response.called).to.be.true; }); - it('should not search if text is empty', async function () { - const searchStub = sinon.stub().returns(['to eat', 'verb']); - rcxMain.search = searchStub; + it('should not call rcxMain.search if text is empty', async function () { + rcxMain.search = sinon.stub().returns(['to eat', 'verb']); const request = { type: 'xsearch', text: '', dictOption: '2' }; const sender = { tab: { id: 0 } }; const response = sinon.spy(); @@ -107,7 +105,7 @@ describe('background.ts', function () { response ); - expect(searchStub).to.not.be.called; + expect(rcxMain.search).to.not.be.called; expect(response.called).to.be.false; }); }); From b0f56dc33a5dcbf9967287648f008d93e823aad1 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Mon, 13 Mar 2023 21:38:00 -0700 Subject: [PATCH 09/13] fix: Revert back to declared stub to resolve ESLint warnings --- extension/test/background_test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index ec17beff5..f708838f9 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -92,7 +92,8 @@ describe('background.ts', function () { }); it('should not call rcxMain.search if text is empty', async function () { - rcxMain.search = sinon.stub().returns(['to eat', 'verb']); + const searchStub = sinon.stub().returns(['to eat', 'verb']); + rcxMain.search = searchStub; const request = { type: 'xsearch', text: '', dictOption: '2' }; const sender = { tab: { id: 0 } }; const response = sinon.spy(); @@ -105,7 +106,7 @@ describe('background.ts', function () { response ); - expect(rcxMain.search).to.not.be.called; + expect(searchStub).to.not.be.called; expect(response.called).to.be.false; }); }); From cb9d24d72f14165bea4bb156ee9fc071845204f7 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Sun, 12 Mar 2023 09:53:12 -0700 Subject: [PATCH 10/13] fix: Revert to previous for separation of concerns --- extension/rikaichan.ts | 1 - package.json | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) 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/package.json b/package.json index 20210005a..cfdcd8dbf 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" From fee9b4dd7fab3464e6590cc97a8c2163a97edb52 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Wed, 15 Mar 2023 04:29:44 -0700 Subject: [PATCH 11/13] test: Update test declaration to be more clear --- extension/test/background_test.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index f708838f9..f6936c2df 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -91,9 +91,7 @@ describe('background.ts', function () { expect(response.called).to.be.true; }); - it('should not call rcxMain.search if text is empty', async function () { - const searchStub = sinon.stub().returns(['to eat', 'verb']); - rcxMain.search = searchStub; + it('should not call response callback if text input is empty', async function () { const request = { type: 'xsearch', text: '', dictOption: '2' }; const sender = { tab: { id: 0 } }; const response = sinon.spy(); @@ -106,7 +104,6 @@ describe('background.ts', function () { response ); - expect(searchStub).to.not.be.called; expect(response.called).to.be.false; }); }); From 982c139af9048badb52d10825ffb2977ef34b7bb Mon Sep 17 00:00:00 2001 From: tora-pan Date: Wed, 15 Mar 2023 04:40:26 -0700 Subject: [PATCH 12/13] fix: Reformat to use helper sendToBackground --- extension/test/background_test.ts | 104 ++++++++++++++++-------------- 1 file changed, 55 insertions(+), 49 deletions(-) diff --git a/extension/test/background_test.ts b/extension/test/background_test.ts index f6936c2df..d03b240b5 100644 --- a/extension/test/background_test.ts +++ b/extension/test/background_test.ts @@ -67,65 +67,71 @@ describe('background.ts', function () { }); describe('xsearch', function () { - it('should call response callback with search results', async function () { - const searchStub = sinon.stub().returns(['to eat', 'verb']); - rcxMain.search = searchStub; - const request = { - type: 'xsearch', - text: 'anything that is not empty', - dictOption: '2', - }; - const sender = { tab: { id: 0 } }; + it('should call response callback with search correct values', async function () { + rcxMain.search = sinon + .stub() + .returns({ text: 'theText', dictOptions: '0' }); const response = sinon.spy(); - // eslint-disable-next-line @typescript-eslint/await-thenable - await chrome.runtime.onMessage.addListener.callArgWith( - 0, - request, - sender, - response - ); - - expect(searchStub.calledWith(request.text, sinon.match.any)).to.be.true; - - expect(response.called).to.be.true; + 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 call response callback if text input is empty', async function () { - const request = { type: 'xsearch', text: '', dictOption: '2' }; - const sender = { tab: { id: 0 } }; + it('should not search if request.text is an empty string', async function () { const response = sinon.spy(); - // eslint-disable-next-line @typescript-eslint/await-thenable - await chrome.runtime.onMessage.addListener.callArgWith( - 0, - request, - sender, - response - ); + await sendMessageToBackground({ + tabId: 0, + type: 'xsearch', + text: '', + responseCallback: response, + }); expect(response.called).to.be.false; }); }); +}); - async function sendMessageToBackground({ - tabId = 0, +type Payload = { + tabId?: number; + text?: string; + type: string; + responseCallback?: (response: unknown) => void; +}; + +async function sendMessageToBackground({ + tabId = 0, + type, + text, + responseCallback = () => { + // Do nothing by default. + }, +}: Payload): Promise { + const request: { type: string; text?: string } = { type, - responseCallback = () => { - // Do nothing by default. - }, - }: { - tabId?: number; - type: string; - responseCallback?: (response: unknown) => void; - }): Promise { - // 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 } }, - responseCallback - ); - return; + }; + 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( + request, + sender, + responseCallback + ); + return; +} From 8317805d794e031c05f80bb882122599ce7e2ce9 Mon Sep 17 00:00:00 2001 From: tora-pan Date: Mon, 20 Mar 2023 05:33:25 -0700 Subject: [PATCH 13/13] fix: Add back console.logs to background addListener --- extension/background.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/extension/background.ts b/extension/background.ts index 36fb7bfa5..3321ad28a 100644 --- a/extension/background.ts +++ b/extension/background.ts @@ -33,24 +33,29 @@ chrome.runtime.onMessage.addListener(async (request, sender, response) => { const rcxMain = await rcxMainPromise; switch (request.type) { case 'enable?': + console.log('enable?'); if (sender.tab === undefined) { throw TypeError('sender.tab is always defined here.'); } rcxMain.onTabSelect(sender.tab.id); break; case 'xsearch': + console.log('xsearch'); if (request.text === '') { break; } response(rcxMain.search(request.text, request.dictOption)); break; case 'resetDict': + console.log('resetDict'); rcxMain.resetDict(); break; case 'translate': + console.log('translate'); response(rcxMain.dict.translate(request.title)); break; case 'makehtml': + console.log('makehtml'); response(rcxMain.dict.makeHtml(request.entry)); break; case 'switchOnlyReading': @@ -60,9 +65,11 @@ chrome.runtime.onMessage.addListener(async (request, sender, response) => { }); break; case 'copyToClip': + console.log('copyToClip'); rcxMain.copyToClip(sender.tab, request.entry); break; case 'playTTS': + console.log('playTTS'); tts.play(request.text); break; default: