From b525a777f690c9c9882071c7f2bd45e8014269a3 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Thu, 14 May 2020 02:17:55 -0300 Subject: [PATCH] test(includer): unit tests for includer function option --- test/ejs.js | 30 ++++++++++++++++++++++++++++++ test/fixtures/hello-template.html | 1 + 2 files changed, 31 insertions(+) create mode 100644 test/fixtures/hello-template.html diff --git a/test/ejs.js b/test/ejs.js index 5a7e7d4e..aee31350 100755 --- a/test/ejs.js +++ b/test/ejs.js @@ -999,6 +999,36 @@ suite('include()', function () { fixture('include.html')); }); + test('include ejs with custom includer function', function () { + var file = 'test/fixtures/include-root.ejs'; + var inc = function (url, prev) { + if (url.charAt(0) === '/') { + return { + filename: path.join(__dirname, 'fixtures', prev) + }; + } else { + return prev; + } + }; + assert.equal(ejs.render(fixture('include-root.ejs'), {pets: users}, {filename: file, delimiter: '@', includer: inc}), + fixture('include.html')); + }); + + test('include ejs with includer returning template', function () { + var file = 'test/fixtures/include-root.ejs'; + var inc = function (url, prev) { + if (prev === '/include.ejs') { + return { + template: '

Hello template!

\n' + }; + } else { + return prev; + } + }; + assert.equal(ejs.render(fixture('include-root.ejs'), {pets: users}, {filename: file, delimiter: '@', includer: inc}), + fixture('hello-template.html')); + }); + test('work when nested', function () { var file = 'test/fixtures/menu.ejs'; assert.equal(ejs.render(fixture('menu.ejs'), {pets: users}, {filename: file}), diff --git a/test/fixtures/hello-template.html b/test/fixtures/hello-template.html new file mode 100644 index 00000000..c0de8a52 --- /dev/null +++ b/test/fixtures/hello-template.html @@ -0,0 +1 @@ +

Hello template!