mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
const jsdom = require("jsdom");
|
|
|
|
exports.startApplication = function (configFilename, exec) {
|
|
jest.resetModules();
|
|
this.stopApplication();
|
|
// Set config sample for use in test
|
|
if (configFilename === "") {
|
|
process.env.MM_CONFIG_FILE = "config/config.js";
|
|
} else {
|
|
process.env.MM_CONFIG_FILE = configFilename;
|
|
}
|
|
if (exec) exec;
|
|
global.app = require("app.js");
|
|
global.app.start();
|
|
};
|
|
|
|
exports.stopApplication = async function () {
|
|
if (global.app) {
|
|
global.app.stop();
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
};
|
|
|
|
exports.getDocument = function (callback) {
|
|
const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080");
|
|
jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
|
|
dom.window.name = "jsdom";
|
|
dom.window.onload = function () {
|
|
global.MutationObserver = dom.window.MutationObserver;
|
|
global.document = dom.window.document;
|
|
callback();
|
|
};
|
|
});
|
|
};
|
|
|
|
exports.waitForElement = function (selector) {
|
|
return new Promise((resolve) => {
|
|
if (document.querySelector(selector) && document.querySelector(selector).value !== undefined) {
|
|
return resolve(document.querySelector(selector));
|
|
}
|
|
|
|
const observer = new MutationObserver(() => {
|
|
if (document.querySelector(selector) && document.querySelector(selector).value !== undefined) {
|
|
resolve(document.querySelector(selector));
|
|
observer.disconnect();
|
|
}
|
|
});
|
|
|
|
observer.observe(document.body, {
|
|
childList: true,
|
|
subtree: true
|
|
});
|
|
});
|
|
};
|