Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add logic to return early if hover returns empty string #1446

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions extension/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,24 @@ chrome.runtime.onMessage.addListener(async (request, sender, response) => {
const rcxMain = await rcxMainPromise;
switch (request.type) {
case 'enable?':
console.log('enable?');
tora-pan marked this conversation as resolved.
Show resolved Hide resolved
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':
Expand All @@ -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:
Expand Down
1 change: 0 additions & 1 deletion extension/rikaichan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ class RcxMain {

const m = this.showMode;
let e: DictEntryData | null = null;

tora-pan marked this conversation as resolved.
Show resolved Hide resolved
do {
switch (this.showMode) {
case 0:
Expand Down
63 changes: 55 additions & 8 deletions extension/test/background_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import sinon from 'sinon';
import sinonChai from 'sinon-chai';

use(sinonChai);

let rcxMain: RcxMain;

describe('background.ts', function () {
Expand Down Expand Up @@ -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;
tora-pan marked this conversation as resolved.
Show resolved Hide resolved
});
});
});

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<void> {
}: Payload): Promise<void> {
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;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
tora-pan marked this conversation as resolved.
Show resolved Hide resolved
"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"
Expand Down