Skip to content
Snippets Groups Projects
Select Git revision
  • 5b95ec7c69695006c93391a2095d8f604c8fbb84
  • master default protected
  • 1.31
  • 4.38.8
  • 4.38.7
  • 4.38.6
  • 4.38.5
  • 4.38.4
  • 4.38.3
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
23 results

index.html

Blame
  • 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);
                });
            })
    
    
        })
    
    
    });