Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Commit

Permalink
Add tests for browser actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Porter committed Oct 11, 2017
1 parent 5674df3 commit f68fd65
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
48 changes: 48 additions & 0 deletions test/background/browser-action-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

require("babel-polyfill");

import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import sinon from "sinon";
import sinonChai from "sinon-chai";

chai.use(chaiAsPromised);
chai.use(sinonChai);

import updateBrowserAction from
"../../src/webextension/background/browser-action";

describe("background > browser action", () => {
let openView;

beforeEach(() => {
openView = sinon.spy();
updateBrowserAction.__Rewire__("openView", openView);
});

afterEach(() => {
updateBrowserAction.__ResetDependency__("openView");
});

it("uninitialized data store", async() => {
await updateBrowserAction({initialized: false});
browser.browserAction.onClicked.mockFireListener();
expect(openView).to.have.been.calledWith("firstrun");
});

it("locked data store", async() => {
await updateBrowserAction({initialized: true, locked: true});
expect(browser.browserAction.getPopup()).to.eventually.equal(
browser.extension.getURL("popup/unlock/index.html")
);
});

it("unlocked data store", async() => {
await updateBrowserAction({initialized: true, locked: false});
browser.browserAction.onClicked.mockFireListener();
expect(openView).to.have.been.calledWith("manage");
});
});
19 changes: 15 additions & 4 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class MockListener {
this._listener = fn;
}

removeListener(fn) {
if (fn === this._listener) {
this._listener = null;
}
}

mockClearListener() {
this._listener = null;
}
Expand Down Expand Up @@ -120,11 +126,16 @@ function makePairedPorts(contextId) {

global.browser = {
browserAction: {
onClicked: {
addListener() {},
removeListener() {},
_popupPage: "",
onClicked: new MockListener(),

setPopup({popup}) {
this._popupPage = popup;
},

async getPopup() {
return this._popupPage;
},
setPopup() {},
},

extension: {
Expand Down

0 comments on commit f68fd65

Please sign in to comment.