Files
MagicMirror/tests/unit/functions/cmp_versions_spec.js
Kristjan ESPERANTO d4a5ebe273 refactor: use ES module imports in browser core (#4158)
With these changes a few browser-side core files now use native ES
modules. `Loader`, `MMSocket`, `Module` and `MM` can be imported
directly instead of being read off `window`. `main.js` and `loader.js`
are no longer wrapped in IIFEs - they're just normal modules now.

`Module`, `MM` and `MMSocket` are still exposed as globals, so
third-party modules that use the old API keep working.

The changes are mostly structural, behavior should stay the same. A few
internal helpers in `main.js` got an underscore prefix because their
names clashed with public `MM` methods.

## Why

The old setup relied a lot on script order: a file could use `Loader` or
`MMSocket` only because another script happened to put it on `window`
first. Imports make that explicit.

The bigger goal is to move away from the legacy script-loading patterns
- making it easier to understand and easier to test - in other words:
easier to maintain.

More of the core could be "cleaned up" the same way, but that would blow
up this PR.

For reviewing, I recommend to hide the whitespace changes.
2026-05-18 19:58:44 +02:00

72 lines
1.8 KiB
JavaScript

const path = require("node:path");
const { pathToFileURL } = require("node:url");
describe("Test function cmpVersions in js/module.js", () => {
let cmp;
let originalWindow;
let originalLog;
let originalConfig;
let originalMM;
let originalTranslator;
let originalNunjucks;
beforeAll(async () => {
originalWindow = global.window;
originalLog = global.Log;
originalConfig = global.config;
originalMM = global.MM;
originalTranslator = global.Translator;
originalNunjucks = global.nunjucks;
global.window = { mmVersion: "2.0.0" };
global.Log = { log: () => {}, info: () => {}, warn: () => {}, error: () => {}, debug: () => {} };
global.config = { language: "en" };
global.MM = {
hideModule: () => {},
showModule: () => {},
sendNotification: () => {},
updateDom: () => {}
};
global.Translator = {
load: () => Promise.resolve(),
translate: () => ""
};
global.nunjucks = {
Environment () {
this.addFilter = () => {};
this.renderString = () => "";
this.render = (_template, _data, callback) => callback(null, "");
},
WebLoader () {},
runtime: {
markSafe: (str) => str
}
};
const modulePath = pathToFileURL(path.join(__dirname, "..", "..", "..", "js", "module.js")).href;
const loaded = await import(`${modulePath}?test=${Date.now()}`);
cmp = loaded.cmpVersions;
});
afterAll(() => {
global.window = originalWindow;
global.Log = originalLog;
global.config = originalConfig;
global.MM = originalMM;
global.Translator = originalTranslator;
global.nunjucks = originalNunjucks;
});
it("should return -1 when comparing 2.1 to 2.2", () => {
expect(cmp("2.1", "2.2")).toBe(-1);
});
it("should be return 0 when comparing 2.2 to 2.2", () => {
expect(cmp("2.2", "2.2")).toBe(0);
});
it("should be return 1 when comparing 1.1 to 1.0", () => {
expect(cmp("1.1", "1.0")).toBe(1);
});
});