MagicMirror/tests/e2e/translations_spec.js

230 lines
6.7 KiB
JavaScript
Raw Normal View History

2018-02-15 23:22:52 +01:00
const fs = require("fs");
const path = require("path");
const translations = require("../../translations/translations.js");
const helmet = require("helmet");
const { JSDOM } = require("jsdom");
2018-02-15 23:22:52 +01:00
const express = require("express");
const sinon = require("sinon");
2018-02-15 23:22:52 +01:00
describe("Translations", () => {
2018-02-16 00:08:11 +01:00
let server;
2018-02-15 23:22:52 +01:00
beforeAll(() => {
2018-02-16 00:08:11 +01:00
const app = express();
app.use(helmet());
app.use((req, res, next) => {
2018-02-16 00:08:11 +01:00
res.header("Access-Control-Allow-Origin", "*");
next();
});
app.use("/translations", express.static(path.join(__dirname, "..", "..", "translations")));
2018-02-15 23:22:52 +01:00
2018-02-16 00:08:11 +01:00
server = app.listen(3000);
});
2018-02-15 23:22:52 +01:00
afterAll(() => {
2018-02-16 00:08:11 +01:00
server.close();
});
2018-02-15 23:22:52 +01:00
it("should have a translation file in the specified path", () => {
for (let language in translations) {
2018-02-15 23:22:52 +01:00
const file = fs.statSync(translations[language]);
2021-06-09 00:19:43 +02:00
expect(file.isFile()).toBe(true);
2018-02-15 23:22:52 +01:00
}
2018-02-16 00:08:11 +01:00
});
describe("loadTranslations", () => {
let dom;
beforeEach(() => {
dom = new JSDOM(
`<script>var Translator = {}; var Log = {log: () => {}}; var config = {language: 'de'};</script>\
<script src="file://${path.join(__dirname, "..", "..", "js", "class.js")}"></script>\
<script src="file://${path.join(__dirname, "..", "..", "js", "module.js")}"></script>`,
{ runScripts: "dangerously", resources: "usable" }
);
});
it("should load translation file", (done) => {
dom.window.onload = async () => {
const { Translator, Module, config } = dom.window;
config.language = "en";
Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());
Module.register("name", { getTranslations: () => translations });
const MMM = Module.create("name");
const loaded = sinon.stub();
MMM.loadTranslations(loaded);
2021-06-09 00:19:43 +02:00
expect(loaded.callCount).toBe(1);
expect(Translator.load.args.length).toBe(1);
expect(Translator.load.calledWith(MMM, "translations/en.json", false, sinon.match.func)).toBe(true);
done();
};
});
it("should load translation + fallback file", (done) => {
dom.window.onload = async () => {
const { Translator, Module } = dom.window;
Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());
Module.register("name", { getTranslations: () => translations });
const MMM = Module.create("name");
const loaded = sinon.stub();
MMM.loadTranslations(loaded);
2021-06-09 00:19:43 +02:00
expect(loaded.callCount).toBe(1);
expect(Translator.load.args.length).toBe(2);
expect(Translator.load.calledWith(MMM, "translations/de.json", false, sinon.match.func)).toBe(true);
expect(Translator.load.calledWith(MMM, "translations/en.json", true, sinon.match.func)).toBe(true);
done();
};
});
it("should load translation fallback file", (done) => {
dom.window.onload = async () => {
const { Translator, Module, config } = dom.window;
config.language = "--";
Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());
Module.register("name", { getTranslations: () => translations });
const MMM = Module.create("name");
const loaded = sinon.stub();
MMM.loadTranslations(loaded);
2021-06-09 00:19:43 +02:00
expect(loaded.callCount).toBe(1);
expect(Translator.load.args.length).toBe(1);
expect(Translator.load.calledWith(MMM, "translations/en.json", true, sinon.match.func)).toBe(true);
done();
};
});
it("should load no file", (done) => {
dom.window.onload = async () => {
const { Translator, Module } = dom.window;
Translator.load = sinon.stub();
Module.register("name", {});
const MMM = Module.create("name");
const loaded = sinon.stub();
MMM.loadTranslations(loaded);
2021-06-09 00:19:43 +02:00
expect(loaded.callCount).toBe(1);
expect(Translator.load.callCount).toBe(0);
done();
};
});
});
2018-02-16 00:08:11 +01:00
const mmm = {
name: "TranslationTest",
file(file) {
return `http://localhost:3000/${file}`;
}
};
describe("Parsing language files through the Translator class", () => {
for (let language in translations) {
it(`should parse ${language}`, (done) => {
const dom = new JSDOM(
`<script>var translations = ${JSON.stringify(translations)}; var Log = {log: () => {}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = () => {
const { Translator } = dom.window;
Translator.load(mmm, translations[language], false, () => {
2021-06-09 00:19:43 +02:00
expect(typeof Translator.translations[mmm.name]).toBe("object");
expect(Object.keys(Translator.translations[mmm.name]).length).toBeGreaterThanOrEqual(1);
2018-02-16 00:08:11 +01:00
done();
});
};
});
}
});
describe("Same keys", () => {
2018-02-16 00:08:11 +01:00
let base;
let missing = [];
2018-02-16 00:08:11 +01:00
beforeAll((done) => {
const dom = new JSDOM(
`<script>var translations = ${JSON.stringify(translations)}; var Log = {log: () => {}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = () => {
const { Translator } = dom.window;
2018-02-16 00:08:11 +01:00
Translator.load(mmm, translations.de, false, () => {
2018-02-16 00:08:11 +01:00
base = Object.keys(Translator.translations[mmm.name]).sort();
done();
});
};
});
afterAll(() => {
console.log(missing);
});
2022-07-25 18:20:30 -04:00
// Using German as the base rather than English, since
// at least one translated word doesn't exist in English.
2018-02-16 00:08:11 +01:00
for (let language in translations) {
2022-07-25 18:20:30 -04:00
if (language === "de") {
2018-02-16 00:08:11 +01:00
continue;
}
describe(`Translation keys of ${language}`, () => {
2018-02-16 00:08:11 +01:00
let keys;
beforeAll((done) => {
const dom = new JSDOM(
`<script>var translations = ${JSON.stringify(translations)}; var Log = {log: () => {}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = () => {
const { Translator } = dom.window;
2018-02-16 00:08:11 +01:00
Translator.load(mmm, translations[language], false, () => {
2018-02-16 00:08:11 +01:00
keys = Object.keys(Translator.translations[mmm.name]).sort();
done();
});
};
});
it(`${language} keys should be in base`, () => {
keys.forEach((key) => {
2021-06-09 00:19:43 +02:00
expect(base.indexOf(key)).toBeGreaterThanOrEqual(0);
2018-02-16 00:08:11 +01:00
});
});
it(`${language} should contain all base keys`, () => {
2018-02-16 00:08:11 +01:00
// TODO: when all translations are fixed, use
2021-06-09 00:19:43 +02:00
// expect(keys).toEqual(base);
2018-02-16 00:08:11 +01:00
// instead of the try-catch-block
try {
2021-06-09 00:19:43 +02:00
expect(keys).toEqual(base);
} catch (e) {
2021-06-09 00:19:43 +02:00
if (e.message.match(/expect.*toEqual/)) {
2021-06-11 23:31:37 +02:00
const diff = base.filter((key) => !keys.includes(key));
missing.push(`Missing Translations for language ${language}: ${diff}`);
2018-02-16 00:08:11 +01:00
} else {
throw e;
}
}
2019-06-05 10:23:58 +02:00
});
2018-02-16 00:08:11 +01:00
});
}
});
2018-02-15 23:22:52 +01:00
});