Select Git revision
pipe.mjs 3.75 KiB
"use strict";
import {expect} from "chai"
import {Pipe} from "../../../source/data/pipe.mjs";
import {initJSDOM} from "../../util/jsdom.mjs";
import {Embed} from "../../../source/i18n/providers/embed.mjs";
describe('Pipe', function () {
before(function (done) {
let promises = []
if (!globalThis['crypto']) {
promises.push(import("@peculiar/webcrypto").then(m => {
globalThis['crypto'] = new m.Crypto();
return true;
}))
}
Promise.all(promises).then(() => {
done()
});
});
describe('run different pipes', function () {
[
['path:b | if:x:\\ ', {a: true}, ' '], // '\\ '.length ↦ 2
['path:a | if:x:\\ ', {a: true}, 'x'],
['nop', 'abcdefghijklmnop', 'abcdefghijklmnop'],
].forEach(function (data) {
let a = data.shift()
let b = data.shift()
let c = data.shift()
it('Pipe.run(' + JSON.stringify(a) + ').run(' + JSON.stringify(b) + ') should return ' + JSON.stringify(c), function () {
let t = new Pipe(a);
const r = t.run(b);
expect(r).to.be.eql(c);
});
});
});
describe('new Pipe should create new Instance', function () {
it('should return Instance', function () {
expect(new Pipe('')).to.be.instanceOf(Pipe);
});
it('should return Instance', function () {
expect(new Pipe('index:a|toupper |prefix:a').run({a: "test"})).to.be.equal('aTEST');
});
[
['index:a|toupper |prefix:a', {a: "test"}, 'aTEST'],
['path:a.b.c.d | toupper | prefix:Hello\\ ', {
a: {
b: {
c: {
d: "world"
}
}
}
}, 'Hello WORLD'],
['path:a.b.c|index:d|toupper |suffix:x', {a: {b: {c: {d: "test"}}}}, 'TESTx'],
].forEach(function (data) {
let a = data.shift()
let b = data.shift()
let c = data.shift()
it('new Pipe(' + JSON.stringify(a) + ').run(' + JSON.stringify(b) + ') should return ' + JSON.stringify(c) + ' ', function () {
let t = new Pipe(a);
expect(t.run(b)).to.be.equal(c);
});
});
});
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);
});
})
})
});