UNPKG

javascript-time-ago

Version:

Localized relative date/time formatting

772 lines (764 loc) 30.5 kB
"use strict"; var _mocha = require("mocha"); var _chai = require("chai"); var _TimeAgo = _interopRequireDefault(require("./TimeAgo.js")); var _en = _interopRequireDefault(require("../locale/en.json")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) { n[e] = r[e]; } return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0) { ; } } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } // Load locale specific relative date/time messages // Just so this function code is covered. _TimeAgo["default"].setDefaultLocale('en'); (0, _mocha.describe)("javascript-time-ago", function () { (0, _mocha.it)('should default to "round-minute" style', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now())).to.equal('just now'); (0, _chai.expect)(timeAgo.format(Date.now() + 20 * 1000)).to.equal('in a moment'); (0, _chai.expect)(timeAgo.format(Date.now() + 1 * 60 * 1000)).to.equal('in 1 minute'); (0, _chai.expect)(timeAgo.format(Date.now() + 4 * 60 * 1000)).to.equal('in 4 minutes'); }); (0, _mocha.it)('should tell `options` argument from `style` argument', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now(), 'mini-now')).to.equal('now'); (0, _chai.expect)(timeAgo.format(0, { future: true, now: 0 })).to.equal('in a moment'); (0, _chai.expect)(timeAgo.format(0, { labels: 'mini', steps: [{ formatAs: 'now' }, { formatAs: 'second' }] }, { future: true, now: 0 })).to.equal('now'); (0, _chai.expect)(timeAgo.format(0, { labels: ['mini'], steps: [{ formatAs: 'now' }, { formatAs: 'second' }] }, { future: true, now: 0 })).to.equal('now'); // `flavour` is a legacy name of `labels` property. (0, _chai.expect)(timeAgo.format(0, { flavour: 'mini' }, { future: true, now: 0 })).to.equal('now'); (0, _chai.expect)(timeAgo.format(0, { flavour: ['mini'] }, { future: true, now: 0 })).to.equal('now'); (0, _chai.expect)(timeAgo.format(0, { steps: [{ formatAs: 'minute' }] }, { future: true, now: 0 })).to.equal('in 0 minutes'); // `gradation` is a legacy name of `steps` property. (0, _chai.expect)(timeAgo.format(0, { gradation: [{ formatAs: 'minute' }] }, { future: true, now: 0 })).to.equal('in 0 minutes'); }); (0, _mocha.it)('should try various label types until an appropriate one is found', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now(), { labels: ['exotic', 'short'] })).to.equal('just now'); }); (0, _mocha.it)('should support the legacy name "flavour" of "labels"', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now(), { labels: ['exotic', 'short'] })).to.equal('just now'); }); (0, _mocha.it)('should fallback to "second.current" for "now" when "now" is not defined', function () { var timeAgo = new _TimeAgo["default"]('en'); var englishNow = _en["default"].now; delete _en["default"].now; _TimeAgo["default"].addLocale(_en["default"]); _en["default"].now = undefined; (0, _chai.expect)(timeAgo.format(Date.now(), { labels: 'long' })).to.equal('now'); _en["default"].now = englishNow; (0, _chai.expect)(timeAgo.format(Date.now(), { labels: 'long' })).to.equal('just now'); }); (0, _mocha.it)('should not use Intl.NumberFormat if it is not available', function () { var NumberFormat = Intl.NumberFormat; delete Intl.NumberFormat; var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now() + 60 * 1000, { labels: 'long-time' })).to.equal('1 minute'); Intl.NumberFormat = NumberFormat; }); (0, _mocha.it)('should work when "past"/"future" messages are same for all quantifiers', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now() + 365 * 24 * 60 * 60 * 1000, { labels: 'short' })).to.equal('in 1 yr.'); }); (0, _mocha.it)('should work when "now" is a string (does not differentiate between "past" and "future")', function () { var timeAgo = new _TimeAgo["default"]('en'); var englishNow = _en["default"].now; _en["default"].now = { now: 'now' }; _TimeAgo["default"].addLocale(_en["default"]); (0, _chai.expect)(timeAgo.format(Date.now(), { labels: 'long' })).to.equal('now'); _en["default"].now = englishNow; (0, _chai.expect)(timeAgo.format(Date.now(), { labels: 'long' })).to.equal('just now'); }); (0, _mocha.it)('should work when a unit has formatting rules for "past" and "future" which are strings (style: not "long", not "short", not "narrow")', function () { var timeAgo = new _TimeAgo["default"]('en'); var englishLongTimeMinute = _en["default"]['long-time'].minute; _en["default"]['long-time'].minute = { past: '{0} minute(s) ago', future: 'in {0} minute(s)' }; _TimeAgo["default"].addLocale(_en["default"]); // Past. (0, _chai.expect)(timeAgo.format(Date.now() - 60 * 1000, { labels: 'long-time' })).to.equal('1 minute(s) ago'); // Future (covers an "else" branch). (0, _chai.expect)(timeAgo.format(Date.now() + 60 * 1000, { labels: 'long-time' })).to.equal('in 1 minute(s)'); // Undo. _en["default"]['long-time'].minute = englishLongTimeMinute; (0, _chai.expect)(timeAgo.format(Date.now() - 60 * 1000, { labels: 'long-time' })).to.equal('1 minute'); }); (0, _mocha.it)("should format \"now\" for \"past\" time", function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now() + 10, { labels: ['long'] })).to.equal('in a moment'); }); (0, _mocha.it)('should accept a string style name argument', function () { var timeAgo = new _TimeAgo["default"]('en'); // "mini". (0, _chai.expect)(timeAgo.format(Date.now() - 0 * 1000, 'mini')).to.equal('0s'); (0, _chai.expect)(timeAgo.format(Date.now() - 1 * 1000, 'mini')).to.equal('1s'); // "mini-now". (0, _chai.expect)(timeAgo.format(Date.now() - 0 * 1000, 'mini-now')).to.equal('now'); (0, _chai.expect)(timeAgo.format(Date.now() - 1 * 1000, 'mini-now')).to.equal('1s'); // "mini-minute-now". (0, _chai.expect)(timeAgo.format(Date.now() - 29 * 1000, 'mini-minute-now')).to.equal('now'); (0, _chai.expect)(timeAgo.format(Date.now() - 60 * 1000, 'mini-minute-now')).to.equal('1m'); // "mini-minute". (0, _chai.expect)(timeAgo.format(Date.now() - 29 * 1000, 'mini-minute')).to.equal('0m'); (0, _chai.expect)(timeAgo.format(Date.now() - 60 * 1000, 'mini-minute')).to.equal('1m'); // "twitter". (0, _chai.expect)(timeAgo.format(Date.now() - 0 * 1000, 'twitter')).to.equal('0s'); (0, _chai.expect)(timeAgo.format(Date.now() - 1 * 1000, 'twitter')).to.equal('1s'); // "twitter-now". (0, _chai.expect)(timeAgo.format(Date.now() - 0 * 1000, 'twitter-now')).to.equal('now'); (0, _chai.expect)(timeAgo.format(Date.now() - 1 * 1000, 'twitter-now')).to.equal('1s'); // "twitter-minute-now". (0, _chai.expect)(timeAgo.format(Date.now() - 29 * 1000, 'twitter-minute-now')).to.equal('now'); (0, _chai.expect)(timeAgo.format(Date.now() - 60 * 1000, 'twitter-minute-now')).to.equal('1m'); // "twitter-minute". (0, _chai.expect)(timeAgo.format(Date.now() - 29 * 1000, 'twitter-minute')).to.equal('0m'); (0, _chai.expect)(timeAgo.format(Date.now() - 60 * 1000, 'twitter-minute')).to.equal('1m'); // "twitter-first-minute". (0, _chai.expect)(timeAgo.format(Date.now() - 29 * 1000, 'twitter-first-minute')).to.equal(''); (0, _chai.expect)(timeAgo.format(Date.now() - 60 * 1000, 'twitter-first-minute')).to.equal('1m'); // "approximate". (0, _chai.expect)(timeAgo.format(Date.now() - 45 * 1000, 'approximate')).to.equal('just now'); // "convenient" style was renamed to "approximate". (0, _chai.expect)(timeAgo.format(Date.now() - 45 * 1000, 'convenient')).to.equal('just now'); (0, _chai.expect)(timeAgo.format(Date.now() - 45 * 1000, 'round')).to.equal('45 seconds ago'); // "default" style was renamed to "round". (0, _chai.expect)(timeAgo.format(Date.now() - 45 * 1000, 'default')).to.equal('45 seconds ago'); (0, _chai.expect)(timeAgo.format(Date.now() - 29 * 1000, 'round-minute')).to.equal('just now'); // "time" style was renamed to "approximate-time". (0, _chai.expect)(timeAgo.format(Date.now() - 2 * 60 * 1000, 'time')).to.equal('2 minutes'); (0, _chai.expect)(timeAgo.format(Date.now() - 2 * 60 * 1000, 'approximate-time')).to.equal('2 minutes'); (0, _chai.expect)(timeAgo.format(Date.now(), 'exotic')).to.equal('just now'); }); (0, _mocha.it)('should accept empty constructor parameters', function () { var timeAgo = new _TimeAgo["default"](); (0, _chai.expect)(timeAgo.format(new Date())).to.equal('just now'); }); (0, _mocha.it)('should accept "mocked" Dates when testing', function () { var timeAgo = new _TimeAgo["default"]('en'); var mockedDate = { getTime: function getTime() { return Date.now(); } }; (0, _chai.expect)(timeAgo.format(mockedDate)).to.equal('just now'); }); (0, _mocha.it)('should not accept anything but Dates and timestamps', function () { var timeAgo = new _TimeAgo["default"]('en'); var thrower = function thrower() { return timeAgo.format('Jan 14, 2017'); }; (0, _chai.expect)(thrower).to["throw"]('Unsupported relative time formatter input: string, Jan 14, 2017'); }); (0, _mocha.it)('should return an empty string if the specified units are not available in locale data', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now(), { units: ['femtosecond'] })).to.equal(''); }); (0, _mocha.it)('should format for a style with "custom" function', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now(), { // `custom` returns a string custom: function custom(_ref) { var now = _ref.now, time = _ref.time, date = _ref.date, locale = _ref.locale; return locale; } })).to.equal('en'); (0, _chai.expect)(timeAgo.format(Date.now(), { // `custom` returns `undefined` custom: function custom(_ref2) { var now = _ref2.now, time = _ref2.time, date = _ref2.date, locale = _ref2.locale; return; } })).to.equal('just now'); }); (0, _mocha.it)('should throw an error when formating for a style with "custom" function and `getTimeToNextUpdate: true` parameter is passed', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(function () { timeAgo.format(Date.now(), { // `custom` returns a string. custom: function custom(_ref3) { var now = _ref3.now, time = _ref3.time, date = _ref3.date, locale = _ref3.locale; return locale; } }, { getTimeToNextUpdate: true }); }).to["throw"]('not supported'); }); (0, _mocha.it)('should format future dates', function () { (0, _chai.expect)(new _TimeAgo["default"]('en').format(Date.now() + 60 * 60 * 1000)).to.equal('in 1 hour'); (0, _chai.expect)(new _TimeAgo["default"]('ru').format(Date.now() + 60 * 1000)).to.equal('через 1 минуту'); }); (0, _mocha.it)('should accept "future" option', function () { // "now" unit. (0, _chai.expect)(new _TimeAgo["default"]('en').format(Date.now())).to.equal('just now'); (0, _chai.expect)(new _TimeAgo["default"]('en').format(Date.now(), 'approximate', { future: true })).to.equal('in a moment'); // Non-"now" unit, "long" style. // const style = { // gradation: [{ // factor: 1, // unit: 'second' // }], // labels: 'long' // } // expect(new TimeAgo('en').format(Date.now(), style)).to.equal('0 seconds ago') // expect(new TimeAgo('en').format(Date.now(), style, { future: true })).to.equal('in 0 seconds') // Non-"now" unit, "mini" style. var style2 = { style: [{ unit: 'year' }], labels: 'mini' }; (0, _chai.expect)(new _TimeAgo["default"]('ru').format(Date.now() - 5 * 365 * 24 * 60 * 60 * 1000, style2)).to.equal('5 л'); (0, _chai.expect)(new _TimeAgo["default"]('ru').format(Date.now() - 5 * 365 * 24 * 60 * 60 * 1000, style2, { future: true })).to.equal('5 л'); }); (0, _mocha.it)('should support the legacy properties: "gradation", "flavour", "factor", "unit", "\'tiny\'" labels style', function () { // Non-"now" unit, "tiny" style. var style = { gradation: [{ factor: 5, unit: 'year' }], flavour: 'tiny' }; (0, _chai.expect)(new _TimeAgo["default"]('ru').format(Date.now() - 10 * 1000, style)).to.equal('2 г'); (0, _chai.expect)(new _TimeAgo["default"]('ru').format(Date.now() - 10 * 1000, style, { future: true })).to.equal('2 г'); }); (0, _mocha.it)('should have generated missing quantifier functions for locales that do not have it in CLDR data', function () { (0, _chai.expect)(new _TimeAgo["default"]('ccp').format(Date.now() + 60 * 1000)).to.include(' 𑄟𑄨𑄚𑄨𑄘𑄬'); }); (0, _mocha.it)('should throw for non-existing locales', function () { (0, _chai.expect)(function () { return _TimeAgo["default"].addLocale(); }).to["throw"]('No locale data passed'); }); (0, _mocha.it)('should choose "future" variant of a label for `0` if "future: true" option is passed', function () { _TimeAgo["default"].addLocale(_en["default"]); var secondLabels = _en["default"]['mini'].second; _en["default"]['mini'].second = { past: '{0} seconds ago', future: 'in {0} seconds' }; (0, _chai.expect)(new _TimeAgo["default"]('en').format(Date.now(), { steps: [{ unit: 'second' }], // Uses "mini" labels so that it doesn't use `Intl.RelativeTimeFormat`. labels: 'mini' })).to.equal('0 seconds ago'); (0, _chai.expect)(new _TimeAgo["default"]('en').format(Date.now(), { steps: [{ unit: 'second' }], // Uses "mini" labels so that it doesn't use `Intl.RelativeTimeFormat`. labels: 'mini' }, { future: true })).to.equal('in 0 seconds'); _en["default"]['mini'].second = secondLabels; }); (0, _mocha.it)('should refresh the label when `refresh` parameter is passed', function () { var refreshedTimes = 0; var timeAgo = new _TimeAgo["default"]('en'); // in 1 second -> in 0 seconds. var _timeAgo$format = timeAgo.format(1000, 'twitter', { refresh: function refresh(text) { if (refreshedTimes === 0) { // This test sometimes throws an error for an unknown weird reason: // // "AssertionError: expected '1s' to equal '0s'". // // "Error: done() called multiple times in test <javascript-time-ago // should refresh the label when `refresh` parameter is passed> of file // c:\dev\javascript-time-ago\source\TimeAgo.test.js; in addition, // done() received error: AssertionError: expected +0 to equal 1" // // In case it does that, just re-run the tests and somehow the error is gone. // I didn't bother debugging the error because it's not clear how it's reproduced. // (0, _chai.expect)(text).to.equal('0s'); } else { throw new Error('Refresh should have been cancelled'); } refreshedTimes++; }, now: 0, round: 'floor' }), _timeAgo$format2 = _slicedToArray(_timeAgo$format, 2), text = _timeAgo$format2[0], cancelRefresh = _timeAgo$format2[1]; (0, _chai.expect)(text).to.equal('1s'); (0, _chai.expect)(cancelRefresh).to.be.a('function'); // Sidenote: `timeToNextUpdate` is `1`. var timeToNextUpdate = 1; return delay(timeToNextUpdate + 1).then(function () { (0, _chai.expect)(refreshedTimes).to.equal(1); cancelRefresh(); }); }); (0, _mocha.it)('should get time to next update (capped)', function () { var timeAgo = new _TimeAgo["default"]('en'); // in 1 second -> in 0 seconds. (0, _chai.expect)(timeAgo.format(0.5 * 365 * 24 * 60 * 60 * 1000, 'twitter', { getTimeToNextUpdate: true, now: 0, round: 'floor' })).to.deep.equal(['Jul 2', 2147483647]); }); (0, _mocha.it)('should get time to next update (capped)', function () { var timeAgo = new _TimeAgo["default"]('en'); // in 1 second -> in 0 seconds. (0, _chai.expect)(timeAgo.format(0.5 * 365 * 24 * 60 * 60 * 1000, 'twitter', { getTimeToNextUpdate: true, getTimeToNextUpdateUncapped: true, now: 0, round: 'floor' })).to.deep.equal(['Jul 2', 15681600001]); }); (0, _mocha.it)('should get time to next update (uncapped)', function () { var timeAgo = new _TimeAgo["default"]('en'); // in 1 second -> in 0 seconds. (0, _chai.expect)(timeAgo.format(1000, 'twitter', { getTimeToNextUpdate: true, now: 0, round: 'floor' })).to.deep.equal(['1s', 1]); }); (0, _mocha.it)('should get time to next update (round: "floor")', function () { var timeAgo = new _TimeAgo["default"]('en'); // in 1 second -> in 0 seconds. (0, _chai.expect)(timeAgo.format(1000, 'twitter', { getTimeToNextUpdate: true, now: 0, round: 'floor' })).to.deep.equal(['1s', 1]); }); (0, _mocha.it)('should get time to next update (round: "round")', function () { var timeAgo = new _TimeAgo["default"]('en'); // in 1 second -> in 0 seconds. (0, _chai.expect)(timeAgo.format(1000, 'twitter', { getTimeToNextUpdate: true, now: 0 })).to.deep.equal(['1s', 501]); }); (0, _mocha.it)('should get time to next update ("mini-now" style) (round: "floor")', function () { var timeAgo = new _TimeAgo["default"]('en'); // In 1 minute. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 0, round: 'floor' })).to.deep.equal(['1m', 1]); // Almost in 1 minute. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 1, round: 'floor' })).to.deep.equal(['59s', 1000]); // In 1 second. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 59 * 1000, round: 'floor' })).to.deep.equal(['1s', 1]); // Almost in 1 second. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 59 * 1000 + 1, round: 'floor' })).to.deep.equal(['now', // Right after zero point. 1000]); // Zero point (future to past). // `future: true`. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000, future: true, round: 'floor' })).to.deep.equal(['now', 1]); // Zero point (future to past). // `future: false`. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000, round: 'floor' })).to.deep.equal(['now', 1000]); // Right after zero point (past). (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000 + 1, round: 'floor' })).to.deep.equal(['now', 1000 - 1]); // 1 second ago. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000 + 1000, round: 'floor' })).to.deep.equal(['1s', 1000]); }); (0, _mocha.it)('should get time to next update ("mini-now" style) (round: "round")', function () { var timeAgo = new _TimeAgo["default"]('en'); // In 1 minute. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 0 })).to.deep.equal(['1m', 500 + 1]); // Almost in 1 minute. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 500 + 1 })).to.deep.equal(['59s', 1000]); // In 1 second. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 59.5 * 1000 })).to.deep.equal(['1s', 1]); // Almost in 1 second. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 59.5 * 1000 + 1 })).to.deep.equal(['now', // Right after zero point. 500]); // Zero point (future to past). // `future: true`. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000, future: true })).to.deep.equal(['now', 1]); // Zero point (future to past). // `future: false`. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000 })).to.deep.equal(['now', 500]); // Right after zero point (past). (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000 + 1 })).to.deep.equal(['now', 500 - 1]); // 1 second ago. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000 + 500 })).to.deep.equal(['1s', 1000]); }); (0, _mocha.it)('should get time to next update (first step has non-zero "minTime") (round: "floor")', function () { var timeAgo = new _TimeAgo["default"]('en'); // Future. // Inside the first step. // Updates soon. (0, _chai.expect)(timeAgo.format(60 * 1000, { steps: [{ formatAs: 'minute', minTime: 60 }], labels: 'mini', round: 'floor' }, { getTimeToNextUpdate: true, now: -0.5 * 1000 })).to.deep.equal(['1m', 0.5 * 1000 + 1]); // Future. // Outside of the first step. // Updates right after zero point. (0, _chai.expect)(timeAgo.format(60 * 1000, { steps: [{ formatAs: 'minute', minTime: 60 * 1000 }], labels: 'mini', round: 'floor' }, { getTimeToNextUpdate: true, now: 1 * 1000 })).to.deep.equal(['', 59 * 1000 + 1]); // Zero point. // Outside of the first step. // Updates at the first step's `minTime`. (0, _chai.expect)(timeAgo.format(60 * 1000, { steps: [{ formatAs: 'minute', minTime: 60 }], labels: 'mini', round: 'floor' }, { getTimeToNextUpdate: true, now: 60 * 1000 })).to.deep.equal(['', 60 * 1000]); // Past. // Inside the first step. // Updates at the next minute. (0, _chai.expect)(timeAgo.format(60 * 1000, { steps: [{ formatAs: 'minute', minTime: 60 }], labels: 'mini', round: 'floor' }, { getTimeToNextUpdate: true, now: 60 * 1000 + 60 * 1000 })).to.deep.equal(['1m', 60 * 1000]); // Almost in 1 minute. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 1, round: 'floor' })).to.deep.equal(['59s', 1000]); // In 1 second. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 59 * 1000, round: 'floor' })).to.deep.equal(['1s', 1]); // Almost in 1 second. // Updates right after the zero point. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 59 * 1000 + 1, round: 'floor' })).to.deep.equal(['now', 1000]); // Zero point (future to past). // `future: true`. // Updates right after zero point. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000, future: true, round: 'floor' })).to.deep.equal(['now', 1]); // Zero point (future to past). // `future: false`. // Updates at the next second. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000, round: 'floor' })).to.deep.equal(['now', 1000]); // Right after zero point (past). (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000 + 1, round: 'floor' })).to.deep.equal(['now', 1000 - 1]); // 1 second ago. (0, _chai.expect)(timeAgo.format(60 * 1000, 'mini-now', { getTimeToNextUpdate: true, now: 60 * 1000 + 1000, round: 'floor' })).to.deep.equal(['1s', 1000]); }); (0, _mocha.it)('should pass `formatAs()` in `step.format()`', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(Date.now(), { labels: 'long', steps: [{ format: function format(date, locale, _ref4) { var formatAs = _ref4.formatAs; return formatAs('second', 1); } }] })).to.equal('in 1 second'); }); (0, _mocha.it)('should support `polyfill: false` option', function () { var timeAgo = new _TimeAgo["default"]('en', { polyfill: false }); // Still uses "now" labels, even when not polyfilled. (0, _chai.expect)(timeAgo.format(0, 'round', { now: 0 })).to.equal('just now'); (0, _chai.expect)(timeAgo.format(1000, 'round', { now: 0 })).to.equal('in 1 second'); }); (0, _mocha.it)('should not use Intl.NumberFormat if it is not available', function () { var Intl = global.Intl; global.Intl = undefined; var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(1000, 'round', { now: 0 })).to.equal('in 1 second'); global.Intl = Intl; }); (0, _mocha.it)('should format `0` in past mode by default', function () { (0, _chai.expect)(new _TimeAgo["default"]('en').format(0, { labels: 'long', steps: [{ formatAs: 'second' }] }, { now: 0 })).to.equal('0 seconds ago'); }); (0, _mocha.it)('should format `0` in future mode when `future: true` option was passed', function () { (0, _chai.expect)(new _TimeAgo["default"]('en').format(0, { labels: 'long', steps: [{ formatAs: 'second' }] }, { now: 0, future: true })).to.equal('in 0 seconds'); }); (0, _mocha.it)('should throw if a step does not define `formatAs` or `format()`', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(function () { return timeAgo.format(Date.now() + 1000, { labels: 'long', steps: [{}] }); }).to["throw"]('Each step must define either `formatAs` or `format()`.'); }); (0, _mocha.it)('should add default locale', function () { (0, _chai.expect)(_TimeAgo["default"].getDefaultLocale()).to.equal('en'); _TimeAgo["default"].addDefaultLocale({ locale: 'el' }); (0, _chai.expect)(_TimeAgo["default"].getDefaultLocale()).to.equal('el'); // Doesn't throw an error because the same locale is already specified as default. _TimeAgo["default"].addDefaultLocale({ locale: 'el' }); // Could throw an error or output an error message because another locale is already specified as default. // expect(() => { _TimeAgo["default"].addDefaultLocale({ locale: 'es' }); // }).to.throw('but you have already added "el" as the default locale.') _TimeAgo["default"].setDefaultLocale('en'); }); (0, _mocha.it)('should support "floor" rounding', function () { var timeAgo = new _TimeAgo["default"]('en'); (0, _chai.expect)(timeAgo.format(0.9 * 1000, 'twitter', { now: 0, round: 'floor' })).to.equal('0s'); (0, _chai.expect)(timeAgo.format(1 * 1000, 'twitter', { now: 0, round: 'floor' })).to.equal('1s'); (0, _chai.expect)(timeAgo.format(1.9 * 1000, 'twitter', { now: 0, round: 'floor' })).to.equal('1s'); (0, _chai.expect)(timeAgo.format(2 * 1000, 'twitter', { now: 0, round: 'floor' })).to.equal('2s'); (0, _chai.expect)(timeAgo.format(1.9 * 60 * 1000, 'twitter', { now: 0, round: 'floor' })).to.equal('1m'); (0, _chai.expect)(timeAgo.format(2 * 60 * 1000, 'twitter', { now: 0, round: 'floor' })).to.equal('2m'); }); }); function delay(delayTime) { return new Promise(function (resolve) { setTimeout(resolve, delayTime); }); } //# sourceMappingURL=TimeAgo.test.js.map