mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-28 09:01:44 +00:00
[tests] Review and refactor translation tests (#3792)
I have refactored the translations tests, they should now be clearer and easier to understand. There should be no functional impact. I have discarded the original approach of also replacing `XMLHttpRequest` with `fetch` in the file `js/translator.js`. I had managed to get it to work functionally, but I couldn't get the tests to work.
This commit is contained in:
committed by
GitHub
parent
c7c0e67c1d
commit
2809ed1750
@@ -6,7 +6,7 @@ const express = require("express");
|
||||
const sinon = require("sinon");
|
||||
const translations = require("../../translations/translations");
|
||||
|
||||
describe("Translations", () => {
|
||||
describe("translations", () => {
|
||||
let server;
|
||||
|
||||
beforeAll(() => {
|
||||
@@ -26,8 +26,9 @@ describe("Translations", () => {
|
||||
});
|
||||
|
||||
it("should have a translation file in the specified path", () => {
|
||||
for (let language in translations) {
|
||||
for (const language in translations) {
|
||||
const file = fs.statSync(translations[language]);
|
||||
|
||||
expect(file.isFile()).toBe(true);
|
||||
}
|
||||
});
|
||||
@@ -36,90 +37,91 @@ describe("Translations", () => {
|
||||
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" }
|
||||
);
|
||||
// Create a new JSDOM instance for each test
|
||||
dom = new JSDOM("", { runScripts: "dangerously", resources: "usable" });
|
||||
|
||||
// Mock the necessary global objects
|
||||
dom.window.Log = { log: jest.fn(), error: jest.fn() };
|
||||
dom.window.Translator = {};
|
||||
dom.window.config = { language: "de" };
|
||||
|
||||
// Load class.js and module.js content directly
|
||||
const classJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "class.js"), "utf-8");
|
||||
const moduleJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "module.js"), "utf-8");
|
||||
|
||||
// Execute the scripts in the JSDOM context
|
||||
dom.window.eval(classJs);
|
||||
dom.window.eval(moduleJs);
|
||||
});
|
||||
|
||||
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 file", async () => {
|
||||
await new Promise((resolve) => {
|
||||
dom.window.onload = resolve;
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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", async () => {
|
||||
await new Promise((resolve) => {
|
||||
dom.window.onload = resolve;
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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 translation fallback file", async () => {
|
||||
await new Promise((resolve) => {
|
||||
dom.window.onload = resolve;
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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();
|
||||
};
|
||||
it("should load no file", async () => {
|
||||
await new Promise((resolve) => {
|
||||
dom.window.onload = resolve;
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -130,29 +132,30 @@ describe("Translations", () => {
|
||||
}
|
||||
};
|
||||
|
||||
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(
|
||||
`<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 = async () => {
|
||||
const { Translator } = dom.window;
|
||||
const translatorJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "translator.js"), "utf-8");
|
||||
|
||||
await Translator.load(mmm, translations[language], false);
|
||||
expect(typeof Translator.translations[mmm.name]).toBe("object");
|
||||
expect(Object.keys(Translator.translations[mmm.name]).length).toBeGreaterThanOrEqual(1);
|
||||
done();
|
||||
};
|
||||
describe("parsing language files through the Translator class", () => {
|
||||
for (const language in translations) {
|
||||
it(`should parse ${language}`, async () => {
|
||||
const dom = new JSDOM("", { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
dom.window.translations = translations;
|
||||
dom.window.eval(translatorJs);
|
||||
|
||||
await new Promise((resolve) => {
|
||||
dom.window.onload = resolve;
|
||||
});
|
||||
|
||||
const { Translator } = dom.window;
|
||||
await Translator.load(mmm, translations[language], false);
|
||||
|
||||
expect(typeof Translator.translations[mmm.name]).toBe("object");
|
||||
expect(Object.keys(Translator.translations[mmm.name]).length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("Same keys", () => {
|
||||
describe("same keys", () => {
|
||||
let base;
|
||||
|
||||
// Some expressions are not easy to translate automatically. For the sake of a working test, we filter them out.
|
||||
@@ -178,7 +181,6 @@ describe("Translations", () => {
|
||||
const dom = new JSDOM("", { runScripts: "dangerously", resources: "usable" });
|
||||
dom.window.Log = { log: jest.fn() };
|
||||
dom.window.translations = translations;
|
||||
const translatorJs = fs.readFileSync(path.join(__dirname, "..", "..", "js", "translator.js"), "utf-8");
|
||||
dom.window.eval(translatorJs);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
|
Reference in New Issue
Block a user