MagicMirror/tests/unit/classes/translator_spec.js

289 lines
10 KiB
JavaScript
Raw Normal View History

2018-02-10 20:33:22 +01:00
const path = require("path");
2018-02-13 07:17:46 +01:00
const helmet = require("helmet");
const { JSDOM } = require("jsdom");
2018-02-10 20:33:22 +01:00
const express = require("express");
2021-06-06 23:13:09 +02:00
const sockets = new Set();
2018-02-10 12:32:43 +01:00
describe("Translator", function () {
2018-02-11 08:58:02 +01:00
let server;
2021-06-06 23:13:09 +02:00
beforeAll(function () {
2018-02-11 08:58:02 +01:00
const app = express();
2018-02-13 07:17:46 +01:00
app.use(helmet());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
2018-02-11 08:58:02 +01:00
});
2018-02-13 07:17:46 +01:00
app.use("/translations", express.static(path.join(__dirname, "..", "..", "..", "tests", "configs", "data")));
2018-02-11 08:58:02 +01:00
server = app.listen(3000);
2021-06-06 23:13:09 +02:00
2021-06-11 22:24:21 +02:00
server.on("connection", (socket) => {
2021-06-06 23:13:09 +02:00
sockets.add(socket);
});
2018-02-11 08:58:02 +01:00
});
2021-06-06 23:13:09 +02:00
afterAll(function () {
for (const socket of sockets) {
socket.destroy();
2021-06-11 22:24:21 +02:00
2021-06-06 23:13:09 +02:00
sockets.delete(socket);
}
2018-02-11 08:58:02 +01:00
server.close();
});
describe("translate", function () {
2018-02-11 08:58:02 +01:00
const translations = {
"MMM-Module": {
Hello: "Hallo",
2018-02-11 08:58:02 +01:00
"Hello {username}": "Hallo {username}"
}
};
const coreTranslations = {
Hello: "XXX",
2018-02-11 08:58:02 +01:00
"Hello {username}": "XXX",
FOO: "Foo",
2018-02-11 08:58:02 +01:00
"BAR {something}": "Bar {something}"
};
const translationsFallback = {
"MMM-Module": {
Hello: "XXX",
2018-02-11 08:58:02 +01:00
"Hello {username}": "XXX",
FOO: "XXX",
2018-02-11 08:58:02 +01:00
"BAR {something}": "XXX",
"A key": "A translation"
}
};
const coreTranslationsFallback = {
FOO: "XXX",
2018-02-11 08:58:02 +01:00
"BAR {something}": "XXX",
Hello: "XXX",
2018-02-11 08:58:02 +01:00
"Hello {username}": "XXX",
"A key": "XXX",
Fallback: "core fallback"
2018-02-11 08:58:02 +01:00
};
2021-06-30 15:13:15 +02:00
/**
* @param Translator
*/
2018-02-11 08:58:02 +01:00
function setTranslations(Translator) {
Translator.translations = translations;
Translator.coreTranslations = coreTranslations;
Translator.translationsFallback = translationsFallback;
Translator.coreTranslationsFallback = coreTranslationsFallback;
}
it("should return custom module translation", function (done) {
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-10 12:32:43 +01:00
setTranslations(Translator);
let translation = Translator.translate({ name: "MMM-Module" }, "Hello");
2021-06-08 00:47:15 +02:00
expect(translation).toBe("Hallo");
translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}", { username: "fewieden" });
2021-06-08 00:47:15 +02:00
expect(translation).toBe("Hallo fewieden");
2018-02-10 12:32:43 +01:00
done();
};
});
it("should return core translation", function (done) {
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-10 12:32:43 +01:00
setTranslations(Translator);
let translation = Translator.translate({ name: "MMM-Module" }, "FOO");
2021-06-08 00:47:15 +02:00
expect(translation).toBe("Foo");
translation = Translator.translate({ name: "MMM-Module" }, "BAR {something}", { something: "Lorem Ipsum" });
2021-06-08 00:47:15 +02:00
expect(translation).toBe("Bar Lorem Ipsum");
2018-02-10 12:32:43 +01:00
done();
};
});
it("should return custom module translation fallback", function (done) {
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-10 12:32:43 +01:00
setTranslations(Translator);
const translation = Translator.translate({ name: "MMM-Module" }, "A key");
2021-06-08 00:47:15 +02:00
expect(translation).toBe("A translation");
2018-02-10 12:32:43 +01:00
done();
};
});
it("should return core translation fallback", function (done) {
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-10 12:32:43 +01:00
setTranslations(Translator);
const translation = Translator.translate({ name: "MMM-Module" }, "Fallback");
2021-06-08 00:47:15 +02:00
expect(translation).toBe("core fallback");
2018-02-10 12:32:43 +01:00
done();
};
});
it("should return translation with placeholder for missing variables", function (done) {
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-10 12:32:43 +01:00
setTranslations(Translator);
const translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}");
2021-06-08 00:47:15 +02:00
expect(translation).toBe("Hallo {username}");
2018-02-10 12:32:43 +01:00
done();
};
});
it("should return key if no translation was found", function (done) {
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-10 12:32:43 +01:00
setTranslations(Translator);
const translation = Translator.translate({ name: "MMM-Module" }, "MISSING");
2021-06-08 00:47:15 +02:00
expect(translation).toBe("MISSING");
2018-02-10 12:32:43 +01:00
done();
};
});
});
describe("load", function () {
2018-02-10 20:33:22 +01:00
const mmm = {
name: "TranslationTest",
file(file) {
return `http://localhost:3000/translations/${file}`;
}
};
it("should load translations", function (done) {
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-10 20:33:22 +01:00
const file = "TranslationTest.json";
Translator.load(mmm, file, false, function () {
2018-02-10 20:33:22 +01:00
const json = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", file));
2021-06-08 00:47:15 +02:00
expect(Translator.translations[mmm.name]).toEqual(json);
2018-02-10 20:33:22 +01:00
done();
});
};
});
it("should load translation fallbacks", function (done) {
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-10 20:33:22 +01:00
const file = "TranslationTest.json";
Translator.load(mmm, file, true, function () {
2018-02-10 20:33:22 +01:00
const json = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", file));
2021-06-08 00:47:15 +02:00
expect(Translator.translationsFallback[mmm.name]).toEqual(json);
2018-02-10 20:33:22 +01:00
done();
});
};
});
it("should not load translations, if module fallback exists", function (done) {
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
dom.window.onload = function () {
const { Translator, XMLHttpRequest } = dom.window;
2018-02-10 20:33:22 +01:00
const file = "TranslationTest.json";
XMLHttpRequest.prototype.send = function () {
2018-02-10 20:33:22 +01:00
throw "Shouldn't load files";
};
Translator.translationsFallback[mmm.name] = {
Hello: "Hallo"
};
Translator.load(mmm, file, false, function () {
2021-06-08 00:47:15 +02:00
expect(Translator.translations[mmm.name]).toBe(undefined);
expect(Translator.translationsFallback[mmm.name]).toEqual({
2018-02-10 20:33:22 +01:00
Hello: "Hallo"
});
done();
});
};
});
});
2018-02-11 08:58:02 +01:00
describe("loadCoreTranslations", function () {
it("should load core translations and fallback", function (done) {
const dom = new JSDOM(
`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-11 08:58:02 +01:00
Translator.loadCoreTranslations("en");
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
setTimeout(function () {
2021-06-08 00:47:15 +02:00
expect(Translator.coreTranslations).toEqual(en);
expect(Translator.coreTranslationsFallback).toEqual(en);
2018-02-11 08:58:02 +01:00
done();
}, 500);
};
});
it("should load core fallback if language cannot be found", function (done) {
const dom = new JSDOM(
`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-11 08:58:02 +01:00
Translator.loadCoreTranslations("MISSINGLANG");
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
setTimeout(function () {
2021-06-08 00:47:15 +02:00
expect(Translator.coreTranslations).toEqual({});
expect(Translator.coreTranslationsFallback).toEqual(en);
2018-02-11 08:58:02 +01:00
done();
}, 500);
};
});
});
2018-02-11 09:08:09 +01:00
describe("loadCoreTranslationsFallback", function () {
it("should load core translations fallback", function (done) {
const dom = new JSDOM(
`<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-13 07:17:46 +01:00
Translator.loadCoreTranslationsFallback();
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
setTimeout(function () {
2021-06-08 00:47:15 +02:00
expect(Translator.coreTranslationsFallback).toEqual(en);
2018-02-13 07:17:46 +01:00
done();
}, 500);
};
});
it("should load core fallback if language cannot be found", function (done) {
const dom = new JSDOM(
`<script>var translations = {}; var Log = {log: function(){}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = function () {
const { Translator } = dom.window;
2018-02-13 07:17:46 +01:00
Translator.loadCoreTranslations();
setTimeout(function () {
2021-06-08 00:47:15 +02:00
expect(Translator.coreTranslationsFallback).toEqual({});
2018-02-13 07:17:46 +01:00
done();
}, 500);
};
});
});
2018-02-10 20:33:22 +01:00
});