UNPKG

javascript-time-ago

Version:
260 lines (220 loc) 10.9 kB
"use strict"; var _twitter = _interopRequireDefault(require("./twitter")); var _TimeAgo = _interopRequireDefault(require("../TimeAgo")); var _steps = require("../steps"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } describe('style/twitter', function () { it('should fallback from "mini" to "narrow"', function () { var timeAgo = new _TimeAgo.default('it'); timeAgo.format(Date.now() - 3 * _steps.hour * 1000, 'twitter').should.equal('3 h fa'); }); it('should format Twitter style relative time (English) (round: "round")', function () { var timeAgo = new _TimeAgo.default('en'); var formatDatePastBy = function formatDatePastBy(secondsPassed) { return timeAgo.format(-secondsPassed * 1000, _objectSpread({ now: 0 }, _twitter.default)); }; formatDatePastBy(0.49).should.equal('0s'); formatDatePastBy(0.5).should.equal('1s'); formatDatePastBy(59.49).should.equal('59s'); formatDatePastBy(59.5).should.equal('1m'); formatDatePastBy(1.49 * _steps.minute).should.equal('1m'); formatDatePastBy(1.5 * _steps.minute).should.equal('2m'); // … formatDatePastBy(59.49 * _steps.minute).should.equal('59m'); formatDatePastBy(59.5 * _steps.minute).should.equal('1h'); formatDatePastBy(1.49 * _steps.hour).should.equal('1h'); formatDatePastBy(1.5 * _steps.hour).should.equal('2h'); // … formatDatePastBy(23.49 * _steps.hour).should.equal('23h'); }); it('should format Twitter style relative time (English) (round: "floor")', function () { var timeAgo = new _TimeAgo.default('en'); var formatDatePastBy = function formatDatePastBy(secondsPassed) { return timeAgo.format(-secondsPassed * 1000, _objectSpread({ now: 0 }, _twitter.default, { round: 'floor' })); }; formatDatePastBy(0).should.equal('0s'); formatDatePastBy(0.9).should.equal('0s'); formatDatePastBy(1).should.equal('1s'); formatDatePastBy(59.9).should.equal('59s'); formatDatePastBy(60).should.equal('1m'); formatDatePastBy(1.9 * _steps.minute).should.equal('1m'); formatDatePastBy(2 * _steps.minute).should.equal('2m'); formatDatePastBy(2.9 * _steps.minute).should.equal('2m'); formatDatePastBy(3 * _steps.minute).should.equal('3m'); // … formatDatePastBy(59.9 * _steps.minute).should.equal('59m'); formatDatePastBy(60 * _steps.minute).should.equal('1h'); formatDatePastBy(1.9 * _steps.hour).should.equal('1h'); formatDatePastBy(2 * _steps.hour).should.equal('2h'); formatDatePastBy(2.9 * _steps.hour).should.equal('2h'); formatDatePastBy(3 * _steps.hour).should.equal('3h'); // … formatDatePastBy(23.9 * _steps.hour).should.equal('23h'); }); it('should format Twitter style relative time (English) (absolute dates)', function () { var timeAgo = new _TimeAgo.default('en'); // April 10th, 2016, 12:00. var now = new Date(2016, 3, 10, 12, 0).getTime(); var formatDatePastBy = function formatDatePastBy(secondsPassed) { return timeAgo.format(now - secondsPassed * 1000, _objectSpread({ now: now }, _twitter.default)); }; formatDatePastBy(_steps.day + 2 * _steps.minute + _steps.hour).should.equal('Apr 9'); // … // `month` is about 30.5 days. formatDatePastBy(_steps.month * 3).should.equal('Jan 10'); formatDatePastBy(_steps.month * 4).should.equal('Dec 10, 2015'); formatDatePastBy(_steps.year).should.equal('Apr 11, 2015'); // Test future dates. // `month` is about 30.5 days. formatDatePastBy(-1 * _steps.month * 8).should.equal('Dec 10'); formatDatePastBy(-1 * _steps.month * 9).should.equal('Jan 9, 2017'); }); it('should format Twitter style relative time (Russian)', function () { var timeAgo = new _TimeAgo.default('ru'); var now = new Date(2016, 3, 10, 22, 59).getTime(); var formatDatePastBy = function formatDatePastBy(secondsPassed) { return timeAgo.format(now - secondsPassed * 1000, _objectSpread({ now: now }, _twitter.default)); }; formatDatePastBy(0).should.equal('0 с'); formatDatePastBy(1).should.equal('1 с'); formatDatePastBy(_steps.minute).should.equal('1 мин'); formatDatePastBy(_steps.hour).should.equal('1 ч'); formatDatePastBy(_steps.day + 62 * _steps.minute).should.equal('9 апр.'); formatDatePastBy(_steps.year).should.equal('11 апр. 2015 г.'); }); it('should format Twitter style relative time (Korean)', function () { var timeAgo = new _TimeAgo.default('ko'); var now = new Date(2016, 3, 10, 22, 59).getTime(); var formatDatePastBy = function formatDatePastBy(secondsPassed) { return timeAgo.format(now - secondsPassed * 1000, _objectSpread({ now: now }, _twitter.default)); }; formatDatePastBy(_steps.minute).should.equal('1분'); formatDatePastBy(_steps.hour).should.equal('1시간'); formatDatePastBy(_steps.day + 62 * _steps.minute).should.equal('4월 9일'); formatDatePastBy(_steps.year).should.equal('2015년 4월 11일'); }); it('should format Twitter style relative time (German)', function () { var timeAgo = new _TimeAgo.default('de'); var now = new Date(2016, 3, 10, 22, 59).getTime(); var formatDatePastBy = function formatDatePastBy(secondsPassed) { return timeAgo.format(now - secondsPassed * 1000, _objectSpread({ now: now }, _twitter.default)); }; formatDatePastBy(_steps.minute).should.equal('1 Min.'); formatDatePastBy(_steps.hour).should.equal('1 Std.'); formatDatePastBy(_steps.day + 62 * _steps.minute).should.equal('9. Apr.'); formatDatePastBy(_steps.year).should.equal('11. Apr. 2015'); }); it('should format Twitter style relative time (French)', function () { var timeAgo = new _TimeAgo.default('fr'); var now = new Date(2016, 3, 10, 22, 59).getTime(); var formatDatePastBy = function formatDatePastBy(secondsPassed) { return timeAgo.format(now - secondsPassed * 1000, _objectSpread({ now: now }, _twitter.default)); }; formatDatePastBy(_steps.minute).should.equal('1 min'); formatDatePastBy(_steps.hour).should.equal('1 h'); formatDatePastBy(_steps.day + 62 * _steps.minute).should.equal('9 avr.'); formatDatePastBy(_steps.year).should.equal('11 avr. 2015'); }); it('should format Twitter style relative time (Chinese)', function () { var timeAgo = new _TimeAgo.default('zh'); var now = new Date(2016, 3, 10, 22, 59).getTime(); var formatDatePastBy = function formatDatePastBy(secondsPassed) { return timeAgo.format(now - secondsPassed * 1000, _objectSpread({ now: now }, _twitter.default)); }; formatDatePastBy(_steps.minute).should.equal('1分钟'); formatDatePastBy(_steps.hour).should.equal('1小时'); formatDatePastBy(_steps.day + 62 * _steps.minute).should.equal('4月9日'); formatDatePastBy(_steps.year).should.equal('2015年4月11日'); }); // This test won't pass because `Intl.DateTimeFormat` is read at // initialization time, not at run time. // it('should fall back to generic style when Intl.DateTimeFormat is not available', () => { // const DateTimeFormat = Intl.DateTimeFormat // Intl.DateTimeFormat = undefined // // const timeAgo = new TimeAgo('en') // timeAgo.format(Date.now() - 365 * 24 * hour * 1000, 'twitter').should.equal('1yr') // // Intl.DateTimeFormat = DateTimeFormat // }) it('should support timestamp argument on `yearMonthAndDay.test()`', function () { var timeAgo = new _TimeAgo.default('en'); timeAgo.format(0, 'twitter').should.equal('Jan 1, 1970'); }); it('should round as "floor"', function () { var timeAgo = new _TimeAgo.default('en'); var test = function test(time, result) { return timeAgo.format(time, 'twitter', { round: 'floor', now: 0 }).should.equal(result); }; test(2001, '2s'); test(2000, '2s'); test(1999, '1s'); test(1001, '1s'); test(1000, '1s'); test(999, '0s'); test(0, '0s'); test(-999, '0s'); test(-1000, '1s'); test(-1001, '1s'); test(-1999, '1s'); test(-2000, '2s'); test(-2001, '2s'); }); it('should get time to next update (round: "floor")', function () { var timeAgo = new _TimeAgo.default('en'); // April 10th, 2018, 12:00. var date = new Date(2018, 3, 10, 12, 0); // April 10th, 2016, 12:00 (two years earlier). var now = new Date(2016, 3, 10, 12, 0).getTime(); timeAgo.format(date, 'twitter', { now: now, getTimeToNextUpdate: true }).should.deep.equal(['Apr 10, 2018', // Updates on Jan 1st, 2018, 00:00. new Date(2018, 0, 1).getTime() - now]); // 1st, 2018, 00:00. now = new Date(2018, 0, 1).getTime(); timeAgo.format(date, 'twitter', { now: now, getTimeToNextUpdate: true, round: 'floor' }).should.deep.equal(['Apr 10', // Updates after April 9th, 2018, 12:00. new Date(2018, 3, 9, 12, 0).getTime() + 1 - now]); // After April 9th, 2018, 12:00. now = new Date(2018, 3, 9, 12, 0).getTime() + 1; timeAgo.format(date, 'twitter', { now: now, getTimeToNextUpdate: true, round: 'floor' }).should.deep.equal(['23h', // Updates in an hour. 60 * 60 * 1000]); }); it('should get time to next update (round: "round")', function () { var timeAgo = new _TimeAgo.default('en'); // April 10th, 2018, 12:00. var date = new Date(2018, 3, 10, 12, 0); var now; // 1st, 2018, 00:00. now = new Date(2018, 0, 1).getTime(); timeAgo.format(date, 'twitter', { now: now, getTimeToNextUpdate: true }).should.deep.equal(['Apr 10', // Updates after April 9th, 2018, 11:30. new Date(2018, 3, 9, 12, 0).getTime() + 30 * 60 * 1000 + 1 - now]); // After April 9th, 2018, 12:00. now = new Date(2018, 3, 9, 12, 0).getTime() + 30 * 60 * 1000 + 1; timeAgo.format(date, 'twitter', { now: now, getTimeToNextUpdate: true }).should.deep.equal(['23h', // Updates in an hour. 60 * 60 * 1000]); }); }); //# sourceMappingURL=twitter.test.js.map