mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-22 05:07:05 +00:00
[tests] Review and refactor translation tests (#3792)
I have refactored the translations tests, they should now be clearer and easier to understand. There should be no functional impact. I have discarded the original approach of also replacing `XMLHttpRequest` with `fetch` in the file `js/translator.js`. I had managed to get it to work functionally, but I couldn't get the tests to work.
This commit is contained in:
committed by
GitHub
parent
c7c0e67c1d
commit
2809ed1750
@@ -1,12 +1,15 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const helmet = require("helmet");
|
||||
const { JSDOM } = require("jsdom");
|
||||
const express = require("express");
|
||||
|
||||
const sockets = new Set();
|
||||
|
||||
describe("Translator", () => {
|
||||
let server;
|
||||
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"));
|
||||
|
||||
beforeAll(() => {
|
||||
const app = express();
|
||||
@@ -77,86 +80,82 @@ describe("Translator", () => {
|
||||
Translator.coreTranslationsFallback = coreTranslationsFallback;
|
||||
};
|
||||
|
||||
it("should return custom module translation", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = () => {
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
let translation = Translator.translate({ name: "MMM-Module" }, "Hello");
|
||||
expect(translation).toBe("Hallo");
|
||||
translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}", { username: "fewieden" });
|
||||
expect(translation).toBe("Hallo fewieden");
|
||||
done();
|
||||
};
|
||||
});
|
||||
it("should return custom module translation", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
|
||||
let translation = Translator.translate({ name: "MMM-Module" }, "Hello");
|
||||
expect(translation).toBe("Hallo");
|
||||
|
||||
translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}", { username: "fewieden" });
|
||||
expect(translation).toBe("Hallo fewieden");
|
||||
});
|
||||
|
||||
it("should return core translation", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = () => {
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
let translation = Translator.translate({ name: "MMM-Module" }, "FOO");
|
||||
expect(translation).toBe("Foo");
|
||||
translation = Translator.translate({ name: "MMM-Module" }, "BAR {something}", { something: "Lorem Ipsum" });
|
||||
expect(translation).toBe("Bar Lorem Ipsum");
|
||||
done();
|
||||
};
|
||||
});
|
||||
it("should return core translation", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
let translation = Translator.translate({ name: "MMM-Module" }, "FOO");
|
||||
expect(translation).toBe("Foo");
|
||||
translation = Translator.translate({ name: "MMM-Module" }, "BAR {something}", { something: "Lorem Ipsum" });
|
||||
expect(translation).toBe("Bar Lorem Ipsum");
|
||||
});
|
||||
|
||||
it("should return custom module translation fallback", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = () => {
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
const translation = Translator.translate({ name: "MMM-Module" }, "A key");
|
||||
expect(translation).toBe("A translation");
|
||||
done();
|
||||
};
|
||||
});
|
||||
it("should return custom module translation fallback", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
const translation = Translator.translate({ name: "MMM-Module" }, "A key");
|
||||
expect(translation).toBe("A translation");
|
||||
});
|
||||
|
||||
it("should return core translation fallback", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = () => {
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
const translation = Translator.translate({ name: "MMM-Module" }, "Fallback");
|
||||
expect(translation).toBe("core fallback");
|
||||
done();
|
||||
};
|
||||
});
|
||||
it("should return core translation fallback", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
const translation = Translator.translate({ name: "MMM-Module" }, "Fallback");
|
||||
expect(translation).toBe("core fallback");
|
||||
});
|
||||
|
||||
it("should return translation with placeholder for missing variables", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = () => {
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
const translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}");
|
||||
expect(translation).toBe("Hallo {username}");
|
||||
done();
|
||||
};
|
||||
});
|
||||
it("should return translation with placeholder for missing variables", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
const translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}");
|
||||
expect(translation).toBe("Hallo {username}");
|
||||
});
|
||||
|
||||
it("should return key if no translation was found", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = () => {
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
const translation = Translator.translate({ name: "MMM-Module" }, "MISSING");
|
||||
expect(translation).toBe("MISSING");
|
||||
done();
|
||||
};
|
||||
});
|
||||
it("should return key if no translation was found", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const { Translator } = dom.window;
|
||||
setTranslations(Translator);
|
||||
const translation = Translator.translate({ name: "MMM-Module" }, "MISSING");
|
||||
expect(translation).toBe("MISSING");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -168,144 +167,127 @@ describe("Translator", () => {
|
||||
}
|
||||
};
|
||||
|
||||
it("should load translations", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script>var Log = {log: () => {}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = async () => {
|
||||
const { Translator } = dom.window;
|
||||
const file = "translation_test.json";
|
||||
it("should load translations", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
await Translator.load(mmm, file, false);
|
||||
const json = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", file));
|
||||
expect(Translator.translations[mmm.name]).toEqual(json);
|
||||
done();
|
||||
};
|
||||
});
|
||||
const { Translator } = dom.window;
|
||||
const file = "translation_test.json";
|
||||
|
||||
await Translator.load(mmm, file, false);
|
||||
const json = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "..", "tests", "mocks", file), "utf8"));
|
||||
expect(Translator.translations[mmm.name]).toEqual(json);
|
||||
});
|
||||
|
||||
it("should load translation fallbacks", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script>var Log = {log: () => {}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = async () => {
|
||||
const { Translator } = dom.window;
|
||||
const file = "translation_test.json";
|
||||
it("should load translation fallbacks", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
|
||||
await Translator.load(mmm, file, true);
|
||||
const json = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", file));
|
||||
expect(Translator.translationsFallback[mmm.name]).toEqual(json);
|
||||
done();
|
||||
};
|
||||
});
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const { Translator } = dom.window;
|
||||
const file = "translation_test.json";
|
||||
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
await Translator.load(mmm, file, true);
|
||||
const json = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "..", "tests", "mocks", file), "utf8"));
|
||||
expect(Translator.translationsFallback[mmm.name]).toEqual(json);
|
||||
});
|
||||
|
||||
it("should not load translations, if module fallback exists", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(`<script>var Log = {log: () => {}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.onload = async () => {
|
||||
const { Translator, XMLHttpRequest } = dom.window;
|
||||
const file = "translation_test.json";
|
||||
it("should not load translations, if module fallback exists", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
XMLHttpRequest.prototype.send = () => {
|
||||
throw new Error("Shouldn't load files");
|
||||
};
|
||||
const { Translator } = dom.window;
|
||||
const file = "translation_test.json";
|
||||
|
||||
Translator.translationsFallback[mmm.name] = {
|
||||
Hello: "Hallo"
|
||||
};
|
||||
|
||||
await Translator.load(mmm, file, false);
|
||||
expect(Translator.translations[mmm.name]).toBeUndefined();
|
||||
expect(Translator.translationsFallback[mmm.name]).toEqual({
|
||||
Hello: "Hallo"
|
||||
});
|
||||
done();
|
||||
};
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
Translator.translationsFallback[mmm.name] = {
|
||||
Hello: "Hallo"
|
||||
};
|
||||
|
||||
await Translator.load(mmm, file, false);
|
||||
expect(Translator.translations[mmm.name]).toBeUndefined();
|
||||
expect(Translator.translationsFallback[mmm.name]).toEqual({
|
||||
Hello: "Hallo"
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadCoreTranslations", () => {
|
||||
it("should load core translations and fallback", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(
|
||||
`<script>var translations = {en: "http://localhost:3000/translations/translation_test.json"}; var Log = {log: () => {}};</script>\
|
||||
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
||||
{ runScripts: "dangerously", resources: "usable" }
|
||||
);
|
||||
dom.window.onload = async () => {
|
||||
const { Translator } = dom.window;
|
||||
await Translator.loadCoreTranslations("en");
|
||||
it("should load core translations and fallback", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
dom.window.translations = { en: "http://localhost:3000/translations/translation_test.json" };
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
|
||||
setTimeout(() => {
|
||||
expect(Translator.coreTranslations).toEqual(en);
|
||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||
done();
|
||||
}, 500);
|
||||
};
|
||||
});
|
||||
const { Translator } = dom.window;
|
||||
await Translator.loadCoreTranslations("en");
|
||||
|
||||
const en = translationTestData;
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
expect(Translator.coreTranslations).toEqual(en);
|
||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||
});
|
||||
|
||||
it("should load core fallback if language cannot be found", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(
|
||||
`<script>var translations = {en: "http://localhost:3000/translations/translation_test.json"}; var Log = {log: () => {}};</script>\
|
||||
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
||||
{ runScripts: "dangerously", resources: "usable" }
|
||||
);
|
||||
dom.window.onload = async () => {
|
||||
const { Translator } = dom.window;
|
||||
await Translator.loadCoreTranslations("MISSINGLANG");
|
||||
it("should load core fallback if language cannot be found", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
dom.window.translations = { en: "http://localhost:3000/translations/translation_test.json" };
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
|
||||
setTimeout(() => {
|
||||
expect(Translator.coreTranslations).toEqual({});
|
||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||
done();
|
||||
}, 500);
|
||||
};
|
||||
});
|
||||
const { Translator } = dom.window;
|
||||
await Translator.loadCoreTranslations("MISSINGLANG");
|
||||
|
||||
const en = translationTestData;
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
expect(Translator.coreTranslations).toEqual({});
|
||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadCoreTranslationsFallback", () => {
|
||||
it("should load core translations fallback", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(
|
||||
`<script>var translations = {en: "http://localhost:3000/translations/translation_test.json"}; var Log = {log: () => {}};</script>\
|
||||
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
||||
{ runScripts: "dangerously", resources: "usable" }
|
||||
);
|
||||
dom.window.onload = async () => {
|
||||
const { Translator } = dom.window;
|
||||
await Translator.loadCoreTranslationsFallback();
|
||||
it("should load core translations fallback", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
dom.window.translations = { en: "http://localhost:3000/translations/translation_test.json" };
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
|
||||
setTimeout(() => {
|
||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||
done();
|
||||
}, 500);
|
||||
};
|
||||
});
|
||||
const { Translator } = dom.window;
|
||||
await Translator.loadCoreTranslationsFallback();
|
||||
|
||||
const en = translationTestData;
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
expect(Translator.coreTranslationsFallback).toEqual(en);
|
||||
});
|
||||
|
||||
it("should load core fallback if language cannot be found", () => {
|
||||
return new Promise((done) => {
|
||||
const dom = new JSDOM(
|
||||
`<script>var translations = {}; var Log = {log: () => {}};</script>\
|
||||
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
||||
{ runScripts: "dangerously", resources: "usable" }
|
||||
);
|
||||
dom.window.onload = async () => {
|
||||
const { Translator } = dom.window;
|
||||
await Translator.loadCoreTranslations();
|
||||
it("should load core fallback if language cannot be found", async () => {
|
||||
const dom = new JSDOM("", { runScripts: "outside-only" });
|
||||
dom.window.eval(translatorJsScriptContent);
|
||||
dom.window.translations = {};
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
|
||||
setTimeout(() => {
|
||||
expect(Translator.coreTranslationsFallback).toEqual({});
|
||||
done();
|
||||
}, 500);
|
||||
};
|
||||
});
|
||||
await new Promise((resolve) => dom.window.onload = resolve);
|
||||
|
||||
const { Translator } = dom.window;
|
||||
await Translator.loadCoreTranslations();
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
expect(Translator.coreTranslationsFallback).toEqual({});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user