Skip to content

Commit

Permalink
test: windows
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Apr 17, 2019
1 parent 4bee804 commit 0c9731c
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions tests/utils/chrome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import * as chrome from "sinon-chrome";
import {
changeTabUrl,
commandListener,
createWindow,
focusOnTab,
focusOnWindow,
getAllCommands,
getAllTabsInWindow,
getLastFocusedWindow,
getWindowIdAfter,
getWindowIdBefore,
moveTabsToWindow,
onCommand,
selectTabs,
} from "utils/chrome";

describe("chrome", () => {
afterAll(chrome.flush);
afterEach(chrome.flush);

const expectToHaveBeenCalledWith = (stub: any, ...args: any[]) => {
expect(stub.withArgs(...args).calledOnce).toBe(true);
Expand Down Expand Up @@ -40,28 +45,29 @@ describe("chrome", () => {
expect(mockCallbackC).not.toHaveBeenCalled();
});

it("resolves to list of commands", () => {
it("resolves to list of commands", async () => {
const commands = ["ctrl+a", "ctrl+b"];
chrome.commands.getAll.yields(commands);
expect(getAllCommands()).resolves.toEqual(commands);
await getAllCommands();
chrome.commands.getAll.flush();
});
});

describe("tabs", () => {
it("gets all tabs", () => {
it("gets all tabs", async () => {
const tabs = ["mock-tab1", "mock-tab2"];
chrome.tabs.query.yields(tabs);
expect(getAllTabsInWindow(1)).resolves.toEqual(tabs);
await getAllTabsInWindow(1);
expect(chrome.tabs.query.withArgs({ windowId: 1 }).calledOnce).toBe(
true,
);
chrome.tabs.query.flush();
});

it("changes tab url", () => {
it("changes tab url", async () => {
const targetUrl = "https://target.url";
expect(changeTabUrl(targetUrl)).resolves.toBeUndefined();
chrome.tabs.update.yields();
await changeTabUrl(targetUrl);
expectToHaveBeenCalledWith(chrome.tabs.update, { url: targetUrl });
});

Expand All @@ -72,28 +78,55 @@ describe("chrome", () => {
});
});

it("selects tab", () => {
it("selects tab", async () => {
const tabIds = [8, 9, 10];
expect(selectTabs([8, 9, 10])).resolves.toBeUndefined();
const selectTabsPromise = selectTabs([8, 9, 10]);
chrome.tabs.update.yield();
await selectTabsPromise;
tabIds.forEach(tabId =>
expectToHaveBeenCalledWith(chrome.tabs.update, tabId, {
highlighted: true,
}),
);
});

it("moves tabs to window", () => {
it("moves tabs to window", async () => {
const windowId = 1;
const tabs = [8, 9, 10].map(
(id, index) => ({ id, index, windowId } as ChromeTab),
);
expect(moveTabsToWindow(tabs, windowId)).resolves.toBeUndefined();
tabs.forEach(t =>
expectToHaveBeenCalledWith(chrome.tabs.update, t.id, {
highlighted: true,
const moveTabsPromise = moveTabsToWindow(tabs, windowId);
chrome.tabs.move.yield();
await moveTabsPromise;
tabs.forEach((t, i) =>
expectToHaveBeenCalledWith(chrome.tabs.move, t.id, {
index: i,
windowId,
}),
);
});
});

describe("windows", () => {
it("focuses on window", async () => {
const windowId = 73;
chrome.windows.update.yields();
await focusOnWindow(windowId);
expectToHaveBeenCalledWith(chrome.windows.update, windowId, {
focused: true,
});
});

it("gets last focused window", async () => {
chrome.windows.getLastFocused.yields();
await getLastFocusedWindow();
expectToHaveBeenCalledWith(chrome.windows.getLastFocused, {});
});

it("creates new window", () => {
const tabId = 11;
expect(createWindow(tabId)).resolves.toBeUndefined();
expectToHaveBeenCalledWith(chrome.windows.create, { tabId });
});
});
});

0 comments on commit 0c9731c

Please sign in to comment.