import test from "ava"; import TemplateRender from "../src/TemplateRender"; // Handlebars test("Handlebars", t => { t.is(new TemplateRender("hbs").getEngineName(), "hbs"); }); test("Handlebars Render", async t => { let fn = await new TemplateRender("hbs").getCompiledTemplate( "

{{name}}

" ); t.is(await fn({ name: "Zach" }), "

Zach

"); }); test("Handlebars Render Unescaped Output (no HTML)", async t => { let fn = await new TemplateRender("hbs").getCompiledTemplate( "

{{{name}}}

" ); t.is(await fn({ name: "Zach" }), "

Zach

"); }); test("Handlebars Render Escaped Output", async t => { let fn = await new TemplateRender("hbs").getCompiledTemplate( "

{{name}}

" ); t.is(await fn({ name: "Zach" }), "

<b>Zach</b>

"); }); test("Handlebars Render Unescaped Output (HTML)", async t => { let fn = await new TemplateRender("hbs").getCompiledTemplate( "

{{{name}}}

" ); t.is(await fn({ name: "Zach" }), "

Zach

"); }); test("Handlebars Render Partial", async t => { let fn = await new TemplateRender("hbs", "./test/stubs/").getCompiledTemplate( "

{{> included}}

" ); t.is(await fn(), "

This is an include.

"); }); test.skip("Handlebars Render Partial (Relative)", async t => { let fn = await new TemplateRender( "./test/stubs/does_not_exist_and_thats_ok.hbs", "./test/stubs/" ).getCompiledTemplate("

{{> ./included}}

"); // not supported yet. t.is(await fn(), "

This is an includdde.

"); }); test("Handlebars Render Partial (Subdirectory)", async t => { let fn = await new TemplateRender("hbs", "./test/stubs/").getCompiledTemplate( "

{{> subfolder/included}}

" ); t.is(await fn(), "

This is an include.

"); }); test("Handlebars Render Partial with variable", async t => { let fn = await new TemplateRender("hbs", "./test/stubs/").getCompiledTemplate( "

{{> includedvar}}

" ); t.is(await fn({ name: "Zach" }), "

This is a Zach.

"); }); test("Handlebars Render: with Library Override", async t => { let tr = new TemplateRender("hbs"); let lib = require("handlebars"); tr.engine.setLibrary(lib); let fn = await tr.getCompiledTemplate("

{{name}}

"); t.is(await fn({ name: "Zach" }), "

Zach

"); }); test("Handlebars Render Helper", async t => { let tr = new TemplateRender("hbs"); tr.engine.addHelpers({ helpername: function() { return "Zach"; } }); let fn = await tr.getCompiledTemplate( "

This is a {{helpername}} {{name}}.

" ); t.is(await fn({ name: "Zach" }), "

This is a Zach Zach.

"); }); test("Handlebars Render Helper (uses argument)", async t => { let tr = new TemplateRender("hbs"); tr.engine.addHelpers({ helpername2: function(name) { return "Zach"; } }); let fn = await tr.getCompiledTemplate( "

This is a {{helpername2 name}}.

" ); t.is(await fn({ name: "Zach" }), "

This is a Zach.

"); }); test("Handlebars Render Shortcode", async t => { let tr = new TemplateRender("hbs"); tr.engine.addShortcodes({ shortcodename: function(name) { return name.toUpperCase(); } }); let fn = await tr.getCompiledTemplate( "

This is a {{shortcodename name}}.

" ); t.is(await fn({ name: "Howdy" }), "

This is a HOWDY.

"); }); test("Handlebars Render HTML in Shortcode (Issue #460)", async t => { let tr = new TemplateRender("hbs"); tr.engine.addShortcodes({ shortcodenamehtml: function(name) { return `${name.toUpperCase()}`; } }); let fn = await tr.getCompiledTemplate( "

This is a {{{shortcodenamehtml name}}}.

" ); t.is(await fn({ name: "Howdy" }), "

This is a HOWDY.

"); }); test("Handlebars Render Shortcode (Multiple args)", async t => { let tr = new TemplateRender("hbs"); tr.engine.addShortcodes({ shortcodename2: function(name, name2) { return name.toUpperCase() + name2.toUpperCase(); } }); let fn = await tr.getCompiledTemplate( "

This is a {{shortcodename2 name name2}}.

" ); t.is( await fn({ name: "Howdy", name2: "Zach" }), "

This is a HOWDYZACH.

" ); }); test("Handlebars Render Paired Shortcode", async t => { let tr = new TemplateRender("hbs"); tr.engine.addPairedShortcodes({ shortcodename3: function(content, name, options) { return (content + name).toUpperCase(); } }); let fn = await tr.getCompiledTemplate( "

This is a {{#shortcodename3 name}}Testing{{/shortcodename3}}.

" ); t.is(await fn({ name: "Howdy" }), "

This is a TESTINGHOWDY.

"); }); test("Handlebars Render Paired Shortcode (HTML)", async t => { let tr = new TemplateRender("hbs"); tr.engine.addPairedShortcodes({ shortcodename3html: function(content, name, options) { return `${(content + name).toUpperCase()}`; } }); let fn = await tr.getCompiledTemplate( "

This is a {{#shortcodename3html name}}Testing{{/shortcodename3html}}.

" ); t.is( await fn({ name: "Howdy" }), "

This is a TESTINGHOWDY.

" ); }); test("Handlebars Render Paired Shortcode (Spaces)", async t => { let tr = new TemplateRender("hbs"); tr.engine.addPairedShortcodes({ shortcodename4: function(content, name, options) { return (content + name).toUpperCase(); } }); let fn = await tr.getCompiledTemplate( "

This is a {{# shortcodename4 name }}Testing{{/ shortcodename4 }}.

" ); t.is(await fn({ name: "Howdy" }), "

This is a TESTINGHOWDY.

"); }); test("Handlebars Render Paired Shortcode with a Nested Single Shortcode", async t => { let tr = new TemplateRender("hbs"); tr.engine.addShortcodes({ shortcodechild: function(txt, options) { return txt; } }); tr.engine.addPairedShortcodes({ shortcodeparent: function(content, name, name2, options) { return (content + name + name2).toUpperCase(); } }); let fn = await tr.getCompiledTemplate( "

This is a {{# shortcodeparent name name2 }}{{shortcodechild 'CHILD CONTENT'}}{{/ shortcodeparent }}.

" ); t.is( await fn({ name: "Howdy", name2: "Zach" }), "

This is a CHILD CONTENTHOWDYZACH.

" ); }); test("Handlebars Render Raw Output (Issue #436)", async t => { let tr = new TemplateRender("hbs"); tr.engine.addHelpers({ "raw-helper": function(options) { return options.fn(); } }); let fn = await tr.getCompiledTemplate( "{{{{raw-helper}}}}{{bar}}{{{{/raw-helper}}}}" ); t.is(await fn({ name: "Zach" }), "{{bar}}"); }); test("Handlebars Render Raw Output (Issue #436 with if statement)", async t => { let tr = new TemplateRender("hbs"); tr.engine.addHelpers({ "raw-helper": function(options) { return options.fn(); } }); let fn = await tr.getCompiledTemplate( `{{{{raw-helper}}}}{{#if ready}}

Ready

{{/if}}{{{{/raw-helper}}}}` ); t.is( await fn({ name: "Zach" }), `{{#if ready}}

Ready

{{/if}}` ); });