mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-09-14 16:13:57 +00:00
[tests] refactor translation tests (#3866)
- Remove `sinon` dependency in favor of Jest native mocking - Unify test helper functions across translation test suites - Rename `setupDOMEnvironment` to `createTranslationTestEnvironment` for consistency - Simplify DOM setup by removing unnecessary Promise/async patterns - Avoid potential port conflicts by using port 3001 for translator unit tests - Improve test reliability and maintainability
This commit is contained in:
committed by
GitHub
parent
eb719429d4
commit
4c0a4689c3
@@ -27,6 +27,13 @@ Thanks to: @dathbe.
|
|||||||
- [tests] refactor: add `setupDOMEnvironment` helper function to eliminate repetitive JSDOM setup code (#3860)
|
- [tests] refactor: add `setupDOMEnvironment` helper function to eliminate repetitive JSDOM setup code (#3860)
|
||||||
- [tests] replace `console` with `Log` in calendar `debug.js` to avoid exception in eslint config (#3846)
|
- [tests] replace `console` with `Log` in calendar `debug.js` to avoid exception in eslint config (#3846)
|
||||||
- [tests] speed up e2e tests, cleanup and stabilize weather e2e tests, update snapshot url (#3847, #3848, #3861)
|
- [tests] speed up e2e tests, cleanup and stabilize weather e2e tests, update snapshot url (#3847, #3848, #3861)
|
||||||
|
- [tests] refactor translation tests (#3866)
|
||||||
|
- Remove `sinon` dependency in favor of Jest native mocking
|
||||||
|
- Unify test helper functions across translation test suites
|
||||||
|
- Rename `setupDOMEnvironment` to `createTranslationTestEnvironment` for consistency
|
||||||
|
- Simplify DOM setup by removing unnecessary Promise/async patterns
|
||||||
|
- Avoid potential port conflicts by using port 3001 for translator unit tests
|
||||||
|
- Improve test reliability and maintainability
|
||||||
|
|
||||||
### Updated
|
### Updated
|
||||||
|
|
||||||
|
59
package-lock.json
generated
59
package-lock.json
generated
@@ -52,7 +52,6 @@
|
|||||||
"markdownlint-cli2": "^0.18.1",
|
"markdownlint-cli2": "^0.18.1",
|
||||||
"playwright": "^1.55.0",
|
"playwright": "^1.55.0",
|
||||||
"prettier": "^3.6.2",
|
"prettier": "^3.6.2",
|
||||||
"sinon": "^21.0.0",
|
|
||||||
"stylelint": "^16.23.1",
|
"stylelint": "^16.23.1",
|
||||||
"stylelint-config-standard": "^39.0.0",
|
"stylelint-config-standard": "^39.0.0",
|
||||||
"stylelint-prettier": "^5.0.3"
|
"stylelint-prettier": "^5.0.3"
|
||||||
@@ -2717,28 +2716,6 @@
|
|||||||
"@sinonjs/commons": "^3.0.1"
|
"@sinonjs/commons": "^3.0.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@sinonjs/samsam": {
|
|
||||||
"version": "8.0.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-8.0.2.tgz",
|
|
||||||
"integrity": "sha512-v46t/fwnhejRSFTGqbpn9u+LQ9xJDse10gNnPgAcxgdoCDMXj/G2asWAC/8Qs+BAZDicX+MNZouXT1A7c83kVw==",
|
|
||||||
"dev": true,
|
|
||||||
"license": "BSD-3-Clause",
|
|
||||||
"dependencies": {
|
|
||||||
"@sinonjs/commons": "^3.0.1",
|
|
||||||
"lodash.get": "^4.4.2",
|
|
||||||
"type-detect": "^4.1.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@sinonjs/samsam/node_modules/type-detect": {
|
|
||||||
"version": "4.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.1.0.tgz",
|
|
||||||
"integrity": "sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==",
|
|
||||||
"dev": true,
|
|
||||||
"license": "MIT",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=4"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@socket.io/component-emitter": {
|
"node_modules/@socket.io/component-emitter": {
|
||||||
"version": "3.1.2",
|
"version": "3.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/@socket.io/component-emitter/-/component-emitter-3.1.2.tgz",
|
||||||
@@ -9534,14 +9511,6 @@
|
|||||||
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
|
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/lodash.get": {
|
|
||||||
"version": "4.4.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
|
|
||||||
"integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==",
|
|
||||||
"deprecated": "This package is deprecated. Use the optional chaining (?.) operator instead.",
|
|
||||||
"dev": true,
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/lodash.merge": {
|
"node_modules/lodash.merge": {
|
||||||
"version": "4.6.2",
|
"version": "4.6.2",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
|
||||||
@@ -12791,34 +12760,6 @@
|
|||||||
"url": "https://github.com/sponsors/isaacs"
|
"url": "https://github.com/sponsors/isaacs"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/sinon": {
|
|
||||||
"version": "21.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/sinon/-/sinon-21.0.0.tgz",
|
|
||||||
"integrity": "sha512-TOgRcwFPbfGtpqvZw+hyqJDvqfapr1qUlOizROIk4bBLjlsjlB00Pg6wMFXNtJRpu+eCZuVOaLatG7M8105kAw==",
|
|
||||||
"dev": true,
|
|
||||||
"license": "BSD-3-Clause",
|
|
||||||
"dependencies": {
|
|
||||||
"@sinonjs/commons": "^3.0.1",
|
|
||||||
"@sinonjs/fake-timers": "^13.0.5",
|
|
||||||
"@sinonjs/samsam": "^8.0.1",
|
|
||||||
"diff": "^7.0.0",
|
|
||||||
"supports-color": "^7.2.0"
|
|
||||||
},
|
|
||||||
"funding": {
|
|
||||||
"type": "opencollective",
|
|
||||||
"url": "https://opencollective.com/sinon"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/sinon/node_modules/diff": {
|
|
||||||
"version": "7.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/diff/-/diff-7.0.0.tgz",
|
|
||||||
"integrity": "sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==",
|
|
||||||
"dev": true,
|
|
||||||
"license": "BSD-3-Clause",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.3.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/slash": {
|
"node_modules/slash": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
|
||||||
|
@@ -111,7 +111,6 @@
|
|||||||
"markdownlint-cli2": "^0.18.1",
|
"markdownlint-cli2": "^0.18.1",
|
||||||
"playwright": "^1.55.0",
|
"playwright": "^1.55.0",
|
||||||
"prettier": "^3.6.2",
|
"prettier": "^3.6.2",
|
||||||
"sinon": "^21.0.0",
|
|
||||||
"stylelint": "^16.23.1",
|
"stylelint": "^16.23.1",
|
||||||
"stylelint-config-standard": "^39.0.0",
|
"stylelint-config-standard": "^39.0.0",
|
||||||
"stylelint-prettier": "^5.0.3"
|
"stylelint-prettier": "^5.0.3"
|
||||||
|
@@ -3,22 +3,24 @@ const path = require("node:path");
|
|||||||
const helmet = require("helmet");
|
const helmet = require("helmet");
|
||||||
const { JSDOM } = require("jsdom");
|
const { JSDOM } = require("jsdom");
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const sinon = require("sinon");
|
|
||||||
const translations = require("../../translations/translations");
|
const translations = require("../../translations/translations");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to setup DOM environment.
|
* Helper function to create a fresh Translator instance with DOM environment.
|
||||||
* @returns {object} The JSDOM window object
|
* @returns {object} Object containing window and Translator
|
||||||
*/
|
*/
|
||||||
function setupDOMEnvironment () {
|
function createTranslationTestEnvironment () {
|
||||||
const dom = new JSDOM("", { runScripts: "dangerously", resources: "usable" });
|
// Setup DOM environment with Translator
|
||||||
|
const translatorJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "translator.js"), "utf-8");
|
||||||
|
const dom = new JSDOM("", { url: "http://localhost:3000", runScripts: "outside-only" });
|
||||||
|
|
||||||
dom.window.Log = { log: jest.fn(), error: jest.fn() };
|
dom.window.Log = { log: jest.fn(), error: jest.fn() };
|
||||||
const translatorJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "translator.js"), "utf-8");
|
|
||||||
dom.window.translations = translations;
|
dom.window.translations = translations;
|
||||||
dom.window.eval(translatorJs);
|
dom.window.eval(translatorJs);
|
||||||
|
|
||||||
return dom.window;
|
const window = dom.window;
|
||||||
|
|
||||||
|
return { window, Translator: window.Translator };
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("translations", () => {
|
describe("translations", () => {
|
||||||
@@ -52,91 +54,76 @@ describe("translations", () => {
|
|||||||
let dom;
|
let dom;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// Create a new JSDOM instance for each test
|
// Create a new translation test environment for each test
|
||||||
const window = setupDOMEnvironment();
|
const env = createTranslationTestEnvironment();
|
||||||
dom = { window };
|
const window = env.window;
|
||||||
|
|
||||||
// Additional setup for loadTranslations tests
|
// Load class.js and module.js content directly for loadTranslations tests
|
||||||
dom.window.Translator = {};
|
|
||||||
dom.window.config = { language: "de" };
|
|
||||||
|
|
||||||
// Load class.js and module.js content directly
|
|
||||||
const classJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "class.js"), "utf-8");
|
const classJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "class.js"), "utf-8");
|
||||||
const moduleJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "module.js"), "utf-8");
|
const moduleJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "module.js"), "utf-8");
|
||||||
|
|
||||||
// Execute the scripts in the JSDOM context
|
// Execute the scripts in the JSDOM context
|
||||||
dom.window.eval(classJs);
|
window.eval(classJs);
|
||||||
dom.window.eval(moduleJs);
|
window.eval(moduleJs);
|
||||||
|
|
||||||
|
// Additional setup for loadTranslations tests
|
||||||
|
window.config = { language: "de" };
|
||||||
|
|
||||||
|
dom = { window };
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load translation file", async () => {
|
it("should load translation file", async () => {
|
||||||
await new Promise((resolve) => {
|
|
||||||
dom.window.onload = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
const { Translator, Module, config } = dom.window;
|
const { Translator, Module, config } = dom.window;
|
||||||
config.language = "en";
|
config.language = "en";
|
||||||
Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null);
|
Translator.load = jest.fn().mockImplementation((_m, _f, _fb) => null);
|
||||||
|
|
||||||
Module.register("name", { getTranslations: () => translations });
|
Module.register("name", { getTranslations: () => translations });
|
||||||
const MMM = Module.create("name");
|
const MMM = Module.create("name");
|
||||||
|
|
||||||
await MMM.loadTranslations();
|
await MMM.loadTranslations();
|
||||||
|
|
||||||
expect(Translator.load.args).toHaveLength(1);
|
expect(Translator.load.mock.calls).toHaveLength(1);
|
||||||
expect(Translator.load.calledWith(MMM, "translations/en.json", false)).toBe(true);
|
expect(Translator.load).toHaveBeenCalledWith(MMM, "translations/en.json", false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load translation + fallback file", async () => {
|
it("should load translation + fallback file", async () => {
|
||||||
await new Promise((resolve) => {
|
|
||||||
dom.window.onload = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
const { Translator, Module } = dom.window;
|
const { Translator, Module } = dom.window;
|
||||||
Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null);
|
Translator.load = jest.fn().mockImplementation((_m, _f, _fb) => null);
|
||||||
|
|
||||||
Module.register("name", { getTranslations: () => translations });
|
Module.register("name", { getTranslations: () => translations });
|
||||||
const MMM = Module.create("name");
|
const MMM = Module.create("name");
|
||||||
|
|
||||||
await MMM.loadTranslations();
|
await MMM.loadTranslations();
|
||||||
|
|
||||||
expect(Translator.load.args).toHaveLength(2);
|
expect(Translator.load.mock.calls).toHaveLength(2);
|
||||||
expect(Translator.load.calledWith(MMM, "translations/de.json", false)).toBe(true);
|
expect(Translator.load).toHaveBeenCalledWith(MMM, "translations/de.json", false);
|
||||||
expect(Translator.load.calledWith(MMM, "translations/en.json", true)).toBe(true);
|
expect(Translator.load).toHaveBeenCalledWith(MMM, "translations/en.json", true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load translation fallback file", async () => {
|
it("should load translation fallback file", async () => {
|
||||||
await new Promise((resolve) => {
|
|
||||||
dom.window.onload = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
const { Translator, Module, config } = dom.window;
|
const { Translator, Module, config } = dom.window;
|
||||||
config.language = "--";
|
config.language = "--";
|
||||||
Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null);
|
Translator.load = jest.fn().mockImplementation((_m, _f, _fb) => null);
|
||||||
|
|
||||||
Module.register("name", { getTranslations: () => translations });
|
Module.register("name", { getTranslations: () => translations });
|
||||||
const MMM = Module.create("name");
|
const MMM = Module.create("name");
|
||||||
|
|
||||||
await MMM.loadTranslations();
|
await MMM.loadTranslations();
|
||||||
|
|
||||||
expect(Translator.load.args).toHaveLength(1);
|
expect(Translator.load.mock.calls).toHaveLength(1);
|
||||||
expect(Translator.load.calledWith(MMM, "translations/en.json", true)).toBe(true);
|
expect(Translator.load).toHaveBeenCalledWith(MMM, "translations/en.json", true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load no file", async () => {
|
it("should load no file", async () => {
|
||||||
await new Promise((resolve) => {
|
|
||||||
dom.window.onload = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
const { Translator, Module } = dom.window;
|
const { Translator, Module } = dom.window;
|
||||||
Translator.load = sinon.stub();
|
Translator.load = jest.fn();
|
||||||
|
|
||||||
Module.register("name", {});
|
Module.register("name", {});
|
||||||
const MMM = Module.create("name");
|
const MMM = Module.create("name");
|
||||||
|
|
||||||
await MMM.loadTranslations();
|
await MMM.loadTranslations();
|
||||||
|
|
||||||
expect(Translator.load.callCount).toBe(0);
|
expect(Translator.load.mock.calls).toHaveLength(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -150,13 +137,7 @@ describe("translations", () => {
|
|||||||
describe("parsing language files through the Translator class", () => {
|
describe("parsing language files through the Translator class", () => {
|
||||||
for (const language in translations) {
|
for (const language in translations) {
|
||||||
it(`should parse ${language}`, async () => {
|
it(`should parse ${language}`, async () => {
|
||||||
const window = setupDOMEnvironment();
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
|
|
||||||
await new Promise((resolve) => {
|
|
||||||
window.onload = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
const { Translator } = window;
|
|
||||||
await Translator.load(mmm, translations[language], false);
|
await Translator.load(mmm, translations[language], false);
|
||||||
|
|
||||||
expect(typeof Translator.translations[mmm.name]).toBe("object");
|
expect(typeof Translator.translations[mmm.name]).toBe("object");
|
||||||
@@ -187,16 +168,10 @@ describe("translations", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Function to initialize JSDOM and load translations
|
// Function to initialize JSDOM and load translations
|
||||||
const initializeTranslationDOM = (language) => {
|
const initializeTranslationDOM = async (language) => {
|
||||||
const window = setupDOMEnvironment();
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
window.onload = async () => {
|
|
||||||
const { Translator } = window;
|
|
||||||
await Translator.load(mmm, translations[language], false);
|
await Translator.load(mmm, translations[language], false);
|
||||||
resolve(Translator.translations[mmm.name]);
|
return Translator.translations[mmm.name];
|
||||||
};
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
|
@@ -5,25 +5,22 @@ const { JSDOM } = require("jsdom");
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to setup DOM environment.
|
* Helper function to create a fresh Translator instance with DOM environment.
|
||||||
* @param {string} scriptContent - The script content to evaluate
|
* @returns {object} Object containing window and Translator
|
||||||
* @returns {Promise<object>} The JSDOM window object
|
|
||||||
*/
|
*/
|
||||||
async function setupDOMEnvironment (scriptContent) {
|
function createTranslationTestEnvironment () {
|
||||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
const translatorJs = fs.readFileSync(path.join(__dirname, "..", "..", "..", "js", "translator.js"), "utf-8");
|
||||||
|
const dom = new JSDOM("", { url: "http://localhost:3001", runScripts: "outside-only" });
|
||||||
|
|
||||||
dom.window.eval(scriptContent);
|
|
||||||
dom.window.Log = { log: jest.fn(), error: jest.fn() };
|
dom.window.Log = { log: jest.fn(), error: jest.fn() };
|
||||||
|
dom.window.eval(translatorJs);
|
||||||
|
|
||||||
await new Promise((resolve) => dom.window.onload = resolve);
|
return { window: dom.window, Translator: dom.window.Translator };
|
||||||
return dom.window;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("Translator", () => {
|
describe("Translator", () => {
|
||||||
let server;
|
let server;
|
||||||
const sockets = new Set();
|
const sockets = new Set();
|
||||||
const translatorJsPath = path.join(__dirname, "..", "..", "..", "js", "translator.js");
|
|
||||||
const translatorJsScriptContent = fs.readFileSync(translatorJsPath, "utf8");
|
|
||||||
const translationTestData = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"), "utf8"));
|
const translationTestData = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"), "utf8"));
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
@@ -35,7 +32,7 @@ describe("Translator", () => {
|
|||||||
});
|
});
|
||||||
app.use("/translations", express.static(path.join(__dirname, "..", "..", "..", "tests", "mocks")));
|
app.use("/translations", express.static(path.join(__dirname, "..", "..", "..", "tests", "mocks")));
|
||||||
|
|
||||||
server = app.listen(3000);
|
server = app.listen(3001);
|
||||||
|
|
||||||
server.on("connection", (socket) => {
|
server.on("connection", (socket) => {
|
||||||
sockets.add(socket);
|
sockets.add(socket);
|
||||||
@@ -96,8 +93,7 @@ describe("Translator", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
it("should return custom module translation", async () => {
|
it("should return custom module translation", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
setTranslations(Translator);
|
setTranslations(Translator);
|
||||||
|
|
||||||
let translation = Translator.translate({ name: "MMM-Module" }, "Hello");
|
let translation = Translator.translate({ name: "MMM-Module" }, "Hello");
|
||||||
@@ -108,8 +104,7 @@ describe("Translator", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should return core translation", async () => {
|
it("should return core translation", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
setTranslations(Translator);
|
setTranslations(Translator);
|
||||||
let translation = Translator.translate({ name: "MMM-Module" }, "FOO");
|
let translation = Translator.translate({ name: "MMM-Module" }, "FOO");
|
||||||
expect(translation).toBe("Foo");
|
expect(translation).toBe("Foo");
|
||||||
@@ -118,32 +113,28 @@ describe("Translator", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should return custom module translation fallback", async () => {
|
it("should return custom module translation fallback", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
setTranslations(Translator);
|
setTranslations(Translator);
|
||||||
const translation = Translator.translate({ name: "MMM-Module" }, "A key");
|
const translation = Translator.translate({ name: "MMM-Module" }, "A key");
|
||||||
expect(translation).toBe("A translation");
|
expect(translation).toBe("A translation");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return core translation fallback", async () => {
|
it("should return core translation fallback", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
setTranslations(Translator);
|
setTranslations(Translator);
|
||||||
const translation = Translator.translate({ name: "MMM-Module" }, "Fallback");
|
const translation = Translator.translate({ name: "MMM-Module" }, "Fallback");
|
||||||
expect(translation).toBe("core fallback");
|
expect(translation).toBe("core fallback");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return translation with placeholder for missing variables", async () => {
|
it("should return translation with placeholder for missing variables", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
setTranslations(Translator);
|
setTranslations(Translator);
|
||||||
const translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}");
|
const translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}");
|
||||||
expect(translation).toBe("Hallo {username}");
|
expect(translation).toBe("Hallo {username}");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return key if no translation was found", async () => {
|
it("should return key if no translation was found", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
setTranslations(Translator);
|
setTranslations(Translator);
|
||||||
const translation = Translator.translate({ name: "MMM-Module" }, "MISSING");
|
const translation = Translator.translate({ name: "MMM-Module" }, "MISSING");
|
||||||
expect(translation).toBe("MISSING");
|
expect(translation).toBe("MISSING");
|
||||||
@@ -154,13 +145,12 @@ describe("Translator", () => {
|
|||||||
const mmm = {
|
const mmm = {
|
||||||
name: "TranslationTest",
|
name: "TranslationTest",
|
||||||
file (file) {
|
file (file) {
|
||||||
return `http://localhost:3000/translations/${file}`;
|
return `http://localhost:3001/translations/${file}`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
it("should load translations", async () => {
|
it("should load translations", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
const file = "translation_test.json";
|
const file = "translation_test.json";
|
||||||
|
|
||||||
await Translator.load(mmm, file, false);
|
await Translator.load(mmm, file, false);
|
||||||
@@ -169,8 +159,7 @@ describe("Translator", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should load translation fallbacks", async () => {
|
it("should load translation fallbacks", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
const file = "translation_test.json";
|
const file = "translation_test.json";
|
||||||
|
|
||||||
await Translator.load(mmm, file, true);
|
await Translator.load(mmm, file, true);
|
||||||
@@ -179,8 +168,7 @@ describe("Translator", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should not load translations, if module fallback exists", async () => {
|
it("should not load translations, if module fallback exists", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { Translator } = createTranslationTestEnvironment();
|
||||||
const { Translator } = window;
|
|
||||||
const file = "translation_test.json";
|
const file = "translation_test.json";
|
||||||
|
|
||||||
Translator.translationsFallback[mmm.name] = {
|
Translator.translationsFallback[mmm.name] = {
|
||||||
@@ -197,29 +185,23 @@ describe("Translator", () => {
|
|||||||
|
|
||||||
describe("loadCoreTranslations", () => {
|
describe("loadCoreTranslations", () => {
|
||||||
it("should load core translations and fallback", async () => {
|
it("should load core translations and fallback", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { window, Translator } = createTranslationTestEnvironment();
|
||||||
window.translations = { en: "http://localhost:3000/translations/translation_test.json" };
|
window.translations = { en: "http://localhost:3001/translations/translation_test.json" };
|
||||||
const { Translator } = window;
|
|
||||||
await Translator.loadCoreTranslations("en");
|
await Translator.loadCoreTranslations("en");
|
||||||
|
|
||||||
const en = translationTestData;
|
const en = translationTestData;
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
||||||
|
|
||||||
expect(Translator.coreTranslations).toEqual(en);
|
expect(Translator.coreTranslations).toEqual(en);
|
||||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load core fallback if language cannot be found", async () => {
|
it("should load core fallback if language cannot be found", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { window, Translator } = createTranslationTestEnvironment();
|
||||||
window.translations = { en: "http://localhost:3000/translations/translation_test.json" };
|
window.translations = { en: "http://localhost:3001/translations/translation_test.json" };
|
||||||
const { Translator } = window;
|
|
||||||
await Translator.loadCoreTranslations("MISSINGLANG");
|
await Translator.loadCoreTranslations("MISSINGLANG");
|
||||||
|
|
||||||
const en = translationTestData;
|
const en = translationTestData;
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
||||||
|
|
||||||
expect(Translator.coreTranslations).toEqual({});
|
expect(Translator.coreTranslations).toEqual({});
|
||||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||||
});
|
});
|
||||||
@@ -227,26 +209,20 @@ describe("Translator", () => {
|
|||||||
|
|
||||||
describe("loadCoreTranslationsFallback", () => {
|
describe("loadCoreTranslationsFallback", () => {
|
||||||
it("should load core translations fallback", async () => {
|
it("should load core translations fallback", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { window, Translator } = createTranslationTestEnvironment();
|
||||||
window.translations = { en: "http://localhost:3000/translations/translation_test.json" };
|
window.translations = { en: "http://localhost:3001/translations/translation_test.json" };
|
||||||
const { Translator } = window;
|
|
||||||
await Translator.loadCoreTranslationsFallback();
|
await Translator.loadCoreTranslationsFallback();
|
||||||
|
|
||||||
const en = translationTestData;
|
const en = translationTestData;
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
||||||
|
|
||||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should load core fallback if language cannot be found", async () => {
|
it("should load core fallback if language cannot be found", async () => {
|
||||||
const window = await setupDOMEnvironment(translatorJsScriptContent);
|
const { window, Translator } = createTranslationTestEnvironment();
|
||||||
window.translations = {};
|
window.translations = {};
|
||||||
const { Translator } = window;
|
|
||||||
await Translator.loadCoreTranslations();
|
await Translator.loadCoreTranslations();
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
||||||
|
|
||||||
expect(Translator.coreTranslationsFallback).toEqual({});
|
expect(Translator.coreTranslationsFallback).toEqual({});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user