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.

translator_spec.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. const path = require("path");
  2. const helmet = require("helmet");
  3. const { JSDOM } = require("jsdom");
  4. const express = require("express");
  5. const sockets = new Set();
  6. describe("Translator", function () {
  7. let server;
  8. beforeAll(function () {
  9. const app = express();
  10. app.use(helmet());
  11. app.use(function (req, res, next) {
  12. res.header("Access-Control-Allow-Origin", "*");
  13. next();
  14. });
  15. app.use("/translations", express.static(path.join(__dirname, "..", "..", "..", "tests", "configs", "data")));
  16. server = app.listen(3000);
  17. server.on("connection", (socket) => {
  18. sockets.add(socket);
  19. });
  20. });
  21. afterAll(function () {
  22. for (const socket of sockets) {
  23. socket.destroy();
  24. sockets.delete(socket);
  25. }
  26. server.close();
  27. });
  28. describe("translate", function () {
  29. const translations = {
  30. "MMM-Module": {
  31. Hello: "Hallo",
  32. "Hello {username}": "Hallo {username}"
  33. }
  34. };
  35. const coreTranslations = {
  36. Hello: "XXX",
  37. "Hello {username}": "XXX",
  38. FOO: "Foo",
  39. "BAR {something}": "Bar {something}"
  40. };
  41. const translationsFallback = {
  42. "MMM-Module": {
  43. Hello: "XXX",
  44. "Hello {username}": "XXX",
  45. FOO: "XXX",
  46. "BAR {something}": "XXX",
  47. "A key": "A translation"
  48. }
  49. };
  50. const coreTranslationsFallback = {
  51. FOO: "XXX",
  52. "BAR {something}": "XXX",
  53. Hello: "XXX",
  54. "Hello {username}": "XXX",
  55. "A key": "XXX",
  56. Fallback: "core fallback"
  57. };
  58. /**
  59. * @param {object} Translator the global Translator object
  60. */
  61. function setTranslations(Translator) {
  62. Translator.translations = translations;
  63. Translator.coreTranslations = coreTranslations;
  64. Translator.translationsFallback = translationsFallback;
  65. Translator.coreTranslationsFallback = coreTranslationsFallback;
  66. }
  67. it("should return custom module translation", function (done) {
  68. const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  69. dom.window.onload = function () {
  70. const { Translator } = dom.window;
  71. setTranslations(Translator);
  72. let translation = Translator.translate({ name: "MMM-Module" }, "Hello");
  73. expect(translation).toBe("Hallo");
  74. translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}", { username: "fewieden" });
  75. expect(translation).toBe("Hallo fewieden");
  76. done();
  77. };
  78. });
  79. it("should return core translation", function (done) {
  80. const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  81. dom.window.onload = function () {
  82. const { Translator } = dom.window;
  83. setTranslations(Translator);
  84. let translation = Translator.translate({ name: "MMM-Module" }, "FOO");
  85. expect(translation).toBe("Foo");
  86. translation = Translator.translate({ name: "MMM-Module" }, "BAR {something}", { something: "Lorem Ipsum" });
  87. expect(translation).toBe("Bar Lorem Ipsum");
  88. done();
  89. };
  90. });
  91. it("should return custom module translation fallback", function (done) {
  92. const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  93. dom.window.onload = function () {
  94. const { Translator } = dom.window;
  95. setTranslations(Translator);
  96. const translation = Translator.translate({ name: "MMM-Module" }, "A key");
  97. expect(translation).toBe("A translation");
  98. done();
  99. };
  100. });
  101. it("should return core translation fallback", function (done) {
  102. const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  103. dom.window.onload = function () {
  104. const { Translator } = dom.window;
  105. setTranslations(Translator);
  106. const translation = Translator.translate({ name: "MMM-Module" }, "Fallback");
  107. expect(translation).toBe("core fallback");
  108. done();
  109. };
  110. });
  111. it("should return translation with placeholder for missing variables", function (done) {
  112. const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  113. dom.window.onload = function () {
  114. const { Translator } = dom.window;
  115. setTranslations(Translator);
  116. const translation = Translator.translate({ name: "MMM-Module" }, "Hello {username}");
  117. expect(translation).toBe("Hallo {username}");
  118. done();
  119. };
  120. });
  121. it("should return key if no translation was found", function (done) {
  122. const dom = new JSDOM(`<script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  123. dom.window.onload = function () {
  124. const { Translator } = dom.window;
  125. setTranslations(Translator);
  126. const translation = Translator.translate({ name: "MMM-Module" }, "MISSING");
  127. expect(translation).toBe("MISSING");
  128. done();
  129. };
  130. });
  131. });
  132. describe("load", function () {
  133. const mmm = {
  134. name: "TranslationTest",
  135. file(file) {
  136. return `http://localhost:3000/translations/${file}`;
  137. }
  138. };
  139. it("should load translations", function (done) {
  140. const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  141. dom.window.onload = function () {
  142. const { Translator } = dom.window;
  143. const file = "TranslationTest.json";
  144. Translator.load(mmm, file, false, function () {
  145. const json = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", file));
  146. expect(Translator.translations[mmm.name]).toEqual(json);
  147. done();
  148. });
  149. };
  150. });
  151. it("should load translation fallbacks", function (done) {
  152. const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  153. dom.window.onload = function () {
  154. const { Translator } = dom.window;
  155. const file = "TranslationTest.json";
  156. Translator.load(mmm, file, true, function () {
  157. const json = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", file));
  158. expect(Translator.translationsFallback[mmm.name]).toEqual(json);
  159. done();
  160. });
  161. };
  162. });
  163. it("should not load translations, if module fallback exists", function (done) {
  164. const dom = new JSDOM(`<script>var Log = {log: function(){}};</script><script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`, { runScripts: "dangerously", resources: "usable" });
  165. dom.window.onload = function () {
  166. const { Translator, XMLHttpRequest } = dom.window;
  167. const file = "TranslationTest.json";
  168. XMLHttpRequest.prototype.send = function () {
  169. throw "Shouldn't load files";
  170. };
  171. Translator.translationsFallback[mmm.name] = {
  172. Hello: "Hallo"
  173. };
  174. Translator.load(mmm, file, false, function () {
  175. expect(Translator.translations[mmm.name]).toBe(undefined);
  176. expect(Translator.translationsFallback[mmm.name]).toEqual({
  177. Hello: "Hallo"
  178. });
  179. done();
  180. });
  181. };
  182. });
  183. });
  184. describe("loadCoreTranslations", function () {
  185. it("should load core translations and fallback", function (done) {
  186. const dom = new JSDOM(
  187. `<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
  188. <script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
  189. { runScripts: "dangerously", resources: "usable" }
  190. );
  191. dom.window.onload = function () {
  192. const { Translator } = dom.window;
  193. Translator.loadCoreTranslations("en");
  194. const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
  195. setTimeout(function () {
  196. expect(Translator.coreTranslations).toEqual(en);
  197. expect(Translator.coreTranslationsFallback).toEqual(en);
  198. done();
  199. }, 500);
  200. };
  201. });
  202. it("should load core fallback if language cannot be found", function (done) {
  203. const dom = new JSDOM(
  204. `<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
  205. <script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
  206. { runScripts: "dangerously", resources: "usable" }
  207. );
  208. dom.window.onload = function () {
  209. const { Translator } = dom.window;
  210. Translator.loadCoreTranslations("MISSINGLANG");
  211. const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
  212. setTimeout(function () {
  213. expect(Translator.coreTranslations).toEqual({});
  214. expect(Translator.coreTranslationsFallback).toEqual(en);
  215. done();
  216. }, 500);
  217. };
  218. });
  219. });
  220. describe("loadCoreTranslationsFallback", function () {
  221. it("should load core translations fallback", function (done) {
  222. const dom = new JSDOM(
  223. `<script>var translations = {en: "http://localhost:3000/translations/en.json"}; var Log = {log: function(){}};</script>\
  224. <script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
  225. { runScripts: "dangerously", resources: "usable" }
  226. );
  227. dom.window.onload = function () {
  228. const { Translator } = dom.window;
  229. Translator.loadCoreTranslationsFallback();
  230. const en = require(path.join(__dirname, "..", "..", "..", "tests", "configs", "data", "en.json"));
  231. setTimeout(function () {
  232. expect(Translator.coreTranslationsFallback).toEqual(en);
  233. done();
  234. }, 500);
  235. };
  236. });
  237. it("should load core fallback if language cannot be found", function (done) {
  238. const dom = new JSDOM(
  239. `<script>var translations = {}; var Log = {log: function(){}};</script>\
  240. <script src="file://${path.join(__dirname, "..", "..", "..", "js", "translator.js")}">`,
  241. { runScripts: "dangerously", resources: "usable" }
  242. );
  243. dom.window.onload = function () {
  244. const { Translator } = dom.window;
  245. Translator.loadCoreTranslations();
  246. setTimeout(function () {
  247. expect(Translator.coreTranslationsFallback).toEqual({});
  248. done();
  249. }, 500);
  250. };
  251. });
  252. });
  253. });