2021-09-23 22:52:32 +02:00
|
|
|
const jsdom = require("jsdom");
|
|
|
|
|
2022-10-29 22:34:17 +02:00
|
|
|
exports.startApplication = async (configFilename, exec) => {
|
2021-09-16 22:36:18 +02:00
|
|
|
jest.resetModules();
|
2022-10-29 22:34:17 +02:00
|
|
|
if (global.app) {
|
|
|
|
await this.stopApplication();
|
|
|
|
}
|
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;
|
2023-03-22 23:53:10 +01:00
|
|
|
global.app = require("../../../js/app");
|
2022-10-29 22:34:17 +02:00
|
|
|
|
2023-02-22 18:58:00 +01:00
|
|
|
return global.app.start();
|
2021-09-16 22:36:18 +02:00
|
|
|
};
|
|
|
|
|
2022-09-20 23:43:06 +02:00
|
|
|
exports.stopApplication = async () => {
|
2023-02-22 18:58:00 +01:00
|
|
|
if (!global.app) {
|
|
|
|
return Promise.resolve();
|
2021-09-16 22:36:18 +02:00
|
|
|
}
|
2023-02-22 18:58:00 +01:00
|
|
|
await global.app.stop();
|
|
|
|
delete global.app;
|
2021-09-16 22:36:18 +02:00
|
|
|
};
|
2021-09-24 23:39:12 +02:00
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
exports.getDocument = () => {
|
|
|
|
return new Promise((resolve) => {
|
2023-03-19 14:32:23 +01:00
|
|
|
const url = `http://${config.address || "localhost"}:${config.port || "8080"}`;
|
2022-10-04 10:15:24 +02:00
|
|
|
jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
|
|
|
|
dom.window.name = "jsdom";
|
2023-09-25 22:27:52 +02:00
|
|
|
global.window = dom.window;
|
2023-09-09 21:12:31 +02:00
|
|
|
dom.window.fetch = fetch;
|
2022-10-04 10:15:24 +02:00
|
|
|
dom.window.onload = () => {
|
|
|
|
global.document = dom.window.document;
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
});
|
2021-09-24 23:39:12 +02:00
|
|
|
});
|
|
|
|
};
|
2022-01-13 00:13:29 +01:00
|
|
|
|
2022-09-20 23:43:06 +02:00
|
|
|
exports.waitForElement = (selector, ignoreValue = "") => {
|
2022-01-13 21:12:15 +01:00
|
|
|
return new Promise((resolve) => {
|
2022-09-20 23:43:06 +02:00
|
|
|
let oldVal = "dummy12345";
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
const element = document.querySelector(selector);
|
|
|
|
if (element) {
|
|
|
|
let newVal = element.textContent;
|
|
|
|
if (newVal === oldVal) {
|
|
|
|
clearInterval(interval);
|
|
|
|
resolve(element);
|
|
|
|
} else {
|
|
|
|
if (ignoreValue === "") {
|
|
|
|
oldVal = newVal;
|
|
|
|
} else {
|
|
|
|
if (!newVal.includes(ignoreValue)) oldVal = newVal;
|
|
|
|
}
|
|
|
|
}
|
2022-01-13 21:12:15 +01:00
|
|
|
}
|
2022-09-20 23:43:06 +02:00
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
};
|
2022-01-13 00:13:29 +01:00
|
|
|
|
2022-09-20 23:43:06 +02:00
|
|
|
exports.waitForAllElements = (selector) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
let oldVal = 999999;
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
const element = document.querySelectorAll(selector);
|
|
|
|
if (element) {
|
|
|
|
let newVal = element.length;
|
|
|
|
if (newVal === oldVal) {
|
|
|
|
clearInterval(interval);
|
|
|
|
resolve(element);
|
|
|
|
} else {
|
|
|
|
if (newVal !== 0) oldVal = newVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 100);
|
2022-01-13 00:13:29 +01:00
|
|
|
});
|
|
|
|
};
|
2022-10-04 10:15:24 +02:00
|
|
|
|
|
|
|
exports.testMatch = async (element, regex) => {
|
|
|
|
const elem = await this.waitForElement(element);
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(elem).not.toBeNull();
|
2022-10-04 10:15:24 +02:00
|
|
|
expect(elem.textContent).toMatch(regex);
|
|
|
|
};
|