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");
|
2023-03-22 23:53:10 +01:00
|
|
|
|
2021-06-06 23:13:09 +02:00
|
|
|
const sockets = new Set();
|
2018-02-10 12:32:43 +01:00
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
describe("Translator", () => {
|
2018-02-11 08:58:02 +01:00
|
|
|
let server;
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
beforeAll(() => {
|
2018-02-11 08:58:02 +01:00
|
|
|
const app = express();
|
2018-02-13 07:17:46 +01:00
|
|
|
app.use(helmet());
|
2022-10-04 10:15:24 +02:00
|
|
|
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
|
|
|
});
|
2022-10-07 19:16:37 +02: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
|
|
|
});
|
|
|
|
|
2023-02-21 22:58:18 +01:00
|
|
|
afterAll(async () => {
|
2021-06-06 23:13:09 +02:00
|
|
|
for (const socket of sockets) {
|
|
|
|
socket.destroy();
|
|
|
|
sockets.delete(socket);
|
|
|
|
}
|
|
|
|
|
2023-02-21 22:58:18 +01:00
|
|
|
await server.close();
|
2018-02-11 08:58:02 +01:00
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
describe("translate", () => {
|
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
|
|
|
*/
|
2022-10-04 10:15:24 +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;
|
2022-10-04 10:15:24 +02:00
|
|
|
};
|
2018-02-11 08:58:02 +01:00
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
2018-02-10 12:32:43 +01:00
|
|
|
});
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
2018-02-10 12:32:43 +01:00
|
|
|
});
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
2018-02-10 12:32:43 +01:00
|
|
|
});
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
2018-02-10 12:32:43 +01:00
|
|
|
});
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
2018-02-10 12:32:43 +01:00
|
|
|
});
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
});
|
2018-02-10 12:32:43 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
describe("load", () => {
|
2018-02-10 20:33:22 +01:00
|
|
|
const mmm = {
|
|
|
|
name: "TranslationTest",
|
|
|
|
file(file) {
|
|
|
|
return `http://localhost:3000/translations/${file}`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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";
|
2018-02-10 20:33:22 +01:00
|
|
|
|
2023-11-22 23:37:17 +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
|
|
|
});
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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";
|
2018-02-10 20:33:22 +01:00
|
|
|
|
2023-11-22 23:37:17 +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
|
|
|
};
|
2023-11-22 23:37:17 +01:00
|
|
|
});
|
|
|
|
});
|
2018-02-10 20:33:22 +01:00
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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";
|
|
|
|
|
|
|
|
XMLHttpRequest.prototype.send = () => {
|
|
|
|
throw new Error("Shouldn't load files");
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
2018-02-10 20:33:22 +01:00
|
|
|
};
|
2023-11-22 23:37:17 +01:00
|
|
|
});
|
2018-02-10 20:33:22 +01:00
|
|
|
});
|
|
|
|
});
|
2018-02-11 08:58:02 +01:00
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
describe("loadCoreTranslations", () => {
|
2023-11-22 23:37:17 +01:00
|
|
|
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>\
|
2020-10-23 02:05:13 +01:00
|
|
|
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
2023-11-22 23:37:17 +01:00
|
|
|
{ runScripts: "dangerously", resources: "usable" }
|
|
|
|
);
|
|
|
|
dom.window.onload = async () => {
|
|
|
|
const { Translator } = dom.window;
|
|
|
|
await Translator.loadCoreTranslations("en");
|
|
|
|
|
|
|
|
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Translator.coreTranslations).toEqual(en);
|
|
|
|
expect(Translator.coreTranslationsFallback).toEqual(en);
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
});
|
2018-02-11 08:58:02 +01:00
|
|
|
});
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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>\
|
2020-10-23 02:05:13 +01:00
|
|
|
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
2023-11-22 23:37:17 +01:00
|
|
|
{ runScripts: "dangerously", resources: "usable" }
|
|
|
|
);
|
|
|
|
dom.window.onload = async () => {
|
|
|
|
const { Translator } = dom.window;
|
|
|
|
await Translator.loadCoreTranslations("MISSINGLANG");
|
|
|
|
|
|
|
|
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Translator.coreTranslations).toEqual({});
|
|
|
|
expect(Translator.coreTranslationsFallback).toEqual(en);
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
});
|
2018-02-11 08:58:02 +01:00
|
|
|
});
|
|
|
|
});
|
2018-02-11 09:08:09 +01:00
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
describe("loadCoreTranslationsFallback", () => {
|
2023-11-22 23:37:17 +01:00
|
|
|
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>\
|
2020-10-23 02:05:13 +01:00
|
|
|
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
2023-11-22 23:37:17 +01:00
|
|
|
{ runScripts: "dangerously", resources: "usable" }
|
|
|
|
);
|
|
|
|
dom.window.onload = async () => {
|
|
|
|
const { Translator } = dom.window;
|
|
|
|
await Translator.loadCoreTranslationsFallback();
|
|
|
|
|
|
|
|
const en = require(path.join(__dirname, "..", "..", "..", "tests", "mocks", "translation_test.json"));
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Translator.coreTranslationsFallback).toEqual(en);
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
});
|
2018-02-13 07:17:46 +01:00
|
|
|
});
|
|
|
|
|
2023-11-22 23:37:17 +01:00
|
|
|
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>\
|
2020-10-23 02:05:13 +01:00
|
|
|
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
|
2023-11-22 23:37:17 +01:00
|
|
|
{ runScripts: "dangerously", resources: "usable" }
|
|
|
|
);
|
|
|
|
dom.window.onload = async () => {
|
|
|
|
const { Translator } = dom.window;
|
|
|
|
await Translator.loadCoreTranslations();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Translator.coreTranslationsFallback).toEqual({});
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
});
|
2018-02-13 07:17:46 +01:00
|
|
|
});
|
|
|
|
});
|
2018-02-10 20:33:22 +01:00
|
|
|
});
|