Adapt existing tests to use webextensions-jsdom

This commit is contained in:
stoically
2019-12-18 09:19:01 +01:00
committed by Jonathan Kingston
parent 77fd843b3d
commit 5eb79949e9
3 changed files with 153 additions and 180 deletions
+23 -26
View File
@@ -1,12 +1,12 @@
describe("Assignment Feature", () => {
const activeTab = {
id: 1,
cookieStoreId: "firefox-container-1",
url: "http://example.com",
index: 0
};
const url = "http://example.com";
let activeTab;
beforeEach(async () => {
await helper.browser.initializeWithTab(activeTab);
activeTab = await helper.browser.initializeWithTab({
cookieStoreId: "firefox-container-1",
url
});
});
describe("click the 'Always open in' checkbox in the popup", () => {
@@ -16,23 +16,24 @@ describe("Assignment Feature", () => {
});
describe("open new Tab with the assigned URL in the default container", () => {
const newTab = {
id: 2,
cookieStoreId: "firefox-default",
url: activeTab.url,
index: 1,
active: true
};
let newTab;
beforeEach(async () => {
// new Tab opening activeTab.url in default container
await helper.browser.openNewTab(newTab);
newTab = await helper.browser.openNewTab({
cookieStoreId: "firefox-default",
url
}, {
options: {
webRequestError: true // because request is canceled due to reopening
}
});
});
it("should open the confirm page", async () => {
// should have created a new tab with the confirm page
background.browser.tabs.create.should.have.been.calledWith({
url: "moz-extension://multi-account-containers/confirm-page.html?" +
`url=${encodeURIComponent(activeTab.url)}` +
background.browser.tabs.create.should.have.been.calledWithMatch({
url: "moz-extension://fake/confirm-page.html?" +
`url=${encodeURIComponent(url)}` +
`&cookieStoreId=${activeTab.cookieStoreId}`,
cookieStoreId: undefined,
openerTabId: null,
@@ -53,16 +54,12 @@ describe("Assignment Feature", () => {
});
describe("open new Tab with the no longer assigned URL in the default container", () => {
const newTab = {
id: 3,
cookieStoreId: "firefox-default",
url: activeTab.url,
index: 3,
active: true
};
beforeEach(async () => {
// new Tab opening activeTab.url in default container
await helper.browser.openNewTab(newTab);
await helper.browser.openNewTab({
cookieStoreId: "firefox-default",
url
});
});
it("should not open the confirm page", async () => {
+18 -30
View File
@@ -1,12 +1,11 @@
describe("External Webextensions", () => {
const activeTab = {
id: 1,
cookieStoreId: "firefox-container-1",
url: "http://example.com",
index: 0
};
const url = "http://example.com";
beforeEach(async () => {
await helper.browser.initializeWithTab(activeTab);
await helper.browser.initializeWithTab({
cookieStoreId: "firefox-container-1",
url
});
await helper.popup.clickElementById("container-page-assigned");
});
@@ -18,25 +17,18 @@ describe("External Webextensions", () => {
const message = {
method: "getAssignment",
url: "http://example.com"
url
};
const sender = {
id: "external-webextension"
};
// currently not possible to get the return value of yielding with sinon
// so we expect that if no error is thrown and the storage was called, everything is ok
// maybe i get around to provide a PR https://github.com/sinonjs/sinon/issues/903
//
// the alternative would be to expose the actual messageHandler and call it directly
// but personally i think that goes against the black-box-ish nature of these feature tests
const rejectionStub = sinon.stub();
process.on("unhandledRejection", rejectionStub);
background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
await nextTick();
process.removeListener("unhandledRejection", rejectionStub);
rejectionStub.should.not.have.been.called;
background.browser.storage.local.get.should.have.been.called;
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
const answer = await promise;
expect(answer).to.deep.equal({
userContextId: "1",
neverAsk: false
});
});
});
@@ -48,20 +40,16 @@ describe("External Webextensions", () => {
const message = {
method: "getAssignment",
url: "http://example.com"
url
};
const sender = {
id: "external-webextension"
};
const rejectionStub = sinon.spy();
process.on("unhandledRejection", rejectionStub);
background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
await nextTick();
process.removeListener("unhandledRejection", rejectionStub);
rejectionStub.should.have.been.calledWith(sinon.match({
message: "Missing contextualIdentities permission"
}));
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
return promise.catch(error => {
expect(error.message).to.equal("Missing contextualIdentities permission");
});
});
});
});