hmpo-template-mixins
Advanced tools
Comparing version 0.1.9 to 0.1.10
@@ -26,4 +26,2 @@ 'use strict'; | ||
// This code probably does not need this as the only time it is used | ||
// only a single key is passed in. | ||
var i18nLookup = require('./i18n-property')(t); | ||
@@ -273,3 +271,4 @@ | ||
txt = txt || ''; | ||
return Hogan.compile(txt).render(this).toLowerCase().replace(' ', '-'); | ||
var value = Hogan.compile(txt).render(this); | ||
return value.trim().toLowerCase().replace(/\s+/g, '-'); | ||
}; | ||
@@ -276,0 +275,0 @@ }; |
{ | ||
"name": "hmpo-template-mixins", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "A middleware that exposes a series of Mustache mixins on res.locals to ease usage of forms, translations, and some general needs.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,12 +19,4 @@ var mixins = require('../lib/template-mixins'); | ||
next = sinon.stub(); | ||
render = sinon.stub(); | ||
sinon.stub(Hogan, 'compile').returns({ | ||
render: render | ||
}); | ||
}); | ||
afterEach(function () { | ||
Hogan.compile.restore(); | ||
}); | ||
it('returns a middleware', function () { | ||
@@ -40,158 +32,173 @@ mixins().should.be.a('function'); | ||
describe('input-text', function () { | ||
describe('with stubbed Hogan', function () { | ||
beforeEach(function () { | ||
middleware = mixins(translate, {}); | ||
render = sinon.stub(); | ||
sinon.stub(Hogan, 'compile').returns({ | ||
render: render | ||
}); | ||
}); | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['input-text'].should.be.a('function'); | ||
afterEach(function () { | ||
Hogan.compile.restore(); | ||
}); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['input-text']().should.be.a('function'); | ||
}); | ||
describe('input-text', function () { | ||
it('looks up field label', function () { | ||
middleware(req, res, next); | ||
res.locals['input-text']().call(res.locals, 'field-name'); | ||
render.should.have.been.calledWith(sinon.match({ | ||
label: 'fields.field-name.label' | ||
})); | ||
}); | ||
beforeEach(function () { | ||
middleware = mixins(translate, {}); | ||
}); | ||
it('prefixes translation lookup with namespace if provided', function () { | ||
middleware = mixins(translate, {}, { sharedTranslationsKey: 'name.space' }); | ||
middleware(req, res, next); | ||
res.locals['input-text']().call(res.locals, 'field-name'); | ||
render.should.have.been.calledWith(sinon.match({ | ||
label: 'name.space.fields.field-name.label' | ||
})); | ||
}); | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['input-text'].should.be.a('function'); | ||
}); | ||
}); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['input-text']().should.be.a('function'); | ||
}); | ||
describe('input-date', function () { | ||
it('looks up field label', function () { | ||
middleware(req, res, next); | ||
res.locals['input-text']().call(res.locals, 'field-name'); | ||
render.should.have.been.calledWith(sinon.match({ | ||
label: 'fields.field-name.label' | ||
})); | ||
}); | ||
beforeEach(function () { | ||
middleware = mixins(translate, {}); | ||
}); | ||
it('prefixes translation lookup with namespace if provided', function () { | ||
middleware = mixins(translate, {}, { sharedTranslationsKey: 'name.space' }); | ||
middleware(req, res, next); | ||
res.locals['input-text']().call(res.locals, 'field-name'); | ||
render.should.have.been.calledWith(sinon.match({ | ||
label: 'name.space.fields.field-name.label' | ||
})); | ||
}); | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['input-date'].should.be.a('function'); | ||
}); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['input-date']().should.be.a('function'); | ||
}); | ||
describe('input-date', function () { | ||
it('renders thrice if the field is not marked as inexact', function () { | ||
middleware(req, res, next); | ||
res.locals['input-date']().call(res.locals, 'field-name'); | ||
render.should.have.been.calledThrice; | ||
}); | ||
beforeEach(function () { | ||
middleware = mixins(translate, {}); | ||
}); | ||
it('renders twice if the field is marked as inexact', function () { | ||
var middlewareWithFieldNameMarkedAsInexact = mixins(translate, { | ||
'field-name': { | ||
'inexact': true | ||
} | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['input-date'].should.be.a('function'); | ||
}); | ||
middlewareWithFieldNameMarkedAsInexact(req, res, next); | ||
res.locals['input-date']().call(res.locals, 'field-name'); | ||
render.should.have.been.calledTwice; | ||
}); | ||
it('looks up field label', function () { | ||
middleware(req, res, next); | ||
res.locals['input-date']().call(res.locals, 'field-name'); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['input-date']().should.be.a('function'); | ||
}); | ||
render.called; | ||
it('renders thrice if the field is not marked as inexact', function () { | ||
middleware(req, res, next); | ||
res.locals['input-date']().call(res.locals, 'field-name'); | ||
render.should.have.been.calledThrice; | ||
}); | ||
var dayCall = render.getCall(0), | ||
monthCall = render.getCall(1), | ||
yearCall = render.getCall(2); | ||
it('renders twice if the field is marked as inexact', function () { | ||
var middlewareWithFieldNameMarkedAsInexact = mixins(translate, { | ||
'field-name': { | ||
'inexact': true | ||
} | ||
}); | ||
middlewareWithFieldNameMarkedAsInexact(req, res, next); | ||
res.locals['input-date']().call(res.locals, 'field-name'); | ||
render.should.have.been.calledTwice; | ||
}); | ||
dayCall.should.have.been.calledWith(sinon.match({ | ||
label: 'fields.field-name-day.label' | ||
})); | ||
it('looks up field label', function () { | ||
middleware(req, res, next); | ||
res.locals['input-date']().call(res.locals, 'field-name'); | ||
monthCall.should.have.been.calledWith(sinon.match({ | ||
label: 'fields.field-name-month.label' | ||
})); | ||
render.called; | ||
yearCall.should.have.been.calledWith(sinon.match({ | ||
label: 'fields.field-name-year.label' | ||
})); | ||
}); | ||
var dayCall = render.getCall(0), | ||
monthCall = render.getCall(1), | ||
yearCall = render.getCall(2); | ||
it('prefixes translation lookup with namespace if provided', function () { | ||
middleware = mixins(translate, {}, { sharedTranslationsKey: 'name.space' }); | ||
middleware(req, res, next); | ||
res.locals['input-date']().call(res.locals, 'field-name'); | ||
dayCall.should.have.been.calledWith(sinon.match({ | ||
label: 'fields.field-name-day.label' | ||
})); | ||
render.called; | ||
monthCall.should.have.been.calledWith(sinon.match({ | ||
label: 'fields.field-name-month.label' | ||
})); | ||
var dayCall = render.getCall(0), | ||
monthCall = render.getCall(1), | ||
yearCall = render.getCall(2); | ||
yearCall.should.have.been.calledWith(sinon.match({ | ||
label: 'fields.field-name-year.label' | ||
})); | ||
}); | ||
dayCall.should.have.been.calledWith(sinon.match({ | ||
label: 'name.space.fields.field-name-day.label' | ||
})); | ||
it('prefixes translation lookup with namespace if provided', function () { | ||
middleware = mixins(translate, {}, { sharedTranslationsKey: 'name.space' }); | ||
middleware(req, res, next); | ||
res.locals['input-date']().call(res.locals, 'field-name'); | ||
monthCall.should.have.been.calledWith(sinon.match({ | ||
label: 'name.space.fields.field-name-month.label' | ||
})); | ||
render.called; | ||
yearCall.should.have.been.calledWith(sinon.match({ | ||
label: 'name.space.fields.field-name-year.label' | ||
})); | ||
}); | ||
var dayCall = render.getCall(0), | ||
monthCall = render.getCall(1), | ||
yearCall = render.getCall(2); | ||
}); | ||
dayCall.should.have.been.calledWith(sinon.match({ | ||
label: 'name.space.fields.field-name-day.label' | ||
})); | ||
describe('input-submit', function () { | ||
monthCall.should.have.been.calledWith(sinon.match({ | ||
label: 'name.space.fields.field-name-month.label' | ||
})); | ||
beforeEach(function () { | ||
middleware = mixins(translate, {}); | ||
}); | ||
yearCall.should.have.been.calledWith(sinon.match({ | ||
label: 'name.space.fields.field-name-year.label' | ||
})); | ||
}); | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['input-submit'].should.be.a('function'); | ||
}); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['input-submit']().should.be.a('function'); | ||
}); | ||
describe('input-submit', function () { | ||
it('looks up button value with default key of "next"', function () { | ||
middleware(req, res, next); | ||
res.locals['input-submit']().call(res.locals); | ||
render.should.have.been.calledWith(sinon.match({ | ||
value: 'buttons.next' | ||
})); | ||
}); | ||
beforeEach(function () { | ||
middleware = mixins(translate, {}); | ||
}); | ||
it('looks up button value with key if provided', function () { | ||
middleware(req, res, next); | ||
res.locals['input-submit']().call(res.locals, 'button-id'); | ||
render.should.have.been.calledWith(sinon.match({ | ||
value: 'buttons.button-id' | ||
})); | ||
}); | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['input-submit'].should.be.a('function'); | ||
}); | ||
it('prefixes translation lookup with namespace if provided', function () { | ||
middleware = mixins(translate, {}, { sharedTranslationsKey: 'name.space' }); | ||
middleware(req, res, next); | ||
res.locals['input-submit']().call(res.locals, 'button-id'); | ||
render.should.have.been.calledWith(sinon.match({ | ||
value: 'name.space.buttons.button-id' | ||
})); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['input-submit']().should.be.a('function'); | ||
}); | ||
it('looks up button value with default key of "next"', function () { | ||
middleware(req, res, next); | ||
res.locals['input-submit']().call(res.locals); | ||
render.should.have.been.calledWith(sinon.match({ | ||
value: 'buttons.next' | ||
})); | ||
}); | ||
it('looks up button value with key if provided', function () { | ||
middleware(req, res, next); | ||
res.locals['input-submit']().call(res.locals, 'button-id'); | ||
render.should.have.been.calledWith(sinon.match({ | ||
value: 'buttons.button-id' | ||
})); | ||
}); | ||
it('prefixes translation lookup with namespace if provided', function () { | ||
middleware = mixins(translate, {}, { sharedTranslationsKey: 'name.space' }); | ||
middleware(req, res, next); | ||
res.locals['input-submit']().call(res.locals, 'button-id'); | ||
render.should.have.been.calledWith(sinon.match({ | ||
value: 'name.space.buttons.button-id' | ||
})); | ||
}); | ||
}); | ||
@@ -201,26 +208,59 @@ | ||
describe('date', function () { | ||
describe('without stubbed Hogan', function () { | ||
beforeEach(function () { | ||
middleware = mixins(translate, {}); | ||
}); | ||
describe('date', function () { | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['date'].should.be.a('function'); | ||
}); | ||
beforeEach(function () { | ||
middleware = mixins(translate, {}); | ||
}); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['date']().should.be.a('function'); | ||
}); | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['date'].should.be.a('function'); | ||
}); | ||
it('formats a date', function () { | ||
middleware(req, res, next); | ||
res.locals['date']().call(res.locals, '2015-03-26').should.equal('26 March 2015'); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['date']().should.be.a('function'); | ||
}); | ||
it('formats a date', function () { | ||
middleware(req, res, next); | ||
res.locals['date']().call(res.locals, '2015-03-26').should.equal('26 March 2015'); | ||
}); | ||
it('applys a date format if specified', function () { | ||
middleware(req, res, next); | ||
res.locals['date']().call(res.locals, '2015-03|MMMM YYYY').should.equal('March 2015'); | ||
}); | ||
}); | ||
it('applys a date format if specified', function () { | ||
middleware(req, res, next); | ||
res.locals['date']().call(res.locals, '2015-03|MMMM YYYY').should.equal('March 2015'); | ||
describe('hyphenate', function () { | ||
beforeEach(function () { | ||
Hogan = require('hogan.js'); | ||
middleware = mixins(translate, {}); | ||
}); | ||
it('adds a function to res.locals', function () { | ||
middleware(req, res, next); | ||
res.locals['hyphenate'].should.be.a('function'); | ||
}); | ||
it('returns a function', function () { | ||
middleware(req, res, next); | ||
res.locals['hyphenate']().should.be.a('function'); | ||
}); | ||
it('hyphenates a string with a single whitespace character', function () { | ||
middleware(req, res, next); | ||
res.locals['hyphenate']().call(res.locals, 'apple blackberry').should.equal('apple-blackberry'); | ||
}); | ||
it('hyphenates a string with multiple whitespace characters', function () { | ||
middleware(req, res, next); | ||
res.locals['hyphenate']().call(res.locals, 'apple blackberry cherry').should.equal('apple-blackberry-cherry'); | ||
}); | ||
}); | ||
@@ -227,0 +267,0 @@ |
28150
500