hof-component-date
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -34,2 +34,10 @@ 'use strict'; | ||
const conditionalTranslate = (key, translate) => { | ||
let result = translate(key); | ||
if (result === key) { | ||
result = null; | ||
} | ||
return result; | ||
}; | ||
module.exports = (key, options) => { | ||
@@ -102,4 +110,6 @@ if (!key) { | ||
})); | ||
const legend = conditionalTranslate(`fields.${key}.legend`, req.translate); | ||
const hint = conditionalTranslate(`fields.${key}.hint`, req.translate); | ||
const error = req.form.errors && req.form.errors[key]; | ||
res.render(template, { key, error }, (err, html) => { | ||
res.render(template, { key, legend, hint, error }, (err, html) => { | ||
if (err) { | ||
@@ -106,0 +116,0 @@ next(err); |
{ | ||
"name": "hof-component-date", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A hof middleware for rendering and processing 3 part date fields", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -145,5 +145,5 @@ 'use strict'; | ||
it('calls res.render with template and key', () => { | ||
it('calls res.render with template, key, legend and hint. Where the latter two are null', () => { | ||
date.hooks['pre-render'](req, res, next); | ||
expect(res.render).to.have.been.calledWith(path.resolve(__dirname, '../templates/date.html'), { error: undefined, key: 'date-field' }); | ||
expect(res.render).to.have.been.calledWith(path.resolve(__dirname, '../templates/date.html'), { error: undefined, key: 'date-field', legend: null, hint: null }); | ||
}); | ||
@@ -157,5 +157,13 @@ | ||
date.hooks['pre-render'](req, res, next); | ||
expect(res.render).to.have.been.calledWith(path.resolve(__dirname, '../templates/date.html'), { error, key: 'date-field' }); | ||
expect(res.render).to.have.been.calledWith(path.resolve(__dirname, '../templates/date.html'), { error, key: 'date-field', legend: null, hint: null }); | ||
}); | ||
it('calls res.render with template, key, legend and hint with values NOT null', () => { | ||
req.translate = sinon.stub(); | ||
req.translate.withArgs('fields.date-field.legend').returns('you legend'); | ||
req.translate.withArgs('fields.date-field.hint').returns('some hint'); | ||
date.hooks['pre-render'](req, res, next); | ||
expect(res.render).to.have.been.calledWith(path.resolve(__dirname, '../templates/date.html'), { error: undefined, key: 'date-field', legend: 'you legend', hint: 'some hint' }); | ||
}); | ||
it('calls next with an error if res.render calls callback with err', () => { | ||
@@ -162,0 +170,0 @@ res.render.yields('err'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17021
375