|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- const fs = require("fs");
- const path = require("path");
- const translations = require("../../translations/translations.js");
- const helmet = require("helmet");
- const { JSDOM } = require("jsdom");
- const express = require("express");
- const sinon = require("sinon");
-
- describe("Translations", function () {
- let server;
-
- beforeAll(function () {
- const app = express();
- app.use(helmet());
- app.use(function (req, res, next) {
- res.header("Access-Control-Allow-Origin", "*");
- next();
- });
- app.use("/translations", express.static(path.join(__dirname, "..", "..", "translations")));
-
- server = app.listen(3000);
- });
-
- afterAll(function () {
- server.close();
- });
-
- it("should have a translation file in the specified path", function () {
- for (let language in translations) {
- const file = fs.statSync(translations[language]);
- expect(file.isFile()).toBe(true);
- }
- });
-
- describe("loadTranslations", () => {
- let dom;
-
- beforeEach(() => {
- dom = new JSDOM(
- `<script>var Translator = {}; var Log = {log: function(){}}; 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 function () {
- 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);
-
- 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 function () {
- 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);
-
- 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 function () {
- 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);
-
- 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 function () {
- const { Translator, Module } = dom.window;
- Translator.load = sinon.stub();
-
- Module.register("name", {});
- const MMM = Module.create("name");
-
- const loaded = sinon.stub();
- MMM.loadTranslations(loaded);
-
- expect(loaded.callCount).toBe(1);
- expect(Translator.load.callCount).toBe(0);
-
- done();
- };
- });
- });
-
- const mmm = {
- name: "TranslationTest",
- file(file) {
- return `http://localhost:3000/${file}`;
- }
- };
-
- describe("Parsing language files through the Translator class", function () {
- for (let language in translations) {
- it(`should parse ${language}`, function (done) {
- const dom = new JSDOM(
- `<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
- <script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
- { runScripts: "dangerously", resources: "usable" }
- );
- dom.window.onload = function () {
- const { Translator } = dom.window;
-
- Translator.load(mmm, translations[language], false, function () {
- expect(typeof Translator.translations[mmm.name]).toBe("object");
- expect(Object.keys(Translator.translations[mmm.name]).length).toBeGreaterThanOrEqual(1);
- done();
- });
- };
- });
- }
- });
-
- describe("Same keys", function () {
- let base;
- let missing = [];
-
- beforeAll(function (done) {
- const dom = new JSDOM(
- `<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
- <script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
- { runScripts: "dangerously", resources: "usable" }
- );
- dom.window.onload = function () {
- const { Translator } = dom.window;
-
- Translator.load(mmm, translations.en, false, function () {
- base = Object.keys(Translator.translations[mmm.name]).sort();
- done();
- });
- };
- });
-
- afterAll(function () {
- console.log(missing);
- });
-
- for (let language in translations) {
- if (language === "en") {
- continue;
- }
-
- describe(`Translation keys of ${language}`, function () {
- let keys;
-
- beforeAll(function (done) {
- const dom = new JSDOM(
- `<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
- <script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
- { runScripts: "dangerously", resources: "usable" }
- );
- dom.window.onload = function () {
- const { Translator } = dom.window;
-
- Translator.load(mmm, translations[language], false, function () {
- keys = Object.keys(Translator.translations[mmm.name]).sort();
- done();
- });
- };
- });
-
- it(`${language} keys should be in base`, function () {
- keys.forEach(function (key) {
- expect(base.indexOf(key)).toBeGreaterThanOrEqual(0);
- });
- });
-
- it(`${language} should contain all base keys`, function () {
- // TODO: when all translations are fixed, use
- // expect(keys).toEqual(base);
- // instead of the try-catch-block
-
- try {
- expect(keys).toEqual(base);
- } catch (e) {
- if (e.message.match(/expect.*toEqual/)) {
- const diff = base.filter((key) => !keys.includes(key));
- missing.push(`Missing Translations for language ${language}: ${diff}`);
- } else {
- throw e;
- }
- }
- });
- });
- }
- });
- });
|