'use strict'; require('mocha'); require('should'); var path = require('path'); var Base = require('base'); var assert = require('assert'); var consolidate = require('consolidate'); var handlebars = require('engine-handlebars'); var matter = require('parser-front-matter'); var swig = consolidate.swig; require('swig'); var support = require('./support'); var App = support.resolve(); var helpers = App._.proto.helpers; var init = App._.proto.init; var app; describe('helpers', function() { describe('constructor', function() { it('should create an instance of Helpers:', function() { app = new App(); assert(app instanceof App); }); }); describe('prototype methods', function() { beforeEach(function() { app = new App(); }); it('should expose `helper`', function() { assert(typeof app.helper === 'function'); }); it('should expose `asyncHelper`', function() { assert(typeof app.asyncHelper === 'function'); }); }); describe('instance', function() { it('should prime _', function() { function Foo() { Base.call(this); init(this); } Base.extend(Foo); var foo = new Foo(); helpers(foo); assert(typeof foo._ === 'object'); }); }); describe('helpers', function() { beforeEach(function() { app = new App(); }); it('should add a sync helper to the `sync` object:', function() { app.helper('one', function() {}); assert(typeof app._.helpers.sync.one === 'function'); }); it('should load a glob of sync helper functions:', function() { app.helpers('test/fixtures/helpers/[a-c].js'); assert(typeof app._.helpers.sync.c === 'function'); assert(typeof app._.helpers.sync.b === 'function'); assert(typeof app._.helpers.sync.a === 'function'); }); it('should fail gracefully on bad globs:', function(cb) { try { app.helpers('test/fixtures/helpers/*.foo'); cb(); } catch (err) { cb(new Error('should not throw an error.')); } }); it('should add a glob of sync helper objects:', function() { app.helpers('test/fixtures/helpers/!([a-c]).js'); assert(typeof app._.helpers.sync.one === 'function'); assert(typeof app._.helpers.sync.two === 'function'); assert(typeof app._.helpers.sync.three === 'function'); }); it('should add a glob with mixed helper objects and functions:', function() { app.helpers('test/fixtures/helpers/*.js'); assert(typeof app._.helpers.sync.a === 'function'); assert(typeof app._.helpers.sync.b === 'function'); assert(typeof app._.helpers.sync.c === 'function'); assert(typeof app._.helpers.sync.one === 'function'); assert(typeof app._.helpers.sync.two === 'function'); assert(typeof app._.helpers.sync.three === 'function'); }); it('should add an object of sync helpers to the `sync` object:', function() { app.helpers({ x: function() {}, y: function() {}, z: function() {} }); assert(typeof app._.helpers.sync.x === 'function'); assert(typeof app._.helpers.sync.y === 'function'); assert(typeof app._.helpers.sync.z === 'function'); }); it('should add a helper "group":', function() { app.helperGroup('foo', { x: function() {}, y: function() {}, z: function() {} }); assert(typeof app._.helpers.sync.foo.x === 'function'); assert(typeof app._.helpers.sync.foo.y === 'function'); assert(typeof app._.helpers.sync.foo.z === 'function'); }); }); describe('async helpers', function() { beforeEach(function() { app = new App(); }); it('should add an async helper to the `async` object:', function() { app.asyncHelper('two', function() {}); assert(typeof app._.helpers.async.two === 'function'); }); it('should load a glob of async helper functions:', function() { app.asyncHelpers('test/fixtures/helpers/[a-c].js'); assert(typeof app._.helpers.async.a === 'function'); assert(typeof app._.helpers.async.b === 'function'); assert(typeof app._.helpers.async.c === 'function'); }); it('should add a glob of async helper objects:', function() { app.asyncHelpers('test/fixtures/helpers/!([a-c]).js'); assert(typeof app._.helpers.async.one === 'function'); assert(typeof app._.helpers.async.two === 'function'); assert(typeof app._.helpers.async.three === 'function'); }); it('should fail gracefully on bad globs:', function(cb) { try { app.asyncHelpers('test/fixtures/helpers/*.foo'); cb(); } catch (err) { cb(new Error('should not throw an error.')); } }); it('should add a glob with mixed helper objects and functions:', function() { app.asyncHelpers('test/fixtures/helpers/*.js'); assert(typeof app._.helpers.async.a === 'function'); assert(typeof app._.helpers.async.b === 'function'); assert(typeof app._.helpers.async.c === 'function'); assert(typeof app._.helpers.async.one === 'function'); assert(typeof app._.helpers.async.two === 'function'); assert(typeof app._.helpers.async.three === 'function'); }); it('should add an object of async helpers to the `async` object:', function() { app.asyncHelpers({ x: function() {}, y: function() {}, z: function() {} }); assert(typeof app._.helpers.async.x === 'function'); assert(typeof app._.helpers.async.y === 'function'); assert(typeof app._.helpers.async.z === 'function'); }); it('should add an async helper "group":', function() { app.helperGroup('foo', { x: function() {}, y: function() {}, z: function() {} }, true); assert(typeof app._.helpers.async.foo.x === 'function'); assert(typeof app._.helpers.async.foo.y === 'function'); assert(typeof app._.helpers.async.foo.z === 'function'); }); }); }); describe('sync helpers', function() { beforeEach(function() { app = new App(); app.engine('tmpl', require('engine-base')); app.create('page'); }); it('should register a helper:', function() { app.helper('a', function() {}); app.helper('b', function() {}); assert(app._.helpers.sync.hasOwnProperty('a')); assert(app._.helpers.sync.hasOwnProperty('b')); }); it('should use a helper:', function(cb) { app.pages('a.tmpl', {path: 'a.tmpl', content: '<%= upper(a) %>', locals: {a: 'bbb'}}); app.helper('upper', function(str) { return str.toUpperCase(); }); var page = app.pages.getView('a.tmpl'); app.render(page, function(err, view) { if (err) return cb(err); assert.equal(typeof view.contents.toString(), 'string'); assert.equal(view.contents.toString(), 'BBB'); cb(); }); }); it('should use a namespaced helper:', function(cb) { app.pages('a.tmpl', {path: 'a.tmpl', content: '<%= foo.upper(a) %>', locals: {a: 'bbb'}}); app.helperGroup('foo', { upper: function(str) { return str.toUpperCase(); } }); // console.log(app._.helpers) var page = app.pages.getView('a.tmpl'); app.render(page, function(err, view) { if (err) return cb(err); assert.equal(typeof view.contents.toString(), 'string'); assert.equal(view.contents.toString(), 'BBB'); cb(); }); }); }); describe('async helpers', function() { beforeEach(function() { app = new App(); app.engine('tmpl', require('engine-base')); app.create('page'); }); it('should register an async helper:', function() { app.asyncHelper('a', function() {}); app.asyncHelper('b', function() {}); app._.helpers.async.should.have.property('a'); app._.helpers.async.should.have.property('b'); }); it('should use an async helper:', function(cb) { app.pages('a.tmpl', {path: 'a.tmpl', content: '<%= lower(a) %>', locals: {a: 'BBB'}}); app.asyncHelper('lower', function(str, next) { if (typeof next !== 'function') return str; next(null, str.toLowerCase()); }); var page = app.pages.getView('a.tmpl'); app.render(page, function(err, view) { if (err) return cb(err); assert.equal(typeof view.content, 'string'); assert.equal(view.content, 'bbb'); cb(); }); }); }); describe('built-in helpers:', function() { describe('automatically generated helpers for default view types:', function() { beforeEach(function() { app = new App({rethrow: false}); app.engine('md', require('engine-base')); app.engine('tmpl', require('engine-base')); app.create('partials', { viewType: 'partial' }); app.create('pages'); // parse front matter app.onLoad(/./, function(view, next) { matter.parse(view, next); }); }); it('should expose front matter to the `partial` helper.', function(cb) { app.partial('a.md', {content: '---\nname: "AAA"\n---\n<%= name %>', locals: {name: 'BBB'}}); app.page('b.md', {path: 'b.md', content: 'foo <%= partial("a.md") %> bar'}); app.render('b.md', function(err, res) { if (err) return cb(err); res.content.should.equal('foo AAA bar'); cb(); }); }); it('should use helper locals.', function(cb) { app.partial('abc.md', {content: '<%= name %>', locals: {name: 'BBB'}}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md", { name: "CCC" }) %> bar'}); app.render('xyz.md', {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo CCC bar'); cb(); }); }); it('should use front matter data.', function(cb) { app.partial('abc.md', {content: '---\nname: "AAA"\n---\n<%= name %>'}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md") %> bar'}); app.render('xyz.md', {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo AAA bar'); cb(); }); }); it('should prefer helper locals over front-matter', function(cb) { app.partial('abc.md', {content: '---\nname: "AAA"\n---\n<%= name %>', locals: {name: 'BBB'}}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md", { name: "CCC" }) %> bar'}); app.render('xyz.md', {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo CCC bar'); cb(); }); }); it('should use partial locals:', function(cb) { app.partial('abc.md', {content: '<%= name %>', locals: {name: 'EEE'}}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md") %> bar'}) .render({name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo EEE bar'); cb(); }); }); it('should use locals from the `view.render` method:', function(cb) { app.partial('abc.md', {content: '<%= name %>', locals: {name: 'EEE'}}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md") %> bar'}) .render({name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo EEE bar'); cb(); }); }); it('should use locals from the `app.render` method:', function(cb) { app.partial('abc.md', {content: '<%= name %>'}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md") %> bar'}); app.render('xyz.md', {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo DDD bar'); cb(); }); }); it('should use a `helperContext` function from app.options', function(cb) { app.option('helperContext', function(view, locals) { return { name: 'blah' }; }); app.partial('abc.md', {content: '<%= name %>'}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md") %> bar'}); app.render('xyz.md', {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo blah bar'); cb(); }); }); it('should return an empty string when the partial is missing.', function(cb) { app.partial('abc.md', {content: '---\nname: "AAA"\n---\n<%= name %>', locals: {name: 'BBB'}}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("def.md", { name: "CCC" }) %> bar'}); app.render('xyz.md', {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo bar'); cb(); }); }); }); describe('helper context:', function() { beforeEach(function() { app = new App({rethrow: false}); app.engine(['tmpl', 'md'], require('engine-base')); app.create('partial', { viewType: 'partial' }); app.create('page'); // parse front matter app.onLoad(/./, function(view, next) { matter.parse(view, next); }); }); it('should prefer helper locals over view locals.', function(cb) { app.partial('abc.md', {content: '<%= name %>', name: 'BBB'}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md", { name: "CCC" }) %> bar'}); app.render('xyz.md', {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo CCC bar'); cb(); }); }); it('should give preference to view locals over render locals.', function(cb) { app.partial('abc.md', {content: '<%= name %>', locals: {name: 'BBB'}}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md") %> bar'}); var page = app.pages.getView('xyz.md'); app.render(page, {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo BBB bar'); cb(); }); }); it('should use render locals when other locals are not defined.', function(cb) { app.partial('abc.md', {content: '<%= name %>'}); app.page('xyz.md', {path: 'xyz.md', content: 'foo <%= partial("abc.md") %> bar'}); app.render('xyz.md', {name: 'DDD'}, function(err, res) { if (err) return cb(err); res.content.should.equal('foo DDD bar'); cb(); }); }); }); describe('user-defined engines:', function() { beforeEach(function() { app = new App({rethrow: false}); app.create('partial', { viewType: 'partial' }); app.create('page'); // parse front matter app.onLoad(/./, function(view, next) { matter.parse(view, next); }); }); it('should use the `partial` helper with handlebars.', function(cb) { app.engine(['tmpl', 'md'], require('engine-base')); app.engine('hbs', handlebars); app.partial('title.hbs', {content: '