const fs = require("fs"); const path = require("path"); const helmet = require("helmet"); const { JSDOM } = require("jsdom"); const express = require("express"); const sinon = require("sinon"); const translations = require("../../translations/translations"); describe("Translations", () => { let server; beforeAll(() => { const app = express(); app.use(helmet()); app.use((req, res, next) => { res.header("Access-Control-Allow-Origin", "*"); next(); }); app.use("/translations", express.static(path.join(__dirname, "..", "..", "translations"))); server = app.listen(3000); }); afterAll(async () => { await server.close(); }); it("should have a translation file in the specified path", () => { for (let language in translations) { const file = fs.statSync(translations[language]); expect(file.isFile()).toBe(true); } }); describe("loadTranslations", () => { let dom; beforeEach(() => { dom = new JSDOM( `\ \ `, { runScripts: "dangerously", resources: "usable" } ); }); it("should load translation file", () => { return new Promise((done) => { dom.window.onload = async () => { const { Translator, Module, config } = dom.window; config.language = "en"; Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null); Module.register("name", { getTranslations: () => translations }); const MMM = Module.create("name"); await MMM.loadTranslations(); expect(Translator.load.args).toHaveLength(1); expect(Translator.load.calledWith(MMM, "translations/en.json", false)).toBe(true); done(); }; }); }); it("should load translation + fallback file", () => { return new Promise((done) => { dom.window.onload = async () => { const { Translator, Module } = dom.window; Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null); Module.register("name", { getTranslations: () => translations }); const MMM = Module.create("name"); await MMM.loadTranslations(); expect(Translator.load.args).toHaveLength(2); expect(Translator.load.calledWith(MMM, "translations/de.json", false)).toBe(true); expect(Translator.load.calledWith(MMM, "translations/en.json", true)).toBe(true); done(); }; }); }); it("should load translation fallback file", () => { return new Promise((done) => { dom.window.onload = async () => { const { Translator, Module, config } = dom.window; config.language = "--"; Translator.load = sinon.stub().callsFake((_m, _f, _fb) => null); Module.register("name", { getTranslations: () => translations }); const MMM = Module.create("name"); await MMM.loadTranslations(); expect(Translator.load.args).toHaveLength(1); expect(Translator.load.calledWith(MMM, "translations/en.json", true)).toBe(true); done(); }; }); }); it("should load no file", () => { return new Promise((done) => { dom.window.onload = async () => { const { Translator, Module } = dom.window; Translator.load = sinon.stub(); Module.register("name", {}); const MMM = Module.create("name"); await MMM.loadTranslations(); 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", () => { for (let language in translations) { it(`should parse ${language}`, () => { return new Promise((done) => { const dom = new JSDOM( `\ \ \