Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

translations_spec.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. const fs = require("fs");
  2. const path = require("path");
  3. const translations = require("../../translations/translations.js");
  4. const helmet = require("helmet");
  5. const { JSDOM } = require("jsdom");
  6. const express = require("express");
  7. const sinon = require("sinon");
  8. describe("Translations", function () {
  9. let server;
  10. beforeAll(function () {
  11. const app = express();
  12. app.use(helmet());
  13. app.use(function (req, res, next) {
  14. res.header("Access-Control-Allow-Origin", "*");
  15. next();
  16. });
  17. app.use("/translations", express.static(path.join(__dirname, "..", "..", "translations")));
  18. server = app.listen(3000);
  19. });
  20. afterAll(function () {
  21. server.close();
  22. });
  23. it("should have a translation file in the specified path", function () {
  24. for (let language in translations) {
  25. const file = fs.statSync(translations[language]);
  26. expect(file.isFile()).toBe(true);
  27. }
  28. });
  29. describe("loadTranslations", () => {
  30. let dom;
  31. beforeEach(() => {
  32. dom = new JSDOM(
  33. `<script>var Translator = {}; var Log = {log: function(){}}; var config = {language: 'de'};</script>\
  34. <script src="file://${path.join(__dirname, "..", "..", "js", "class.js")}"></script>\
  35. <script src="file://${path.join(__dirname, "..", "..", "js", "module.js")}"></script>`,
  36. { runScripts: "dangerously", resources: "usable" }
  37. );
  38. });
  39. it("should load translation file", (done) => {
  40. dom.window.onload = async function () {
  41. const { Translator, Module, config } = dom.window;
  42. config.language = "en";
  43. Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());
  44. Module.register("name", { getTranslations: () => translations });
  45. const MMM = Module.create("name");
  46. const loaded = sinon.stub();
  47. MMM.loadTranslations(loaded);
  48. expect(loaded.callCount).toBe(1);
  49. expect(Translator.load.args.length).toBe(1);
  50. expect(Translator.load.calledWith(MMM, "translations/en.json", false, sinon.match.func)).toBe(true);
  51. done();
  52. };
  53. });
  54. it("should load translation + fallback file", (done) => {
  55. dom.window.onload = async function () {
  56. const { Translator, Module } = dom.window;
  57. Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());
  58. Module.register("name", { getTranslations: () => translations });
  59. const MMM = Module.create("name");
  60. const loaded = sinon.stub();
  61. MMM.loadTranslations(loaded);
  62. expect(loaded.callCount).toBe(1);
  63. expect(Translator.load.args.length).toBe(2);
  64. expect(Translator.load.calledWith(MMM, "translations/de.json", false, sinon.match.func)).toBe(true);
  65. expect(Translator.load.calledWith(MMM, "translations/en.json", true, sinon.match.func)).toBe(true);
  66. done();
  67. };
  68. });
  69. it("should load translation fallback file", (done) => {
  70. dom.window.onload = async function () {
  71. const { Translator, Module, config } = dom.window;
  72. config.language = "--";
  73. Translator.load = sinon.stub().callsFake((_m, _f, _fb, callback) => callback());
  74. Module.register("name", { getTranslations: () => translations });
  75. const MMM = Module.create("name");
  76. const loaded = sinon.stub();
  77. MMM.loadTranslations(loaded);
  78. expect(loaded.callCount).toBe(1);
  79. expect(Translator.load.args.length).toBe(1);
  80. expect(Translator.load.calledWith(MMM, "translations/en.json", true, sinon.match.func)).toBe(true);
  81. done();
  82. };
  83. });
  84. it("should load no file", (done) => {
  85. dom.window.onload = async function () {
  86. const { Translator, Module } = dom.window;
  87. Translator.load = sinon.stub();
  88. Module.register("name", {});
  89. const MMM = Module.create("name");
  90. const loaded = sinon.stub();
  91. MMM.loadTranslations(loaded);
  92. expect(loaded.callCount).toBe(1);
  93. expect(Translator.load.callCount).toBe(0);
  94. done();
  95. };
  96. });
  97. });
  98. const mmm = {
  99. name: "TranslationTest",
  100. file(file) {
  101. return `http://localhost:3000/${file}`;
  102. }
  103. };
  104. describe("Parsing language files through the Translator class", function () {
  105. for (let language in translations) {
  106. it(`should parse ${language}`, function (done) {
  107. const dom = new JSDOM(
  108. `<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
  109. <script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
  110. { runScripts: "dangerously", resources: "usable" }
  111. );
  112. dom.window.onload = function () {
  113. const { Translator } = dom.window;
  114. Translator.load(mmm, translations[language], false, function () {
  115. expect(typeof Translator.translations[mmm.name]).toBe("object");
  116. expect(Object.keys(Translator.translations[mmm.name]).length).toBeGreaterThanOrEqual(1);
  117. done();
  118. });
  119. };
  120. });
  121. }
  122. });
  123. describe("Same keys", function () {
  124. let base;
  125. let missing = [];
  126. beforeAll(function (done) {
  127. const dom = new JSDOM(
  128. `<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
  129. <script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
  130. { runScripts: "dangerously", resources: "usable" }
  131. );
  132. dom.window.onload = function () {
  133. const { Translator } = dom.window;
  134. Translator.load(mmm, translations.en, false, function () {
  135. base = Object.keys(Translator.translations[mmm.name]).sort();
  136. done();
  137. });
  138. };
  139. });
  140. afterAll(function () {
  141. console.log(missing);
  142. });
  143. for (let language in translations) {
  144. if (language === "en") {
  145. continue;
  146. }
  147. describe(`Translation keys of ${language}`, function () {
  148. let keys;
  149. beforeAll(function (done) {
  150. const dom = new JSDOM(
  151. `<script>var translations = ${JSON.stringify(translations)}; var Log = {log: function(){}};</script>\
  152. <script src="file://${path.join(__dirname, "..", "..", "js", "translator.js")}">`,
  153. { runScripts: "dangerously", resources: "usable" }
  154. );
  155. dom.window.onload = function () {
  156. const { Translator } = dom.window;
  157. Translator.load(mmm, translations[language], false, function () {
  158. keys = Object.keys(Translator.translations[mmm.name]).sort();
  159. done();
  160. });
  161. };
  162. });
  163. it(`${language} keys should be in base`, function () {
  164. keys.forEach(function (key) {
  165. expect(base.indexOf(key)).toBeGreaterThanOrEqual(0);
  166. });
  167. });
  168. it(`${language} should contain all base keys`, function () {
  169. // TODO: when all translations are fixed, use
  170. // expect(keys).toEqual(base);
  171. // instead of the try-catch-block
  172. try {
  173. expect(keys).toEqual(base);
  174. } catch (e) {
  175. if (e.message.match(/expect.*toEqual/)) {
  176. const diff = base.filter((key) => !keys.includes(key));
  177. missing.push(`Missing Translations for language ${language}: ${diff}`);
  178. } else {
  179. throw e;
  180. }
  181. }
  182. });
  183. });
  184. }
  185. });
  186. });