mirror of
https://github.com/MichMich/MagicMirror.git
synced 2026-06-09 03:34:51 +00:00
config endpoint must handle functions in module configs (#4106)
Fixes #4105 ```bash In JavaScript, standard JSON does not support functions. If you use JSON.stringify() on an object containing functions, those functions will be omitted (if they are object properties) or changed to null (if they are in an array). ``` --------- Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
This commit is contained in:
35
tests/configs/config_functions.js
Normal file
35
tests/configs/config_functions.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^roundToInt2$" }]*/
|
||||
|
||||
let config = require(`${process.cwd()}/tests/configs/default.js`).configFactory({
|
||||
modules: [
|
||||
{
|
||||
module: "clock",
|
||||
position: "middle_center",
|
||||
config: {
|
||||
moduleFunctions: {
|
||||
roundToInt1: (value) => {
|
||||
try {
|
||||
return Math.round(parseFloat(value));
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
},
|
||||
roundToInt2: function (value) {
|
||||
try {
|
||||
return Math.round(parseFloat(value));
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
},
|
||||
stringWithArrow: "a => b is not a function",
|
||||
stringWithFunction: "this function keyword is just text"
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
/*************** DO NOT EDIT THE LINE BELOW ***************/
|
||||
if (typeof module !== "undefined") {
|
||||
module.exports = config;
|
||||
}
|
||||
21
tests/e2e/config_functions_spec.js
Normal file
21
tests/e2e/config_functions_spec.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const helpers = require("./helpers/global-setup");
|
||||
|
||||
describe("config with module function", () => {
|
||||
beforeAll(async () => {
|
||||
await helpers.startApplication("tests/configs/config_functions.js");
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await helpers.stopApplication();
|
||||
});
|
||||
|
||||
it("config should resolve module functions", () => {
|
||||
expect(config.modules[0].config.moduleFunctions.roundToInt1(13.3)).toBe(13);
|
||||
expect(config.modules[0].config.moduleFunctions.roundToInt2(13.3)).toBe(13);
|
||||
});
|
||||
|
||||
it("config should not revive plain strings containing arrow or function keywords", () => {
|
||||
expect(config.modules[0].config.stringWithArrow).toBe("a => b is not a function");
|
||||
expect(config.modules[0].config.stringWithFunction).toBe("this function keyword is just text");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user