From 42a9a6e766f47eb0102b32b991cf023ff98bb50f Mon Sep 17 00:00:00 2001 From: Francis McKenzie Date: Wed, 15 Jan 2020 18:21:15 +0100 Subject: [PATCH] Container locking - fixed broken tests due to rebasing against master --- package.json | 2 +- test/features/lock.test.js | 36 +++++++++++++++++++++++------------- test/helper.js | 5 +++-- test/setup.js | 3 +++ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 753f292..90dce9a 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "stylelint-config-standard": "^16.0.0", "stylelint-order": "^0.3.0", "web-ext": "^2.9.3", - "webextensions-jsdom": "^1.1.0" + "webextensions-jsdom": "^1.1.1" }, "homepage": "https://github.com/mozilla/multi-account-containers#readme", "license": "MPL-2.0", diff --git a/test/features/lock.test.js b/test/features/lock.test.js index 62bb313..4ea0049 100644 --- a/test/features/lock.test.js +++ b/test/features/lock.test.js @@ -1,14 +1,14 @@ // https://github.com/mozilla/multi-account-containers/issues/847 describe("Lock Feature", () => { - const activeTab = { - id: 1, - cookieStoreId: "firefox-container-1", - url: "http://example.com", - index: 0, - active: true - }; + const url1 = "http://example.com"; + const url2 = "http://example2.com"; + + let activeTab; beforeEach(async () => { - await helper.browser.initializeWithTab(activeTab); + activeTab = await helper.browser.initializeWithTab({ + cookieStoreId: "firefox-container-1", + url: url1 + }); }); describe("click the 'Always open in' checkbox in the popup", () => { @@ -18,9 +18,8 @@ describe("Lock Feature", () => { }); describe("open different URL in same tab", () => { - const differentURL = "http://example2.com"; beforeEach(async () => { - await helper.browser.browseToURL(activeTab.id, differentURL); + await helper.browser.browseToURL(activeTab.id, url2); }); it("should not open a new tab", () => { @@ -32,20 +31,31 @@ describe("Lock Feature", () => { await helper.popup.setContainerIsLocked(activeTab.cookieStoreId, true); }); - describe("open different URL in same tab", () => { + describe("wait 2 seconds, then open different URL in same tab", () => { beforeEach(async () => { - await helper.browser.browseToURL(activeTab.id, differentURL); + // Note: must wait 2 seconds, because of code in messageHandler that assumes + // a newly-created tab is not 'genuine' until 2 seconds have elapsed since + // its creation. + // Unfortunately, this test runner recreates the tab at every single 'beforeEach', + // so messageHandler thinks the tab is not genuine and tries to remove it, + // meaning the below tests will (incorrectly) fail. + await global.sleep(2500); + await helper.browser.browseToURL(activeTab.id, url2); }); it("should open a new tab in the default container", () => { background.browser.tabs.create.should.have.been.calledWith(sinon.match({ - url: differentURL, + url: url2, cookieStoreId: "firefox-default", openerTabId: activeTab.id, index: activeTab.index + 1, active: activeTab.active })); }); + + it("should not remove the original tab", () => { + background.browser.tabs.remove.should.not.have.been.called; + }); }); }); }); diff --git a/test/helper.js b/test/helper.js index 21ca95c..611ee84 100644 --- a/test/helper.js +++ b/test/helper.js @@ -11,6 +11,8 @@ module.exports = { } }, popup: { + // Required to access variables, because nyc messes up 'eval' + script: "function evalScript(v) { return eval(v); }", jsdom: { beforeParse(window) { window.browser.storage.local.set({ @@ -31,7 +33,6 @@ module.exports = { return background.browser.tabs._create(tab, options); }, - // https://github.com/mozilla/multi-account-containers/issues/847 async browseToURL(tabId, url) { const [promise] = background.browser.webRequest.onBeforeRequest.addListener.yield({ frameId: 0, @@ -53,7 +54,7 @@ module.exports = { // https://github.com/mozilla/multi-account-containers/issues/847 async setContainerIsLocked(cookieStoreId, isLocked) { - const Logic = popup.dom.window.eval("Logic"); + const Logic = popup.window.evalScript("Logic"); const userContextId = Logic.userContextId(cookieStoreId); await Logic.lockOrUnlockContainer(userContextId, isLocked); } diff --git a/test/setup.js b/test/setup.js index 0cc45ee..0a7f51f 100644 --- a/test/setup.js +++ b/test/setup.js @@ -16,6 +16,9 @@ global.nextTick = () => { }); }); }; +global.sleep = (delay) => { + return new Promise((resolve) => { setTimeout(resolve, delay); }); +}; global.helper = require("./helper");