From 69e30f689e67959b31928a78ef7c66c15dfed149 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Sun, 26 Feb 2023 19:10:58 +0100 Subject: [PATCH] chore: add pipe and i18n test --- application/source/i18n/providers/embed.mjs | 4 +- development/test/cases/data/pipe.mjs | 70 +++++++++++++++++++-- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/application/source/i18n/providers/embed.mjs b/application/source/i18n/providers/embed.mjs index 0f2e5a1c0..c82e91e69 100644 --- a/application/source/i18n/providers/embed.mjs +++ b/application/source/i18n/providers/embed.mjs @@ -109,7 +109,7 @@ class Embed extends Provider { let translations = null; try { - translations = JSON.parse(this.translateElement.innerHTML); + translations = JSON.parse(this.translateElement.innerHTML.trim()); } catch (e) { reject(e); return; @@ -150,8 +150,6 @@ class Embed extends Provider { const promises = []; - let result - list.forEach((translationElement) => { const p = new Embed(translationElement); promises.push(p.assignToElement(undefined, element)); diff --git a/development/test/cases/data/pipe.mjs b/development/test/cases/data/pipe.mjs index 3f4f9fbac..5539e7dc0 100644 --- a/development/test/cases/data/pipe.mjs +++ b/development/test/cases/data/pipe.mjs @@ -3,6 +3,7 @@ import {expect} from "chai" import {Pipe} from "../../../../application/source/data/pipe.mjs"; import {initJSDOM} from "../../util/jsdom.mjs"; +import {Embed} from "../../../../application/source/i18n/providers/embed.mjs"; describe('Pipe', function () { @@ -10,7 +11,7 @@ describe('Pipe', function () { before(function (done) { let promises = [] - if(!globalThis['crypto']) { + if (!globalThis['crypto']) { promises.push(import("@peculiar/webcrypto").then(m => { globalThis['crypto'] = new m.Crypto(); return true; @@ -21,12 +22,12 @@ describe('Pipe', function () { done() }); - }); - + }); + describe('run different pipes', function () { [ - ['path:b | if:x:\\ ', {a:true}, ' '], // '\\ '.length ↦ 2 - ['path:a | if:x:\\ ', {a:true}, 'x'], + ['path:b | if:x:\\ ', {a: true}, ' '], // '\\ '.length ↦ 2 + ['path:a | if:x:\\ ', {a: true}, 'x'], ['nop', 'abcdefghijklmnop', 'abcdefghijklmnop'], ].forEach(function (data) { @@ -83,4 +84,63 @@ describe('Pipe', function () { }); + + describe('new Pipe and locale', function () { + + + let html1 = ` +<div id="mock-translations"></div> +<script type="application/json" data-monster-role="translations"> + { + "51": "xyz", + "52": "abc", + "53": "def" + } +</script> +`; + + beforeEach((done) => { + let mocks = document.getElementById('mocks'); + mocks.innerHTML = html1; + let elem = document.getElementById('mock-translations'); + Embed.assignTranslationsToElement(elem).then((o) => { + done() + }).catch((e) => { + done(e) + }) + + + }) + + afterEach(() => { + let mocks = document.getElementById('mocks'); + mocks.innerHTML = ""; + }) + + before(function (done) { + initJSDOM().then(() => { + done() + }); + }); + + [ + ['path:status | tostring | i18n', {status: 51}, "xyz"] + + + ].forEach(function (data) { + + let pipe = data.shift() + let obj = data.shift() + let expected = data.shift() + + it('should transform(' + pipe + ').run(' + JSON.stringify(obj) + ') return ' + JSON.stringify(expected), function () { + let t = new Pipe(pipe); + expect(t.run(obj)).to.be.equal(expected); + }); + }) + + + }) + + }); -- GitLab