2021-03-05 16:35:31 +03:00
|
|
|
|
const sinon = require('sinon');
|
2020-04-29 18:44:27 +03:00
|
|
|
|
const should = require('should');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
|
// Stuff we are testing
|
2021-10-06 12:52:46 +03:00
|
|
|
|
const date = require('../../../../core/frontend/helpers/date');
|
2020-04-29 18:44:27 +03:00
|
|
|
|
|
|
|
|
|
const moment = require('moment-timezone');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
|
|
describe('{{date}} helper', function () {
|
|
|
|
|
it('creates properly formatted date strings', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
|
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'
|
|
|
|
|
];
|
|
|
|
|
|
2021-03-05 16:35:31 +03:00
|
|
|
|
const timezone = 'Europe/Dublin';
|
2020-04-29 18:44:27 +03:00
|
|
|
|
const format = 'MMM Do, YYYY';
|
|
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
|
hash: {
|
|
|
|
|
format: format
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
site: {
|
2021-03-05 16:35:31 +03:00
|
|
|
|
timezone
|
2014-10-10 18:54:07 +04:00
|
|
|
|
}
|
2020-04-29 18:44:27 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
2021-03-05 16:35:31 +03:00
|
|
|
|
let rendered;
|
|
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
|
testDates.forEach(function (d) {
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({published_at: d}, context);
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
|
|
should.exist(rendered);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
String(rendered).should.equal(moment(d).tz(timezone).format(format));
|
|
|
|
|
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({}, d, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
|
|
|
|
|
should.exist(rendered);
|
|
|
|
|
String(rendered).should.equal(moment(d).tz(timezone).format(format));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// No date falls back to now
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({}, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
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';
|
|
|
|
|
|
2021-05-04 18:49:35 +03:00
|
|
|
|
locales.forEach(function (locale) {
|
2021-03-05 16:35:31 +03:00
|
|
|
|
let rendered;
|
|
|
|
|
|
2021-05-04 18:49:35 +03:00
|
|
|
|
const context = {
|
|
|
|
|
hash: {},
|
|
|
|
|
data: {
|
|
|
|
|
site: {
|
|
|
|
|
timezone,
|
|
|
|
|
locale
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-05 16:35:31 +03:00
|
|
|
|
testDates.forEach(function (d) {
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({published_at: d}, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
|
|
|
|
|
should.exist(rendered);
|
2021-05-04 18:49:35 +03:00
|
|
|
|
String(rendered).should.equal(moment(d).tz(timezone).locale(locale).format(format));
|
2021-03-05 16:35:31 +03:00
|
|
|
|
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({}, d, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
|
|
|
|
|
should.exist(rendered);
|
2021-05-04 18:49:35 +03:00
|
|
|
|
String(rendered).should.equal(moment(d).tz(timezone).locale(locale).format(format));
|
2021-03-05 16:35:31 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// No date falls back to now
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({}, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
should.exist(rendered);
|
2021-05-04 18:49:35 +03:00
|
|
|
|
String(rendered).should.equal(moment().tz(timezone).locale(locale).format(format));
|
2014-10-10 18:54:07 +04:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('creates properly formatted time ago date strings', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
|
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'
|
|
|
|
|
];
|
|
|
|
|
|
2021-03-05 16:35:31 +03:00
|
|
|
|
const timezone = 'Europe/Dublin';
|
2020-04-29 18:44:27 +03:00
|
|
|
|
const timeNow = moment().tz('Europe/Dublin');
|
|
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
|
hash: {
|
|
|
|
|
timeago: true
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
site: {
|
2021-03-05 16:35:31 +03:00
|
|
|
|
timezone
|
2014-10-10 18:54:07 +04:00
|
|
|
|
}
|
2020-04-29 18:44:27 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
2021-03-05 16:35:31 +03:00
|
|
|
|
let rendered;
|
|
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
|
testDates.forEach(function (d) {
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({published_at: d}, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
|
|
|
|
|
should.exist(rendered);
|
|
|
|
|
String(rendered).should.equal(moment(d).tz(timezone).from(timeNow));
|
|
|
|
|
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({}, d, context);
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
|
|
should.exist(rendered);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
String(rendered).should.equal(moment(d).tz(timezone).from(timeNow));
|
2014-10-10 18:54:07 +04:00
|
|
|
|
});
|
2021-03-05 16:35:31 +03:00
|
|
|
|
|
|
|
|
|
// No date falls back to now
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({}, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
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;
|
|
|
|
|
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({published_at: invalidDate}, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
|
|
|
|
|
should.exist(rendered);
|
|
|
|
|
String(rendered).should.equal('a few seconds ago');
|
|
|
|
|
|
2021-10-04 18:30:54 +03:00
|
|
|
|
rendered = date.call({}, invalidDate, context);
|
2021-03-05 16:35:31 +03:00
|
|
|
|
|
|
|
|
|
should.exist(rendered);
|
|
|
|
|
String(rendered).should.equal('a few seconds ago');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
});
|
2022-01-19 17:43:53 +03:00
|
|
|
|
|
|
|
|
|
it('allows user to override the site\'s locale and timezone', function () {
|
|
|
|
|
const context = {
|
|
|
|
|
hash: {
|
|
|
|
|
format: 'ddd, DD MMM YYYY HH:mm:ss ZZ' // RFC 822
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
site: {
|
|
|
|
|
timezone: 'Asia/Tokyo',
|
|
|
|
|
locale: 'ja-jp'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Using the site locale by default, none specified in hash
|
|
|
|
|
const published_at = '2013-12-31T23:58:58.593+02:00';
|
|
|
|
|
String(date.call({published_at}, context)).should.equal('水, 01 1月 2014 06:58:58 +0900');
|
|
|
|
|
|
|
|
|
|
// Overriding the site locale and timezone in hash
|
|
|
|
|
context.hash.timezone = 'Europe/Paris';
|
|
|
|
|
context.hash.locale = 'fr-fr';
|
|
|
|
|
String(date.call({published_at}, context)).should.equal('mar., 31 déc. 2013 22:58:58 +0100');
|
|
|
|
|
|
|
|
|
|
context.hash.timezone = 'Europe/Moscow';
|
|
|
|
|
context.hash.locale = 'ru-ru';
|
|
|
|
|
String(date.call({published_at}, context)).should.equal('ср, 01 янв. 2014 01:58:58 +0400');
|
|
|
|
|
|
|
|
|
|
context.hash.locale = 'en-us';
|
|
|
|
|
String(date.call({published_at}, context)).should.equal('Wed, 01 Jan 2014 01:58:58 +0400');
|
|
|
|
|
});
|
2014-10-10 18:54:07 +04:00
|
|
|
|
});
|