2018-07-19 19:05:42 +00:00
|
|
|
const date = require('../../../src/website/theme-base/helpers/date')
|
2017-11-27 18:56:06 +00:00
|
|
|
const handlebars = require('handlebars')
|
2017-11-29 10:11:12 +00:00
|
|
|
const moment = require('moment')
|
2017-11-27 18:56:06 +00:00
|
|
|
const should = require('should/as-function')
|
|
|
|
|
|
|
|
describe('Handlebars helpers: date', () => {
|
|
|
|
handlebars.registerHelper('date', date)
|
|
|
|
|
2017-11-29 10:11:12 +00:00
|
|
|
it('renders a date as DD MMM YYYY by default', () => {
|
2017-11-27 18:56:06 +00:00
|
|
|
const template = handlebars.compile(`<p>{{date taken}}</p>`)
|
2018-12-11 22:11:03 +00:00
|
|
|
const res = template({ taken: new Date(2017, 10, 27) }) // month is 0-based
|
2017-11-27 18:56:06 +00:00
|
|
|
should(res).eql('<p>27 Nov 2017</p>')
|
|
|
|
})
|
2017-11-29 10:11:12 +00:00
|
|
|
|
|
|
|
it('renders a date with a custom format', () => {
|
|
|
|
const template = handlebars.compile(`<p>{{date taken "MMMM YYYY"}}</p>`)
|
2018-12-11 22:11:03 +00:00
|
|
|
const res = template({ taken: new Date(2017, 10, 27) }) // month is 0-based
|
2017-11-29 10:11:12 +00:00
|
|
|
should(res).eql('<p>November 2017</p>')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('renders a date as <time ago>', () => {
|
|
|
|
const template = handlebars.compile(`<p>{{date taken "ago"}}</p>`)
|
2018-12-11 22:11:03 +00:00
|
|
|
const data = { taken: new Date(2017, 10, 27) } // month is 0-based
|
2017-11-29 10:11:12 +00:00
|
|
|
const res = template(data)
|
|
|
|
const expected = moment(data.taken).fromNow()
|
|
|
|
should(res).match(/<p>(.*) ago<\/p>/)
|
|
|
|
should(res).eql(`<p>${expected}</p>`)
|
|
|
|
})
|
2017-11-27 18:56:06 +00:00
|
|
|
})
|