2019-06-04 10:43:06 +02:00
|
|
|
const expect = require("chai").expect;
|
2018-02-10 20:33:22 +01:00
|
|
|
const path = require("path");
|
2018-02-13 07:17:46 +01:00
|
|
|
const helmet = require("helmet");
|
2018-02-10 20:33:22 +01:00
|
|
|
const {JSDOM} = require("jsdom");
|
|
|
|
const express = require("express");
|
2018-02-10 12:32:43 +01:00
|
|
|
|
|
|
|
describe("Translator", function() {
|
2018-02-11 08:58:02 +01:00
|
|
|
let server;
|
|
|
|
|
|
|
|
before(function() {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2018-02-10 12:32:43 +01:00
|
|
|
describe("translate", function() {
|
2018-02-11 08:58:02 +01:00
|
|
|
const translations = {
|
|
|
|
"MMM-Module": {
|
|
|
|
"Hello": "Hallo",
|
|
|
|
"Hello {username}": "Hallo {username}"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const coreTranslations = {
|
|
|
|
"Hello": "XXX",
|
|
|
|
"Hello {username}": "XXX",
|
|
|
|
"FOO": "Foo",
|
|
|
|
"BAR {something}": "Bar {something}"
|
|
|
|
};
|
|
|
|
|
|
|
|
const translationsFallback = {
|
|
|
|
"MMM-Module": {
|
|
|
|
"Hello": "XXX",
|
|
|
|
"Hello {username}": "XXX",
|
|
|
|
"FOO": "XXX",
|
|
|
|
"BAR {something}": "XXX",
|
|
|
|
"A key": "A translation"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const coreTranslationsFallback = {
|
|
|
|
"FOO": "XXX",
|
|
|
|
"BAR {something}": "XXX",
|
|
|
|
"Hello": "XXX",
|
|
|
|
"Hello {username}": "XXX",
|
|
|
|
"A key": "XXX",
|
|
|
|
"Fallback": "core fallback"
|
|
|
|
};
|
|
|
|
|
|
|
|
function setTranslations(Translator) {
|
|
|
|
Translator.translations = translations;
|
|
|
|
Translator.coreTranslations = coreTranslations;
|
|
|
|
Translator.translationsFallback = translationsFallback;
|
|
|
|
Translator.coreTranslationsFallback = coreTranslationsFallback;
|
|
|
|
}
|
|
|
|
|
2018-02-10 12:32:43 +01:00
|
|
|
it("should return custom module translation", function(done) {
|
2018-02-10 20:33:22 +01:00
|
|
|
const dom = new JSDOM(`<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
2018-02-10 12:32:43 +01:00
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
2018-02-10 20:33:22 +01:00
|
|
|
const {Translator} = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2018-02-10 20:33:22 +01:00
|
|
|
let translation = Translator.translate({name: "MMM-Module"}, "Hello");
|
2018-02-10 12:32:43 +01:00
|
|
|
expect(translation).to.be.equal("Hallo");
|
|
|
|
translation = Translator.translate({name: "MMM-Module"}, "Hello {username}", {username: "fewieden"});
|
|
|
|
expect(translation).to.be.equal("Hallo fewieden");
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return core translation", function(done) {
|
2018-02-10 20:33:22 +01:00
|
|
|
const dom = new JSDOM(`<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
2018-02-10 12:32:43 +01:00
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
2018-02-10 20:33:22 +01:00
|
|
|
const {Translator} = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2018-02-10 20:33:22 +01:00
|
|
|
let translation = Translator.translate({name: "MMM-Module"}, "FOO");
|
2018-02-10 12:32:43 +01:00
|
|
|
expect(translation).to.be.equal("Foo");
|
|
|
|
translation = Translator.translate({name: "MMM-Module"}, "BAR {something}", {something: "Lorem Ipsum"});
|
|
|
|
expect(translation).to.be.equal("Bar Lorem Ipsum");
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return custom module translation fallback", function(done) {
|
2018-02-10 20:33:22 +01:00
|
|
|
const dom = new JSDOM(`<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
2018-02-10 12:32:43 +01:00
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
2018-02-10 20:33:22 +01:00
|
|
|
const {Translator} = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2018-02-10 20:33:22 +01:00
|
|
|
const translation = Translator.translate({name: "MMM-Module"}, "A key");
|
2018-02-10 12:32:43 +01:00
|
|
|
expect(translation).to.be.equal("A translation");
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return core translation fallback", function(done) {
|
2018-02-10 20:33:22 +01:00
|
|
|
const dom = new JSDOM(`<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
2018-02-10 12:32:43 +01:00
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
2018-02-10 20:33:22 +01:00
|
|
|
const {Translator} = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2018-02-10 20:33:22 +01:00
|
|
|
const translation = Translator.translate({name: "MMM-Module"}, "Fallback");
|
2018-02-10 12:32:43 +01:00
|
|
|
expect(translation).to.be.equal("core fallback");
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return translation with placeholder for missing variables", function(done) {
|
2018-02-10 20:33:22 +01:00
|
|
|
const dom = new JSDOM(`<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
2018-02-10 12:32:43 +01:00
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
2018-02-10 20:33:22 +01:00
|
|
|
const {Translator} = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2018-02-10 20:33:22 +01:00
|
|
|
const translation = Translator.translate({name: "MMM-Module"}, "Hello {username}");
|
2018-02-10 12:32:43 +01:00
|
|
|
expect(translation).to.be.equal("Hallo {username}");
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return key if no translation was found", function(done) {
|
2018-02-10 20:33:22 +01:00
|
|
|
const dom = new JSDOM(`<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
2018-02-10 12:32:43 +01:00
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
2018-02-10 20:33:22 +01:00
|
|
|
const {Translator} = dom.window;
|
2018-02-10 12:32:43 +01:00
|
|
|
setTranslations(Translator);
|
2018-02-10 20:33:22 +01:00
|
|
|
const translation = Translator.translate({name: "MMM-Module"}, "MISSING");
|
2018-02-10 12:32:43 +01:00
|
|
|
expect(translation).to.be.equal("MISSING");
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-10 20:33:22 +01:00
|
|
|
describe("load", function() {
|
|
|
|
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="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
|
|
|
const {Translator} = dom.window;
|
|
|
|
const file = "TranslationTest.json";
|
|
|
|
|
|
|
|
Translator.load(mmm, file, false, function() {
|
|
|
|
const json = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", file));
|
|
|
|
expect(Translator.translations[mmm.name]).to.be.deep.equal(json);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should load translation fallbacks", function(done) {
|
|
|
|
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
|
|
|
const {Translator} = dom.window;
|
|
|
|
const file = "TranslationTest.json";
|
|
|
|
|
|
|
|
Translator.load(mmm, file, true, function() {
|
|
|
|
const json = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", file));
|
|
|
|
expect(Translator.translationsFallback[mmm.name]).to.be.deep.equal(json);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2018-02-13 07:17:46 +01:00
|
|
|
it("should strip comments", function(done) {
|
|
|
|
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
|
|
|
const {Translator} = dom.window;
|
|
|
|
const file = "StripComments.json";
|
|
|
|
|
|
|
|
Translator.load(mmm, file, false, function() {
|
|
|
|
expect(Translator.translations[mmm.name]).to.be.deep.equal({
|
|
|
|
"FOO\"BAR": "Today",
|
|
|
|
"N": "N",
|
|
|
|
"E": "E",
|
|
|
|
"S": "S",
|
|
|
|
"W": "W"
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2018-02-10 20:33:22 +01:00
|
|
|
it("should not load translations, if module fallback exists", function(done) {
|
|
|
|
const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
|
|
|
const {Translator, XMLHttpRequest} = dom.window;
|
|
|
|
const file = "TranslationTest.json";
|
|
|
|
|
|
|
|
XMLHttpRequest.prototype.send = function() {
|
|
|
|
throw "Shouldn't load files";
|
|
|
|
};
|
|
|
|
|
|
|
|
Translator.translationsFallback[mmm.name] = {
|
|
|
|
Hello: "Hallo"
|
|
|
|
};
|
|
|
|
|
|
|
|
Translator.load(mmm, file, false, function() {
|
|
|
|
expect(Translator.translations[mmm.name]).to.be.undefined;
|
|
|
|
expect(Translator.translationsFallback[mmm.name]).to.be.deep.equal({
|
|
|
|
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>\
|
2018-02-13 07:17:46 +01:00
|
|
|
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
2018-02-11 08:58:02 +01:00
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
|
|
|
const {Translator} = dom.window;
|
|
|
|
Translator.loadCoreTranslations("en");
|
|
|
|
|
|
|
|
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(Translator.coreTranslations).to.be.deep.equal(en);
|
|
|
|
expect(Translator.coreTranslationsFallback).to.be.deep.equal(en);
|
|
|
|
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>\
|
2018-02-13 07:17:46 +01:00
|
|
|
<script src="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
2018-02-11 08:58:02 +01:00
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
|
|
|
const {Translator} = dom.window;
|
|
|
|
Translator.loadCoreTranslations("MISSINGLANG");
|
|
|
|
|
|
|
|
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(Translator.coreTranslations).to.be.deep.equal({});
|
|
|
|
expect(Translator.coreTranslationsFallback).to.be.deep.equal(en);
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
2018-02-11 09:08:09 +01:00
|
|
|
|
2018-02-13 07:17:46 +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="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
|
|
|
const {Translator} = dom.window;
|
|
|
|
Translator.loadCoreTranslationsFallback();
|
|
|
|
|
|
|
|
const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(Translator.coreTranslationsFallback).to.be.deep.equal(en);
|
|
|
|
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="${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously",
|
|
|
|
resources: "usable" });
|
|
|
|
dom.window.onload = function() {
|
|
|
|
const {Translator} = dom.window;
|
|
|
|
Translator.loadCoreTranslations();
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
expect(Translator.coreTranslationsFallback).to.be.deep.equal({});
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
2018-02-10 20:33:22 +01:00
|
|
|
});
|