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: Return early if search string is empty #1458

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions extension/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 54 additions & 7 deletions extension/test/background_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,71 @@ describe('background.ts', function () {
);
});
});
describe('xsearch', function () {
it('should call response callback with search string and dictOptions value', 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<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