diff --git a/package.json b/package.json index 382736f0..8a682e7f 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ ] }, "jest": { + "preset": "ts-jest", "moduleDirectories": ["./src", "./tests", "./node_modules"], "setupFilesAfterEnv": [ "./tests/setup.ts" @@ -41,9 +42,6 @@ "./node_modules/" ], "coverageDirectory": "./artifacts/coverage", - "transform": { - "^.+\\.tsx?$": "ts-jest" - }, "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$", "moduleFileExtensions": [ "node", diff --git a/src/utils/chrome.ts b/src/utils/chrome.ts index 889e139e..dd25752d 100644 --- a/src/utils/chrome.ts +++ b/src/utils/chrome.ts @@ -4,9 +4,10 @@ import { wrapIndex } from "./base"; * Bind single oncommand listener and switch based on received */ const listeners: { [key: string]: Set<() => any> } = {}; -chrome.commands.onCommand.addListener(command => { +export const commandListener = (command: string) => { (listeners[command] || []).forEach((callback: () => any) => callback()); -}); +}; +chrome.commands.onCommand.addListener(commandListener); /** * Register callback to be run on specific command diff --git a/tests/sinon-chrome.d.ts b/tests/globals.d.ts similarity index 100% rename from tests/sinon-chrome.d.ts rename to tests/globals.d.ts diff --git a/tests/setup.ts b/tests/setup.ts index d267cd0d..5ed2471c 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -1,4 +1,3 @@ -import chrome from "sinon-chrome"; +import * as chrome from "sinon-chrome"; Object.assign(global, { chrome }); -Object.assign(window, { chrome }); diff --git a/tests/utils/chrome.test.ts b/tests/utils/chrome.test.ts new file mode 100644 index 00000000..83a23171 --- /dev/null +++ b/tests/utils/chrome.test.ts @@ -0,0 +1,28 @@ +import * as chrome from "sinon-chrome"; + +import { commandListener, onCommand } from "utils/chrome"; + +describe("chrome", () => { + describe("commands", () => { + it("registers listener to chome oncommand", () => { + expect(chrome.commands.onCommand.hasListener(commandListener)).toBe( + true, + ); + }); + + it("triggers all registered callbacks", () => { + const mockCommand1 = "ctrl+1"; + const mockCommand2 = "ctrl+2"; + const mockCallbackA = jest.fn(); + const mockCallbackB = jest.fn(); + const mockCallbackC = jest.fn(); + onCommand(mockCommand1, mockCallbackA); + onCommand(mockCommand1, mockCallbackB); + onCommand(mockCommand2, mockCallbackC); + commandListener(mockCommand1); + expect(mockCallbackA).toHaveBeenCalledTimes(1); + expect(mockCallbackB).toHaveBeenCalledTimes(1); + expect(mockCallbackC).not.toHaveBeenCalled(); + }); + }); +});