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

Merged
merged 18 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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?');
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;

do {
switch (this.showMode) {
case 0:
Expand Down
116 changes: 94 additions & 22 deletions extension/test/background_test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>,
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
Expand Down Expand Up @@ -65,26 +87,76 @@ 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.');
});
});
});

async function sendMessageToBackground({
tabId = 0,
type,
responseCallback = () => {
// Do nothing by default.
},
}: {
tabId?: number;
type: string;
responseCallback?: (response: unknown) => void;
}): Promise<void> {
// 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('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;
});
});

async function sendMessageToBackground({
tabId = 0,
type,
responseCallback = () => {
// Do nothing by default.
},
}: {
tabId?: number;
type: string;
responseCallback?: (response: unknown) => void;
}): Promise<void> {
// 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;
}
});
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\"",
"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