Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

View File

@ -0,0 +1,59 @@
import test from 'ava';
import { setup } from './helpers/setup';
test('should pre-fill template-string data', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{foo}} {{bar}} {{baz}}');
wax.data({ foo: 'hello', bar: 'world' });
t.is(waxedTemplate(), 'hello world ');
t.is(waxedTemplate({ foo: 'a' }), 'a world ');
t.is(waxedTemplate({ bar: 'b' }), 'hello b ');
t.is(waxedTemplate({ baz: 'c' }), 'hello world c');
});
test('should pre-fill template-function data', async t => {
const { hb, wax } = setup();
const template = hb.compile('{{foo}} {{bar}} {{baz}}');
const waxedTemplate = wax.compile(template);
wax.data({ foo: 'hello', bar: 'world' });
t.is(template(), ' ');
t.is(template({ foo: 'a' }), 'a ');
t.is(template({ bar: 'b' }), ' b ');
t.is(template({ baz: 'c' }), ' c');
t.is(waxedTemplate(), 'hello world ');
t.is(waxedTemplate({ foo: 'a' }), 'a world ');
t.is(waxedTemplate({ bar: 'b' }), 'hello b ');
t.is(waxedTemplate({ baz: 'c' }), 'hello world c');
});
test('should set registered data as _parent', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{_parent.foo}} {{foo}}');
wax.data({ foo: 'hello' });
t.is(waxedTemplate({ foo: 'world' }), 'hello world');
});
test('should set registered data as @root', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{@root.foo}} {{foo}}');
wax.data({ foo: 'hello' });
t.is(waxedTemplate({ foo: 'world' }), 'hello world');
});
test('should prefer user-specified @root', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{foo}} {{_parent.foo}} {{@root.foo}} {{@root._parent.foo}}');
wax.data({ foo: 'hello' });
t.is(waxedTemplate({ foo: 'world' }, { data: { root: { foo: 'bye' } } }), 'world hello bye hello');
});

View File

@ -0,0 +1,30 @@
import test from 'ava';
import { setup } from './helpers/setup';
test('should not modify data', async t => {
const { wax, defaultData } = setup();
wax.data();
t.deepEqual(Object.keys(wax.context), defaultData);
});
test('should register data by object', async t => {
const { wax } = setup();
const foo = 'hello';
const bar = 'world';
wax.data({ foo, bar });
t.is(wax.context.foo, foo);
t.is(wax.context.bar, bar);
});
test('should register data by globbed object', async t => {
const { wax } = setup();
wax.data('./fixtures/data/object/**/*.{js,json}');
t.is(wax.context.hello, 'world');
t.deepEqual(wax.context.good.night, ['chair', 'bear', 'moon']);
});

View File

@ -0,0 +1,65 @@
import test from 'ava';
import { setup } from './helpers/setup';
test('should not modify decorators', async t => {
const { hb, wax, defaultDecorators } = setup();
wax.decorators().decorators('./fixtures/decorators/bogu*.js');
t.deepEqual(Object.keys(hb.decorators), defaultDecorators);
});
test('should register decorators by object', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.decorators({ foo, bar });
t.is(hb.decorators.foo, foo);
t.is(hb.decorators.bar, bar);
});
test('should register decorators by globbed factory', async t => {
const { hb, wax } = setup();
wax.decorators('./fixtures/decorators/factory/**/*.js');
t.is(typeof hb.decorators.currencyDecimal, 'function');
t.is(typeof hb.decorators.currencyFormat, 'function');
t.is(typeof hb.decorators.i18nLanguage, 'function');
t.is(typeof hb.decorators.i18nCountry, 'function');
t.is(hb.decorators.empty, undefined);
});
test('should register decorators by globbed function', async t => {
const { hb, wax } = setup();
wax.decorators('./fixtures/decorators/function/**/*.{hbs,js}');
t.is(typeof hb.decorators['currency-decimal'], 'function');
t.is(typeof hb.decorators['currency-format'], 'function');
t.is(typeof hb.decorators.language, 'function');
t.is(typeof hb.decorators.country, 'function');
t.is(hb.decorators.empty, undefined);
});
test('should register decorators by globbed object', async t => {
const { hb, wax } = setup();
wax.decorators('./fixtures/decorators/object/**/*.js');
t.is(typeof hb.decorators.currencyDecimal, 'function');
t.is(typeof hb.decorators.currencyFormat, 'function');
t.is(typeof hb.decorators.i18nLanguage, 'function');
t.is(typeof hb.decorators.i18nCountry, 'function');
t.is(hb.decorators.empty, undefined);
});
test('should raise errors', async t => {
const { wax } = setup();
const template = wax.compile('{{* foo}}');
t.throws(() => template(), /not a function/i);
});

View File

@ -0,0 +1 @@
["chair", "bear", "moon"]

View File

@ -0,0 +1 @@
module.exports = 'world';

View File

@ -0,0 +1,12 @@
'use strict';
module.exports.register = function (hb) {
hb.registerDecorator({
currencyDecimal: function (str) {
// do stuff
},
currencyFormat: function (str) {
// do stuff
}
});
};

View File

@ -0,0 +1,12 @@
'use strict';
module.exports.register = function (hb) {
hb.registerDecorator({
i18nLanguage: function (lang) {
// do stuff
},
i18nCountry: function (str) {
// do stuff
}
});
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function (str) {
// do stuff
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function (str) {
// do stuff
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function (str) {
// do stuff
};

View File

@ -0,0 +1,2 @@
'use strict';
module.exports = null;

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function (str) {
// do stuff
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
currencyDecimal: function (str) {
// do stuff
},
currencyFormat: function (str) {
// do stuff
}
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
i18nLanguage: function (lang) {
// do stuff
},
i18nCountry: function (str) {
// do stuff
}
};

View File

@ -0,0 +1,12 @@
'use strict';
module.exports.register = function (hb) {
hb.registerHelper({
lower: function (str) {
return str.toLowerCase();
},
upper: function (str) {
return str.toUpperCase();
}
});
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports.register = function (hb) {
hb.registerHelper('lest', function (a, b) {
return a !== b;
});
hb.registerHelper('when', function (a, b) {
return a === b;
});
};

View File

@ -0,0 +1,2 @@
'use strict';
module.exports = null;

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function lest(a, b) {
return a !== b;
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function when(a, b) {
return a === b;
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function lower(str) {
return str.toLowerCase();
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = function upper(str) {
return str.toUpperCase();
};

View File

@ -0,0 +1 @@
module.exports = true;

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
lower: function (str) {
return str.toLowerCase();
},
upper: function (str) {
return str.toUpperCase();
}
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
lest: function (a, b) {
return a !== b;
},
when: function (a, b) {
return a === b;
}
};

View File

@ -0,0 +1,8 @@
'use strict';
module.exports.register = function (hb) {
hb.registerPartial({
item: '<li>{{label}}</li>',
link: '<a href="{{url}}">{{label}}</a>'
});
};

View File

@ -0,0 +1,6 @@
'use strict';
module.exports.register = function (hb) {
hb.registerPartial('layout', '<!doctype html>\n<html>\n<head>\n <title>{{{block "title"}}}</title>\n</head>\n<body>\n {{{block "body"}}}\n</body>\n</html>');
hb.registerPartial('layout-2col', '{{#extend "layout"}}\n {{#content "body"}}\n {{{block "left"}}}\n {{{block "right"}}}\n {{/content}}\n{{/extend}}');
};

View File

@ -0,0 +1 @@
<li>{{label}}</li>

View File

@ -0,0 +1 @@
<a href="{{url}}">{{label}}</a>

View File

@ -0,0 +1,6 @@
{{#extend "layout"}}
{{#content "body"}}
{{{block "left"}}}
{{{block "right"}}}
{{/content}}
{{/extend}}

View File

@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<title>{{{block "title"}}}</title>
</head>
<body>
{{{block "body"}}}
</body>
</html>

View File

@ -0,0 +1,6 @@
'use strict';
module.exports = {
item: '<li>{{label}}</li>',
link: '<a href="{{url}}">{{label}}</a>'
};

View File

@ -0,0 +1,6 @@
'use strict';
module.exports = {
layout: '<!doctype html>\n<html>\n<head>\n <title>{{{block "title"}}}</title>\n</head>\n<body>\n {{{block "body"}}}\n</body>\n</html>',
'layout-2col': '{{#extend "layout"}}\n {{#content "body"}}\n {{{block "left"}}}\n {{{block "right"}}}\n {{/content}}\n{{/extend}}'
};

View File

@ -0,0 +1,65 @@
import test from 'ava';
import { setup } from './helpers/setup';
test('should not modify helpers', async t => {
const { hb, wax, defaultHelpers } = setup();
wax.helpers().helpers('./fixtures/helpers/bogu*.js');
t.deepEqual(Object.keys(hb.helpers), defaultHelpers);
});
test('should register helpers by object', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.helpers({ foo, bar });
t.is(hb.helpers.foo, foo);
t.is(hb.helpers.bar, bar);
});
test('should register helpers by globbed factory', async t => {
const { hb, wax } = setup();
wax.helpers('./fixtures/helpers/factory/**/*.js');
t.is(typeof hb.helpers.lower, 'function');
t.is(typeof hb.helpers.upper, 'function');
t.is(typeof hb.helpers.lest, 'function');
t.is(typeof hb.helpers.when, 'function');
t.is(hb.helpers.empty, undefined);
});
test('should register helpers by globbed function', async t => {
const { hb, wax } = setup();
wax.helpers('./fixtures/helpers/function/**/*.{hbs,js}');
t.is(typeof hb.helpers.lower, 'function');
t.is(typeof hb.helpers.upper, 'function');
t.is(typeof hb.helpers['flow-lest'], 'function');
t.is(typeof hb.helpers['flow-when'], 'function');
t.is(hb.helpers.empty, undefined);
});
test('should register helpers by globbed object', async t => {
const { hb, wax } = setup();
wax.helpers('./fixtures/helpers/object/**/*.js');
t.is(typeof hb.helpers.lower, 'function');
t.is(typeof hb.helpers.upper, 'function');
t.is(typeof hb.helpers.lest, 'function');
t.is(typeof hb.helpers.when, 'function');
t.is(hb.helpers.empty, undefined);
});
test('should raise errors', async t => {
const { wax } = setup();
const template = wax.compile('{{{foo "bar"}}}');
t.throws(() => template(), /missing helper/i);
});

View File

@ -0,0 +1,21 @@
var handlebars = require('handlebars');
var handlebarsWax = require('../../src/handlebars-wax');
exports.setup = function setup() {
var hb = handlebars.create();
var defaultPartials = Object.keys(hb.partials);
var defaultHelpers = Object.keys(hb.helpers);
var defaultDecorators = Object.keys(hb.decorators);
var wax = handlebarsWax(hb);
var defaultData = Object.keys(wax.context);
return {
hb: hb,
wax: wax,
defaultPartials: defaultPartials,
defaultHelpers: defaultHelpers,
defaultDecorators: defaultDecorators,
defaultData: defaultData
};
};

View File

@ -0,0 +1,111 @@
import test from 'ava';
import handlebars from 'handlebars';
import { setup } from './helpers/setup';
test('should not modify partials', async t => {
const { hb, wax, defaultPartials } = setup();
wax.partials().partials('./fixtures/helpers/bogu*.js');
t.deepEqual(Object.keys(hb.partials), defaultPartials);
});
test('should register partials by factory', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.partials({
register: function (handlebars) {
t.is(handlebars, hb);
handlebars.registerPartial('foo', foo);
}
});
wax.partials({
register: function (handlebars) {
t.is(handlebars, hb);
return { bar };
}
});
t.is(hb.partials.foo, foo);
t.is(hb.partials.bar, bar);
});
test('should register partials by function', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.partials(function (handlebars) {
t.is(handlebars, hb);
handlebars.registerPartial('foo', foo);
});
wax.partials(function (handlebars) {
t.is(handlebars, hb);
return { bar };
});
t.is(hb.partials.foo, foo);
t.is(hb.partials.bar, bar);
});
test('should register partials by object', async t => {
const { hb, wax } = setup();
function foo() {}
function bar() {}
wax.partials({ foo, bar });
t.is(hb.partials.foo, foo);
t.is(hb.partials.bar, bar);
});
test('should register partials by globbed factory', async t => {
const { hb, wax } = setup();
wax.partials('./fixtures/partials/factory/**/*.js');
t.is(typeof hb.partials.item, 'string');
t.is(typeof hb.partials.link, 'string');
t.is(typeof hb.partials.layout, 'string');
t.is(typeof hb.partials['layout-2col'], 'string');
});
test('should register partials by globbed function', async t => {
const { hb, wax } = setup();
wax.partials('./fixtures/partials/function/**/*.{hbs,js}');
t.is(typeof hb.partials['components/item'], 'function');
t.is(typeof hb.partials['components/link'], 'function');
t.is(typeof hb.partials.layout, 'function');
t.is(typeof hb.partials['layout-2col'], 'function');
});
test('should register partials by globbed object', async t => {
const { hb, wax } = setup();
wax.partials('./fixtures/partials/object/**/*.js');
t.is(typeof hb.partials.item, 'string');
t.is(typeof hb.partials.link, 'string');
t.is(typeof hb.partials.layout, 'string');
t.is(typeof hb.partials['layout-2col'], 'string');
});
test('should raise errors', async t => {
const { wax } = setup();
const waxedTemplate = wax.compile('{{> foo}}');
t.throws(() => waxedTemplate(), /could not be found/i);
});
test.after('should not cause cross-contamination', async t => {
t.is(Object.keys(handlebars.partials).length, 0);
});