import test from "ava"; import TemplateRender from "../src/TemplateRender"; test("JS", t => { t.is(new TemplateRender("11ty.js").getEngineName(), "11ty.js"); t.is( new TemplateRender("./test/stubs/filename.11ty.js").getEngineName(), "11ty.js" ); }); test("JS Render a string (no data)", async t => { let fn = await new TemplateRender( "./test/stubs/string.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Bill" }), "

Zach

"); }); test("JS Render a promise (no data)", async t => { let fn = await new TemplateRender( "./test/stubs/promise.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Bill" }), "

Zach

"); }); test("JS Render a buffer (no data)", async t => { let fn = await new TemplateRender( "./test/stubs/buffer.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Bill" }), "

tést

"); }); test("JS Render a function", async t => { let fn = await new TemplateRender( "./test/stubs/function.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Zach" }), "

Zach

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

Bill

"); }); test("JS Render a function (arrow syntax)", async t => { let fn = await new TemplateRender( "./test/stubs/function-arrow.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Zach" }), "

Zach

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

Bill

"); }); test("JS Render a function, returns a Buffer", async t => { let fn = await new TemplateRender( "./test/stubs/function-buffer.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "tést" }), "

tést

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

Zach

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

Bill

"); }); test("JS Render a function (Markdown)", async t => { let tr = new TemplateRender("./test/stubs/function-markdown.11ty.js"); tr.setEngineOverride("11ty.js,md"); let fn = await tr.getCompiledTemplate(); t.is((await fn({ name: "Zach" })).trim(), "

Zach

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

Bill

"); }); test("JS Render a function (Collections)", async t => { let tr = new TemplateRender("./test/stubs/use-collection.11ty.js"); let fn = await tr.getCompiledTemplate(); t.is( (await fn({ collections: { post: [ { data: { title: "Testing" } }, { data: { title: "Testing2" } } ] } })).trim(), `` ); }); test("JS Render an async function", async t => { let fn = await new TemplateRender( "./test/stubs/function-async.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Zach" }), "

Zach

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

Bill

"); }); test("JS Render with a Class", async t => { let fn = await new TemplateRender( "./test/stubs/class.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Zach" }), "

ZachBillTed

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

BillBillTed

"); }); test("JS Render with a Class, returns a buffer", async t => { let fn = await new TemplateRender( "./test/stubs/class-buffer.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Zách" }), "

ZáchBillTed

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

ZachBillTed

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

BillBillTed

"); }); test("JS Render with a Class, async render", async t => { let fn = await new TemplateRender( "./test/stubs/class-async.11ty.js" ).getCompiledTemplate(); t.is(await fn({ name: "Zach" }), "

Zach

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

Bill

"); }); test("JS Render using Vue", async t => { let fn = await new TemplateRender( "./test/stubs/vue.11ty.js" ).getCompiledTemplate(); t.is( await fn({ name: "Zach" }), '

Hello Zach, this is a Vue template.

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

Hello Bill, this is a Vue template.

' ); }); test("JS Render using Vue (with a layout)", async t => { let fn = await new TemplateRender( "./test/stubs/vue-layout.11ty.js" ).getCompiledTemplate(); t.is( await fn({ name: "Zach" }), ` Test

Hello Zach, this is a Vue template.

` ); }); test("JS Render using ViperHTML", async t => { let fn = await new TemplateRender( "./test/stubs/viperhtml.11ty.js" ).getCompiledTemplate(); t.is( await fn({ name: "Zach", html: "Hi" }), `
This is a viper template, Zach Hi
` ); }); test("JS Render with a function", async t => { let tr = new TemplateRender("./test/stubs/function-filter.11ty.js"); tr.config = { javascriptFunctions: { upper: function(val) { return new String(val).toUpperCase(); } } }; let fn = await tr.getCompiledTemplate(); t.is(await fn({ name: "Zach" }), "

ZACHT9000

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

BILLT9000

"); }); test("JS Render with a function prototype", async t => { let tr = new TemplateRender("./test/stubs/function-prototype.11ty.js"); tr.config = { javascriptFunctions: { upper: function(val) { return new String(val).toUpperCase(); } } }; let fn = await tr.getCompiledTemplate(); t.is(await fn({ name: "Zach" }), "

ZACHBillT9001

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

BILLBillT9001

"); }); test("JS Class Render with a function", async t => { let tr = new TemplateRender("./test/stubs/class-filter.11ty.js"); tr.config = { javascriptFunctions: { upper: function(val) { return new String(val).toUpperCase(); } } }; let fn = await tr.getCompiledTemplate(); t.is(await fn({ name: "Zach" }), "

ZACHBillTed

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

BILLBillTed

"); }); test("JS Class Async Render with a function", async t => { let tr = new TemplateRender("./test/stubs/class-async-filter.11ty.js"); tr.config = { javascriptFunctions: { upper: function(val) { return new String(val).toUpperCase(); } } }; let fn = await tr.getCompiledTemplate(); // Overrides all names to Ted t.is(await fn({ name: "Zach" }), "

ZACHBillTed

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

BILLBillTed

"); });