MagicMirror/tests/unit/classes/translator_spec.js

286 lines
9.9 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", () => {
2018-02-11 08:58:02 +01:00
let server;
beforeAll(() => {
2018-02-11 08:58:02 +01:00
const app = express();
2018-02-13 07:17:46 +01:00
app.use(helmet());
app.use((req, res, next) => {
2018-02-13 07:17:46 +01:00
res.header("Access-Control-Allow-Origin", "*");
next();
2018-02-11 08:58:02 +01:00
});
app.use("/translations", express.static(path.join(__dirname, "..", "..", "..", "tests", "mocks")));
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
});
afterAll(async () => {
2021-06-06 23:13:09 +02:00
for (const socket of sockets) {
socket.destroy();
sockets.delete(socket);
}
await server.close();
2018-02-11 08:58:02 +01:00
});
describe("translate", () => {
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
/**
2021-07-24 09:49:46 +02:00
* @param {object} Translator the global Translator object
2021-06-30 15:13:15 +02:00
*/
const setTranslations = (Translator) => {
2018-02-11 08:58:02 +01:00
Translator.translations = translations;
Translator.coreTranslations = coreTranslations;
Translator.translationsFallback = translationsFallback;
Translator.coreTranslationsFallback = coreTranslationsFallback;
};
2018-02-11 08:58:02 +01:00
it("should return custom module translation", (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;
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", (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;
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", (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;
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", (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;
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", (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;
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", (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;
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", () => {
2018-02-10 20:33:22 +01:00
const mmm = {
name: "TranslationTest",
file(file) {
return `http://localhost:3000/translations/${file}`;
}
};
it("should load translations", (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";
2018-02-10 20:33:22 +01:00
await Translator.load(mmm, file, false);
const json = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", file));
expect(Translator.translations[mmm.name]).toEqual(json);
done();
2018-02-10 20:33:22 +01:00
};
});
it("should load translation fallbacks", (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";
2018-02-10 20:33:22 +01:00
await Translator.load(mmm, file, true);
const json = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", file));
expect(Translator.translationsFallback[mmm.name]).toEqual(json);
done();
2018-02-10 20:33:22 +01:00
};
});
it("should not load translations, if module fallback exists", (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";
2018-02-10 20:33:22 +01:00
XMLHttpRequest.prototype.send = () => {
throw new Error("Shouldn't load files");
2018-02-10 20:33:22 +01:00
};
Translator.translationsFallback[mmm.name] = {
Hello: "Hallo"
};
await Translator.load(mmm, file, false);
expect(Translator.translations[mmm.name]).toBe(undefined);
expect(Translator.translationsFallback[mmm.name]).toEqual({
Hello: "Hallo"
2018-02-10 20:33:22 +01:00
});
done();
2018-02-10 20:33:22 +01:00
};
});
});
2018-02-11 08:58:02 +01:00
describe("loadCoreTranslations", () => {
it("should load core translations and fallback", (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");
2018-02-11 08:58:02 +01:00
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
setTimeout(() => {
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", (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");
2018-02-11 08:58:02 +01:00
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
setTimeout(() => {
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", () => {
it("should load core translations fallback", (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();
2018-02-13 07:17:46 +01:00
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
setTimeout(() => {
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", (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();
2018-02-13 07:17:46 +01:00
setTimeout(() => {
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
});