Ghost/test/unit/helpers/date_spec.js
Matt Hanley b4140d4310
Updated default format for date helper to locale-based date string (#12733)
refs https://github.com/TryGhost/Casper/pull/741
closes https://github.com/TryGhost/Team/issues/524

- Use a local-based format as the default format as suggested in https://github.com/TryGhost/Casper/pull/741
- reworked the helper to be easier to read and follow the different use cases
- introduced setting and resetting locale in tests via settingsCache and themei18n
- updated tests to cover more cases e.g. passing a date, this.published_at and no date
- added validation for user inputted dates because they could literally be anything

Co-authored-by: Hannah Wolfe <erisds@gmail.com>
2021-03-05 13:35:31 +00:00

181 lines
5.1 KiB
JavaScript

const sinon = require('sinon');
const should = require('should');
// Stuff we are testing
const helpers = require('../../../core/frontend/helpers');
const proxy = require('../../../core/frontend/services/proxy');
const settingsCache = require('../../../core/server/services/settings/cache');
const moment = require('moment-timezone');
describe('{{date}} helper', function () {
afterEach(function () {
settingsCache.reset();
proxy.themeI18n._loadLocale();
});
it('creates properly formatted date strings', function () {
const testDates = [
'2013-12-31T11:28:58.593+02:00',
'2014-01-01T01:28:58.593+11:00',
'2014-02-20T01:28:58.593-04:00',
'2014-03-01T01:28:58.593+00:00'
];
const timezone = 'Europe/Dublin';
const format = 'MMM Do, YYYY';
const context = {
hash: {
format: format
},
data: {
site: {
timezone
}
}
};
let rendered;
testDates.forEach(function (d) {
rendered = helpers.date.call({published_at: d}, context);
should.exist(rendered);
String(rendered).should.equal(moment(d).tz(timezone).format(format));
rendered = helpers.date.call({}, d, context);
should.exist(rendered);
String(rendered).should.equal(moment(d).tz(timezone).format(format));
});
// No date falls back to now
rendered = helpers.date.call({}, context);
should.exist(rendered);
String(rendered).should.equal(moment().tz(timezone).format(format));
});
it('creates properly localised date strings', function () {
const testDates = [
'2013-12-31T23:58:58.593+02:00',
'2014-01-01T00:28:58.593+11:00',
'2014-11-20T01:28:58.593-04:00',
'2014-03-01T01:28:58.593+00:00'
];
const locales = [
'en',
'en-gb',
'de'
];
const timezone = 'Europe/Dublin';
const format = 'll';
const context = {
hash: {},
data: {
site: {
timezone
}
}
};
locales.forEach(function (l) {
settingsCache.set('lang', {value: l});
proxy.themeI18n._loadLocale();
let rendered;
testDates.forEach(function (d) {
rendered = helpers.date.call({published_at: d}, context);
should.exist(rendered);
String(rendered).should.equal(moment(d).tz(timezone).locale(l).format(format));
rendered = helpers.date.call({}, d, context);
should.exist(rendered);
String(rendered).should.equal(moment(d).tz(timezone).locale(l).format(format));
});
// No date falls back to now
rendered = helpers.date.call({}, context);
should.exist(rendered);
String(rendered).should.equal(moment().tz(timezone).locale(l).format(format));
});
});
it('creates properly formatted time ago date strings', function () {
const testDates = [
'2013-12-31T23:58:58.593+02:00',
'2014-01-01T00:28:58.593+11:00',
'2014-11-20T01:28:58.593-04:00',
'2014-03-01T01:28:58.593+00:00'
];
const timezone = 'Europe/Dublin';
const timeNow = moment().tz('Europe/Dublin');
const context = {
hash: {
timeago: true
},
data: {
site: {
timezone
}
}
};
let rendered;
testDates.forEach(function (d) {
rendered = helpers.date.call({published_at: d}, context);
should.exist(rendered);
String(rendered).should.equal(moment(d).tz(timezone).from(timeNow));
rendered = helpers.date.call({}, d, context);
should.exist(rendered);
String(rendered).should.equal(moment(d).tz(timezone).from(timeNow));
});
// No date falls back to now
rendered = helpers.date.call({}, context);
should.exist(rendered);
String(rendered).should.equal('a few seconds ago');
});
it('ignores an invalid date, defaulting to now', function () {
const timezone = 'Europe/Dublin';
const timeNow = moment().tz('Europe/Dublin');
const context = {
hash: {
timeago: true
},
data: {
site: {
timezone
}
}
};
let invalidDate = 'Fred';
let rendered;
rendered = helpers.date.call({published_at: invalidDate}, context);
should.exist(rendered);
String(rendered).should.equal('a few seconds ago');
rendered = helpers.date.call({}, invalidDate, context);
should.exist(rendered);
String(rendered).should.equal('a few seconds ago');
});
});