mirror of
https://github.com/MichMich/MagicMirror.git
synced 2026-07-23 07:44:20 -07:00
ed95826d19
Another step towards ES modules: this converts `Translator` to a proper module and imports it where needed, instead of relying on it being globally available. The goal is to gradually get rid of the old global-script style in the core. It makes dependencies explicit, reduces hidden coupling and keeps the codebase easier to maintain going forward. The code change is tiny - the bigger and harder part was adapting the tests to the new loading behavior.
44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
const path = require("node:path");
|
|
const { pathToFileURL } = require("node:url");
|
|
const { JSDOM } = require("jsdom");
|
|
|
|
const TRANSLATOR_MODULE_URL = pathToFileURL(path.join(__dirname, "..", "..", "js", "translator.js")).href;
|
|
|
|
/**
|
|
* Reset mutable Translator state between tests.
|
|
* Clears all non-function plain-object properties to remain resilient
|
|
* when additional translation stores are introduced.
|
|
* @param {object} Translator The shared Translator module instance.
|
|
*/
|
|
function resetTranslatorState (Translator) {
|
|
for (const [key, value] of Object.entries(Translator)) {
|
|
if (typeof value === "function") {
|
|
continue;
|
|
}
|
|
|
|
if (Object.prototype.toString.call(value) === "[object Object]") {
|
|
Translator[key] = {};
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set up DOM globals used by translation tests.
|
|
* @param {number} [port] Base URI port used to resolve relative translation paths.
|
|
* @returns {void}
|
|
*/
|
|
function setupTranslationTestEnvironment (port = 3000) {
|
|
const dom = new JSDOM("", { url: `http://localhost:${port}` });
|
|
|
|
global.document = dom.window.document;
|
|
if (!global.Log) {
|
|
global.Log = { log: vi.fn(), error: vi.fn() };
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
setupTranslationTestEnvironment,
|
|
TRANSLATOR_MODULE_URL,
|
|
resetTranslatorState
|
|
};
|