Refactor tests to not rely on globals

This commit is contained in:
stoically
2020-01-24 08:16:15 +01:00
parent 53baee1d5c
commit 405e605ba3
10 changed files with 213 additions and 311 deletions
+28 -23
View File
@@ -1,25 +1,30 @@
describe("Assignment Feature", () => {
const {initializeWithTab} = require("../common");
describe("Assignment Feature", function () {
const url = "http://example.com";
let activeTab;
beforeEach(async () => {
activeTab = await helper.browser.initializeWithTab({
beforeEach(async function () {
this.webExt = await initializeWithTab({
cookieStoreId: "firefox-container-1",
url
});
});
describe("click the 'Always open in' checkbox in the popup", () => {
beforeEach(async () => {
afterEach(function () {
this.webExt.destroy();
});
describe("click the 'Always open in' checkbox in the popup", function () {
beforeEach(async function () {
// popup click to set assignment for activeTab.url
await helper.popup.clickElementById("container-page-assigned");
await this.webExt.popup.helper.clickElementById("container-page-assigned");
});
describe("open new Tab with the assigned URL in the default container", () => {
describe("open new Tab with the assigned URL in the default container", function () {
let newTab;
beforeEach(async () => {
beforeEach(async function () {
// new Tab opening activeTab.url in default container
newTab = await helper.browser.openNewTab({
newTab = await this.webExt.background.browser.tabs._create({
cookieStoreId: "firefox-default",
url
}, {
@@ -29,12 +34,12 @@ describe("Assignment Feature", () => {
});
});
it("should open the confirm page", async () => {
it("should open the confirm page", async function () {
// should have created a new tab with the confirm page
background.browser.tabs.create.should.have.been.calledWithMatch({
this.webExt.background.browser.tabs.create.should.have.been.calledWithMatch({
url: "moz-extension://fake/confirm-page.html?" +
`url=${encodeURIComponent(url)}` +
`&cookieStoreId=${activeTab.cookieStoreId}`,
`&cookieStoreId=${this.webExt.tab.cookieStoreId}`,
cookieStoreId: undefined,
openerTabId: null,
index: 2,
@@ -42,29 +47,29 @@ describe("Assignment Feature", () => {
});
});
it("should remove the new Tab that got opened in the default container", () => {
background.browser.tabs.remove.should.have.been.calledWith(newTab.id);
it("should remove the new Tab that got opened in the default container", function () {
this.webExt.background.browser.tabs.remove.should.have.been.calledWith(newTab.id);
});
});
describe("click the 'Always open in' checkbox in the popup again", () => {
beforeEach(async () => {
describe("click the 'Always open in' checkbox in the popup again", function () {
beforeEach(async function () {
// popup click to remove assignment for activeTab.url
await helper.popup.clickElementById("container-page-assigned");
await this.webExt.popup.helper.clickElementById("container-page-assigned");
});
describe("open new Tab with the no longer assigned URL in the default container", () => {
beforeEach(async () => {
describe("open new Tab with the no longer assigned URL in the default container", function () {
beforeEach(async function () {
// new Tab opening activeTab.url in default container
await helper.browser.openNewTab({
await this.webExt.background.browser.tabs._create({
cookieStoreId: "firefox-default",
url
});
});
it("should not open the confirm page", async () => {
it("should not open the confirm page", async function () {
// should not have created a new tab
background.browser.tabs.create.should.not.have.been.called;
this.webExt.background.browser.tabs.create.should.not.have.been.called;
});
});
});
+22 -16
View File
@@ -1,27 +1,33 @@
describe("Containers Management", () => {
beforeEach(async () => {
await helper.browser.initializeWithTab();
const {initializeWithTab} = require("../common");
describe("Containers Management", function () {
beforeEach(async function () {
this.webExt = await initializeWithTab();
});
describe("creating a new container", () => {
beforeEach(async () => {
await helper.popup.clickElementById("container-add-link");
await helper.popup.clickElementById("edit-container-ok-link");
afterEach(function () {
this.webExt.destroy();
});
describe("creating a new container", function () {
beforeEach(async function () {
await this.webExt.popup.helper.clickElementById("container-add-link");
await this.webExt.popup.helper.clickElementById("edit-container-ok-link");
});
it("should create it in the browser as well", () => {
background.browser.contextualIdentities.create.should.have.been.calledOnce;
it("should create it in the browser as well", function () {
this.webExt.background.browser.contextualIdentities.create.should.have.been.calledOnce;
});
describe("removing it afterwards", () => {
beforeEach(async () => {
await helper.popup.clickElementById("edit-containers-link");
await helper.popup.clickLastMatchingElementByQuerySelector(".delete-container-icon");
await helper.popup.clickElementById("delete-container-ok-link");
describe("removing it afterwards", function () {
beforeEach(async function () {
await this.webExt.popup.helper.clickElementById("edit-containers-link");
await this.webExt.popup.helper.clickElementByQuerySelectorAll(".delete-container-icon", "last");
await this.webExt.popup.helper.clickElementById("delete-container-ok-link");
});
it("should remove it in the browser as well", () => {
background.browser.contextualIdentities.remove.should.have.been.calledOnce;
it("should remove it in the browser as well", function () {
this.webExt.background.browser.contextualIdentities.remove.should.have.been.calledOnce;
});
});
});
+19 -12
View File
@@ -1,17 +1,24 @@
describe("External Webextensions", () => {
const {expect, initializeWithTab} = require("../common");
describe("External Webextensions", function () {
const url = "http://example.com";
beforeEach(async () => {
await helper.browser.initializeWithTab({
beforeEach(async function () {
this.webExt = await initializeWithTab({
cookieStoreId: "firefox-container-1",
url
});
await helper.popup.clickElementById("container-page-assigned");
await this.webExt.popup.helper.clickElementById("container-page-assigned");
});
describe("with contextualIdentities permissions", () => {
it("should be able to get assignments", async () => {
background.browser.management.get.resolves({
afterEach(function () {
this.webExt.destroy();
});
describe("with contextualIdentities permissions", function () {
it("should be able to get assignments", async function () {
this.webExt.background.browser.management.get.resolves({
permissions: ["contextualIdentities"]
});
@@ -23,7 +30,7 @@ describe("External Webextensions", () => {
id: "external-webextension"
};
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
const [promise] = this.webExt.background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
const answer = await promise;
expect(answer.userContextId === "1").to.be.true;
expect(answer.neverAsk === false).to.be.true;
@@ -33,9 +40,9 @@ describe("External Webextensions", () => {
});
});
describe("without contextualIdentities permissions", () => {
it("should throw an error", async () => {
background.browser.management.get.resolves({
describe("without contextualIdentities permissions", function () {
it("should throw an error", async function () {
this.webExt.background.browser.management.get.resolves({
permissions: []
});
@@ -47,7 +54,7 @@ describe("External Webextensions", () => {
id: "external-webextension"
};
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
const [promise] = this.webExt.background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
return promise.catch(error => {
expect(error.message).to.equal("Missing contextualIdentities permission");
});
-64
View File
@@ -1,64 +0,0 @@
describe("Sync", () => {
it("should init sync on startup", async () => {
const tab = await helper.browser.initializeWithTab();
console.log(await background.browser.storage.local.get());
const mozContainer = await background.browser.contextualIdentities.create({
name: "Mozilla",
color: "red",
icon: "briefcase",
});
await background.browser.contextualIdentities.update("firefox-container-2", {color:"purple"});
await background.browser.contextualIdentities.update("firefox-container-4", {icon:"pet"});
await Promise.all([
{
userContextId: "1",
url: "https://twitter.com",
},
{
userContextId: "2",
url: "https://www.facebook.com",
},
{
userContextId: "4",
url: "https://www.linkedin.com",
neverAsk: true,
},
{
userContextId: mozContainer.cookieStoreId.replace("firefox-container-", ""),
url: "https://developer.mozilla.org",
neverAsk: true,
}
].map(async (assign) => {
await background.browser.tabs.update(tab.id, {
cookieStoreId: `firefox-container-${assign.userContextId}`
});
await background.browser.runtime.onMessage.addListener.yield({
method: "setOrRemoveAssignment",
tabId: tab.id,
url: assign.url,
userContextId: assign.userContextId,
value: !true
});
if (assign.neverAsk) {
await nextTick();
await background.browser.runtime.onMessage.addListener.yield({
method: "neverAsk",
neverAsk: true,
pageUrl: assign.url,
});
}
}));
// await background.browser.storage.onChanged.addListener.yield();
await nextTick();
const sync = await background.browser.storage.sync.get();
console.log("sync", sync);
// expect(sync.length).to.equal(4);
});
});