Replace manual mock/jsdom with webextensions-jsdom
This commit is contained in:
committed by
Jonathan Kingston
parent
96ab4685e9
commit
77fd843b3d
+45
-65
@@ -2,7 +2,6 @@ if (!process.listenerCount("unhandledRejection")) {
|
||||
// eslint-disable-next-line no-console
|
||||
process.on("unhandledRejection", r => console.log(r));
|
||||
}
|
||||
const jsdom = require("jsdom");
|
||||
const path = require("path");
|
||||
const chai = require("chai");
|
||||
const sinonChai = require("sinon-chai");
|
||||
@@ -19,83 +18,64 @@ global.nextTick = () => {
|
||||
};
|
||||
|
||||
global.helper = require("./helper");
|
||||
const browserMock = require("./browser.mock");
|
||||
const srcBasePath = path.resolve(path.join(__dirname, "..", "src"));
|
||||
const srcJsBackgroundPath = path.join(srcBasePath, "js", "background");
|
||||
global.buildBackgroundDom = async (options = {}) => {
|
||||
const dom = await jsdom.JSDOM.fromFile(path.join(srcJsBackgroundPath, "index.html"), {
|
||||
runScripts: "dangerously",
|
||||
resources: "usable",
|
||||
virtualConsole: (new jsdom.VirtualConsole).sendTo(console),
|
||||
beforeParse(window) {
|
||||
window.browser = browserMock();
|
||||
window.fetch = sinon.stub().resolves({
|
||||
json: sinon.stub().resolves({})
|
||||
});
|
||||
|
||||
if (options.beforeParse) {
|
||||
options.beforeParse(window);
|
||||
const webExtensionsJSDOM = require("webextensions-jsdom");
|
||||
const manifestPath = path.resolve(path.join(__dirname, "../src/manifest.json"));
|
||||
|
||||
global.buildDom = async ({background = {}, popup = {}}) => {
|
||||
background = {
|
||||
...background,
|
||||
jsdom: {
|
||||
...background.jsom,
|
||||
beforeParse(window) {
|
||||
window.browser.permissions.getAll.resolves({permissions: ["bookmarks"]});
|
||||
}
|
||||
}
|
||||
});
|
||||
await new Promise(resolve => {
|
||||
dom.window.document.addEventListener("DOMContentLoaded", resolve);
|
||||
});
|
||||
await nextTick();
|
||||
|
||||
global.background = {
|
||||
dom,
|
||||
browser: dom.window.browser
|
||||
};
|
||||
};
|
||||
|
||||
global.buildPopupDom = async (options = {}) => {
|
||||
const dom = await jsdom.JSDOM.fromFile(path.join(srcBasePath, "popup.html"), {
|
||||
runScripts: "dangerously",
|
||||
resources: "usable",
|
||||
virtualConsole: (new jsdom.VirtualConsole).sendTo(console),
|
||||
beforeParse(window) {
|
||||
window.browser = browserMock();
|
||||
window.browser.storage.local.set("browserActionBadgesClicked", []);
|
||||
window.browser.storage.local.set("onboarding-stage", 5);
|
||||
window.browser.storage.local.set("achievements", []);
|
||||
window.browser.storage.local.set.resetHistory();
|
||||
window.fetch = sinon.stub().resolves({
|
||||
json: sinon.stub().resolves({})
|
||||
});
|
||||
|
||||
if (options.beforeParse) {
|
||||
options.beforeParse(window);
|
||||
}
|
||||
popup = {
|
||||
...popup,
|
||||
jsdom: {
|
||||
...popup.jsdom,
|
||||
pretendToBeVisual: true
|
||||
}
|
||||
});
|
||||
await new Promise(resolve => {
|
||||
dom.window.document.addEventListener("DOMContentLoaded", resolve);
|
||||
});
|
||||
await nextTick();
|
||||
dom.window.browser.runtime.sendMessage.resetHistory();
|
||||
|
||||
if (global.background) {
|
||||
dom.window.browser.runtime.sendMessage = sinon.spy(function() {
|
||||
global.background.browser.runtime.onMessage.addListener.yield(...arguments);
|
||||
});
|
||||
}
|
||||
|
||||
global.popup = {
|
||||
dom,
|
||||
document: dom.window.document,
|
||||
browser: dom.window.browser
|
||||
};
|
||||
|
||||
const webExtension = await webExtensionsJSDOM.fromManifest(manifestPath, {
|
||||
apiFake: true,
|
||||
wiring: true,
|
||||
sinon: global.sinon,
|
||||
background,
|
||||
popup
|
||||
});
|
||||
|
||||
// eslint-disable-next-line require-atomic-updates
|
||||
global.background = webExtension.background;
|
||||
// eslint-disable-next-line require-atomic-updates
|
||||
global.popup = webExtension.popup;
|
||||
};
|
||||
|
||||
global.buildBackgroundDom = async background => {
|
||||
await global.buildDom({
|
||||
background,
|
||||
popup: false
|
||||
});
|
||||
};
|
||||
|
||||
global.buildPopupDom = async popup => {
|
||||
await global.buildDom({
|
||||
popup,
|
||||
background: false
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
global.afterEach(() => {
|
||||
if (global.background) {
|
||||
global.background.dom.window.close();
|
||||
delete global.background;
|
||||
global.background.destroy();
|
||||
}
|
||||
|
||||
if (global.popup) {
|
||||
global.popup.dom.window.close();
|
||||
delete global.popup;
|
||||
global.popup.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user