Skip to content

Commit

Permalink
test: setup chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Apr 16, 2019
1 parent ee0608f commit 344d855
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
]
},
"jest": {
"preset": "ts-jest",
"moduleDirectories": ["./src", "./tests", "./node_modules"],
"setupFilesAfterEnv": [
"./tests/setup.ts"
Expand All @@ -41,9 +42,6 @@
"./node_modules/"
],
"coverageDirectory": "./artifacts/coverage",
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"node",
Expand Down
5 changes: 3 additions & 2 deletions src/utils/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import chrome from "sinon-chrome";
import * as chrome from "sinon-chrome";

Object.assign(global, { chrome });
Object.assign(window, { chrome });
28 changes: 28 additions & 0 deletions tests/utils/chrome.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 344d855

Please sign in to comment.