Skip to content

Commit

Permalink
Add fallback for handleCmd function
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Oct 22, 2024
1 parent f8977b7 commit c2f8936
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/mjs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -894,13 +894,16 @@ export const onWindowRemoved = async () => {
* @param {object} tab - tabs.Tab
* @returns {?Promise} - sendMessage() / openOptionsPage()
*/
export const handleCmd = async (cmd, tab = {}) => {
export const handleCmd = async (cmd, tab) => {
if (!isString(cmd)) {
throw new TypeError(`Expected String but got ${getType(cmd)}.`);
}
let func;
switch (cmd) {
case EDITOR_EXEC: {
if (!tab) {
tab = await getActiveTab();
}
const { id } = tab;
if (tabList.has(id)) {
func = sendMessage(id, {
Expand Down
18 changes: 18 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,24 @@ describe('main', () => {
assert.strictEqual(browser.tabs.query.callCount, j, 'not called');
assert.deepEqual(res, {}, 'result');
});

it('should call function', async () => {
const i = browser.tabs.sendMessage.callCount;
const j = browser.tabs.query.callCount;
browser.tabs.sendMessage.resolves({});
browser.tabs.query.withArgs({
windowId: browser.windows.WINDOW_ID_CURRENT,
active: true,
windowType: 'normal'
}).resolves([{
id: 2,
windowId: 1
}]);
const res = await func(EDITOR_EXEC);
assert.strictEqual(browser.tabs.sendMessage.callCount, i + 1, 'called');
assert.strictEqual(browser.tabs.query.callCount, j + 1, 'called');
assert.deepEqual(res, {}, 'result');
});
});

describe('send variable', () => {
Expand Down

0 comments on commit c2f8936

Please sign in to comment.