2021-09-23 22:52:32 +02:00
|
|
|
const jsdom = require("jsdom");
|
|
|
|
|
2021-09-24 23:39:12 +02:00
|
|
|
exports.startApplication = function (configFilename, exec) {
|
2021-09-16 22:36:18 +02:00
|
|
|
jest.resetModules();
|
2021-09-25 23:45:34 +02:00
|
|
|
if (global.app) {
|
|
|
|
global.app.stop();
|
2021-09-25 23:37:37 +02:00
|
|
|
}
|
2021-09-16 22:36:18 +02:00
|
|
|
// Set config sample for use in test
|
2021-10-16 00:05:12 +02:00
|
|
|
if (configFilename === "") {
|
|
|
|
process.env.MM_CONFIG_FILE = "config/config.js";
|
|
|
|
} else {
|
|
|
|
process.env.MM_CONFIG_FILE = configFilename;
|
|
|
|
}
|
2021-09-16 22:36:18 +02:00
|
|
|
if (exec) exec;
|
2021-09-25 23:45:34 +02:00
|
|
|
global.app = require("app.js");
|
|
|
|
global.app.start();
|
2021-09-16 22:36:18 +02:00
|
|
|
};
|
|
|
|
|
2021-09-25 23:45:34 +02:00
|
|
|
exports.stopApplication = function () {
|
|
|
|
if (global.app) {
|
|
|
|
global.app.stop();
|
2021-09-16 22:36:18 +02:00
|
|
|
}
|
|
|
|
};
|
2021-09-24 23:39:12 +02:00
|
|
|
|
2022-01-13 22:33:57 +01:00
|
|
|
exports.getDocument = function (callback) {
|
2021-09-24 23:39:12 +02:00
|
|
|
const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080");
|
|
|
|
jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
|
2021-09-28 22:08:21 +02:00
|
|
|
dom.window.name = "jsdom";
|
2021-09-24 23:39:12 +02:00
|
|
|
dom.window.onload = function () {
|
2022-01-13 00:13:29 +01:00
|
|
|
global.MutationObserver = dom.window.MutationObserver;
|
2021-09-24 23:39:12 +02:00
|
|
|
global.document = dom.window.document;
|
2022-01-13 22:33:57 +01:00
|
|
|
callback();
|
2021-09-24 23:39:12 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2022-01-13 00:13:29 +01:00
|
|
|
|
2022-01-13 21:12:15 +01:00
|
|
|
exports.waitForElement = function (selector) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (document.querySelector(selector) && document.querySelector(selector).value !== undefined) {
|
|
|
|
return resolve(document.querySelector(selector));
|
|
|
|
}
|
2022-01-13 00:13:29 +01:00
|
|
|
|
2022-01-13 21:12:15 +01:00
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
if (document.querySelector(selector) && document.querySelector(selector).value !== undefined) {
|
|
|
|
resolve(document.querySelector(selector));
|
|
|
|
observer.disconnect();
|
|
|
|
}
|
|
|
|
});
|
2022-01-13 00:13:29 +01:00
|
|
|
|
2022-01-13 21:12:15 +01:00
|
|
|
observer.observe(document.body, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
});
|
2022-01-13 00:13:29 +01:00
|
|
|
});
|
|
|
|
};
|