diff --git a/package.json b/package.json index cb6b94cf8..519881194 100755 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "husky": "^7.0.4", "js-yaml": "^4.1.0", "lint-staged": "^11.2.5", + "liquidjs": "^9.28.4", "markdown-it-emoji": "^2.0.0", "nyc": "^15.1.0", "prettier": "^2.4.1", @@ -104,7 +105,6 @@ "hamljs": "^0.6.2", "handlebars": "^4.7.7", "is-glob": "^4.0.3", - "liquidjs": "9.25.1", "lodash": "^4.17.21", "luxon": "^2.0.2", "markdown-it": "^12.2.0", diff --git a/test/TemplateRenderLiquidTest.js b/test/TemplateRenderLiquidTest.js index 67e59a43e..8d8dcbbdb 100644 --- a/test/TemplateRenderLiquidTest.js +++ b/test/TemplateRenderLiquidTest.js @@ -2,9 +2,10 @@ const test = require("ava"); const TemplateRender = require("../src/TemplateRender"); const TemplateConfig = require("../src/TemplateConfig"); const EleventyExtensionMap = require("../src/EleventyExtensionMap"); +const { Drop } = require("liquidjs"); -function getNewTemplateRender(name, inputDir, userConfig = {}) { - let eleventyConfig = new TemplateConfig(); +function getNewTemplateRender(name, inputDir, userConfig = {}, templateConfig) { + let eleventyConfig = new TemplateConfig(templateConfig); for (let key in userConfig) { eleventyConfig.userConfig[key] = userConfig[key]; } @@ -102,6 +103,11 @@ test("Liquid Render Relative (current dir) Include", async (t) => { liquidOptions: { dynamicPartials: false, }, + }, + { + dir: { + includes: "relative-liquid", + }, } ); @@ -117,6 +123,11 @@ test("Liquid Render Relative (parent dir) Include", async (t) => { liquidOptions: { dynamicPartials: false, }, + }, + { + dir: { + includes: "relative-liquid", + }, } ); @@ -124,10 +135,16 @@ test("Liquid Render Relative (parent dir) Include", async (t) => { t.is(await fn(), "

TIME IS RELATIVE.

"); }); -test.skip("Liquid Render Relative (relative include should ignore _includes dir) Include", async (t) => { +test("Liquid Render Relative (relative include should ignore _includes dir) Include", async (t) => { let tr = getNewTemplateRender( "./test/stubs/does_not_exist_and_thats_ok.liquid", - "./test/stubs/" + "./test/stubs/", + {}, + { + dir: { + includes: ".", + }, + } ); let fn = await tr.getCompiledTemplate(`

{% include './included' %}

`); @@ -575,7 +592,13 @@ test("Liquid Render Include Subfolder Single quotes no extension dynamicPartials test("Liquid Render Include Subfolder Single quotes (relative include current dir) dynamicPartials true", async (t) => { let tr = getNewTemplateRender( "./test/stubs/does_not_exist_and_thats_ok.liquid", - "./test/stubs/" + "./test/stubs/", + {}, + { + dir: { + includes: "relative-liquid", + }, + } ); let fn = await tr.getCompiledTemplate( `

{% include './relative-liquid/dir/included' %}

` @@ -586,7 +609,13 @@ test("Liquid Render Include Subfolder Single quotes (relative include current di test("Liquid Render Include Subfolder Single quotes (relative include parent dir) dynamicPartials true", async (t) => { let tr = getNewTemplateRender( "./test/stubs/subfolder/does_not_exist_and_thats_ok.liquid", - "./test/stubs/" + "./test/stubs/", + {}, + { + dir: { + includes: "relative-liquid", + }, + } ); let fn = await tr.getCompiledTemplate( `

{% include '../relative-liquid/dir/included' %}

` @@ -704,17 +733,32 @@ test("Liquid Shortcode Multiple Args", async (t) => { ); }); -test.skip("Liquid Include Scope Leak", async (t) => { +test("Liquid Include Scope Leak", async (t) => { t.is( getNewTemplateRender("liquid", "./test/stubs/").getEngineName(), "liquid" ); - // This might be by design? + // This is by design, `include` assigns value to its parent scope, + // use `{% render %}` for separated, clean scope + // see: https://github.com/harttle/liquidjs/issues/404#issuecomment-955660149 let fn = await getNewTemplateRender( "liquid", "./test/stubs/" - ).getCompiledTemplate("

{% include scopeleak %}{{ test }}

"); + ).getCompiledTemplate("

{% include 'scopeleak' %}{{ test }}

"); + t.is(await fn({ test: 1 }), "

22

"); +}); + +test("Liquid Render Scope Leak", async (t) => { + t.is( + getNewTemplateRender("liquid", "./test/stubs/").getEngineName(), + "liquid" + ); + + let fn = await getNewTemplateRender( + "liquid", + "./test/stubs/" + ).getCompiledTemplate("

{% render 'scopeleak' %}{{ test }}

"); t.is(await fn({ test: 1 }), "

21

"); }); @@ -957,15 +1001,17 @@ test("Issue 600: Liquid Shortcode argument with underscores", async (t) => { ); }); -test.skip("Issue 611: Run a function", async (t) => { - // This works in Nunjucks +test("Issue 611: Run a function", async (t) => { + // function calls in Nunjucks can be replaced by custom Drops let tr = getNewTemplateRender("liquid", "./test/stubs/"); - + class CustomDrop extends Drop { + valueOf() { + return "alkdsjfksljaZach"; + } + } t.is( - await tr._testRender("{{ test() }}", { - test: function () { - return "alkdsjfksljaZach"; - }, + await tr._testRender("{{ test }}", { + test: new CustomDrop(), }), "alkdsjfksljaZach" );