2018-02-10 20:33:22 +01:00
|
|
|
const path = require("path");
|
2018-02-13 07:17:46 +01:00
|
|
|
const helmet = require("helmet");
|
2020-05-11 22:22:32 +02:00
|
|
|
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
|
|
|
|
2020-05-11 22:22:32 +02: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();
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
describe("translate", function () {
|
2018-02-11 08:58:02 +01:00
|
|
|
const translations = {
|
|
|
|
"MMM-Module": {
|
2020-05-11 22:22:32 +02:00
|
|
|
Hello: "Hallo",
|
2018-02-11 08:58:02 +01:00
|
|
|
"Hello {username}": "Hallo {username}"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const coreTranslations = {
|
2020-05-11 22:22:32 +02:00
|
|
|
Hello: "XXX",
|
2018-02-11 08:58:02 +01:00
|
|
|
"Hello {username}": "XXX",
|
2020-05-11 22:22:32 +02:00
|
|
|
FOO: "Foo",
|
2018-02-11 08:58:02 +01:00
|
|
|
"BAR {something}": "Bar {something}"
|
|
|
|
};
|
|
|
|
|
|
|
|
const translationsFallback = {
|
|
|
|
"MMM-Module": {
|
2020-05-11 22:22:32 +02:00
|
|
|
Hello: "XXX",
|
2018-02-11 08:58:02 +01:00
|
|
|
"Hello {username}": "XXX",
|
2020-05-11 22:22:32 +02:00
|
|
|
FOO: "XXX",
|
2018-02-11 08:58:02 +01:00
|
|
|
"BAR {something}": "XXX",
|
|
|
|
"A key": "A translation"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const coreTranslationsFallback = {
|
2020-05-11 22:22:32 +02:00
|
|
|
FOO: "XXX",
|
2018-02-11 08:58:02 +01:00
|
|
|
"BAR {something}": "XXX",
|
2020-05-11 22:22:32 +02:00
|
|
|
Hello: "XXX",
|
2018-02-11 08:58:02 +01:00
|
|
|
"Hello {username}": "XXX",
|
|
|
|
"A key": "XXX",
|
2020-05-11 22:22:32 +02:00
|
|
|
Fallback: "core fallback"
|
2018-02-11 08:58:02 +01:00
|
|
|
};
|
|
|
|
|
2021-06-30 15:13:15 +02:00
|
|
|
/**
|
2021-07-24 09:49:46 +02:00
|
|
|
* @param {object} Translator the global Translator object
|
2021-06-30 15:13:15 +02:00
|
|
|
*/
|
2018-02-11 08:58:02 +01:00
|
|
|
function setTranslations(Translator) {
|
|
|
|
Translator.translations = translations;
|
|
|
|
Translator.coreTranslations = coreTranslations;
|
|
|
|
Translator.translationsFallback = translationsFallback;
|
|
|
|
Translator.coreTranslationsFallback = coreTranslationsFallback;
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should return custom module translation", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2020-05-11 22:22:32 +02:00
|
|
|
let translation = Translator.translate({ name: "MMM-Module" }, "Hello");
|
2021-06-08 00:47:15 +02:00
|
|
|
expect(translation).toBe("Hallo");
|
2020-05-11 22:22:32 +02:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should return core translation", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2020-05-11 22:22:32 +02:00
|
|
|
let translation = Translator.translate({ name: "MMM-Module" }, "FOO");
|
2021-06-08 00:47:15 +02:00
|
|
|
expect(translation).toBe("Foo");
|
2020-05-11 22:22:32 +02:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should return custom module translation fallback", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2020-05-11 22:22:32 +02:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should return core translation fallback", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2020-05-11 22:22:32 +02:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should return translation with placeholder for missing variables", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2020-05-11 22:22:32 +02:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should return key if no translation was found", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2020-05-11 22:22:32 +02:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
describe("load", function () {
|
2018-02-10 20:33:22 +01:00
|
|
|
const mmm = {
|
|
|
|
name: "TranslationTest",
|
|
|
|
file(file) {
|
|
|
|
return `http://localhost:3000/translations/${file}`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should load translations", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-10 20:33:22 +01:00
|
|
|
const file = "TranslationTest.json";
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should load translation fallbacks", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-10 20:33:22 +01:00
|
|
|
const file = "TranslationTest.json";
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should not load translations, if module fallback exists", function (done) {
|
2020-10-23 02:05:13 +01:00
|
|
|
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
|
2020-05-11 22:22:32 +02:00
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator, XMLHttpRequest } = dom.window;
|
2018-02-10 20:33:22 +01:00
|
|
|
const file = "TranslationTest.json";
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
XMLHttpRequest.prototype.send = function () {
|
2018-02-10 20:33:22 +01:00
|
|
|
throw "Shouldn't load files";
|
|
|
|
};
|
|
|
|
|
|
|
|
Translator.translationsFallback[mmm.name] = {
|
|
|
|
Hello: "Hallo"
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
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
|
|
|
|
2020-05-11 22:22:32 +02: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>\
|
2020-10-23 02:05:13 +01:00
|
|
|
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
2020-05-11 22:22:32 +02:00
|
|
|
{ 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"));
|
2020-05-11 22:22:32 +02:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
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>\
|
2020-10-23 02:05:13 +01:00
|
|
|
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
2020-05-11 22:22:32 +02:00
|
|
|
{ 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"));
|
2020-05-11 22:22:32 +02:00
|
|
|
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
|
|
|
|
2020-05-11 22:22:32 +02: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>\
|
2020-10-23 02:05:13 +01:00
|
|
|
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
2020-05-11 22:22:32 +02:00
|
|
|
{ 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"));
|
2020-05-11 22:22:32 +02:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
it("should load core fallback if language cannot be found", function (done) {
|
|
|
|
const dom = new JSDOM(
|
|
|
|
`<script>var translations = {}; var Log = {log: function(){}};</script>\
|
2020-10-23 02:05:13 +01:00
|
|
|
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
2020-05-11 22:22:32 +02:00
|
|
|
{ runScripts: "dangerously", resources: "usable" }
|
|
|
|
);
|
|
|
|
dom.window.onload = function () {
|
|
|
|
const { Translator } = dom.window;
|
2018-02-13 07:17:46 +01:00
|
|
|
Translator.loadCoreTranslations();
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
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
|
|
|
});
|