UNPKG

javascript-time-ago

Version:

Localized relative date/time formatting

117 lines (112 loc) 3.64 kB
"use strict"; var _mocha = require("mocha"); var _chai = require("chai"); var _getStep = _interopRequireDefault(require("./getStep.js")); var _round = _interopRequireDefault(require("./round.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } (0, _mocha.describe)('getStep', function () { (0, _mocha.it)('should return nothing if no time units are supported', function () { (0, _chai.expect)((0, _getStep["default"])(_round["default"], 0, { units: ['femtosecond'] })).to.be.undefined; }); // it('should throw if a non-first step does not have a `minTime` or `test()`', () => { // expect(getStep([{ unit: 'second' }], 2, { units: ['second'] })).to.deep.equal({ unit: 'second' }) // // expect(() => { // getStep([{ unit: 'second' }, { unit: 'minute' }], 2, { units: ['second', 'minute'] }) // }).to.throw( // 'Each step must define either `minTime` or `test()`, except for the first one. Got "undefined", undefined. Step: {"unit":"minute"}' // ) // }) (0, _mocha.it)('should fall back to previous step if granularity is too high for the next step', function () { var steps = _round["default"].slice(); (0, _chai.expect)(steps[1].formatAs).to.equal('second'); steps[1].granularity = 3; (0, _chai.expect)((0, _getStep["default"])(steps, 1.49, { now: 0, units: ['now', 'second'] }).formatAs).to.equal('now'); // And if there's no previous step, then use the current one. var firstStep = steps[0]; steps.splice(0, 1); (0, _chai.expect)((0, _getStep["default"])(steps, 1.49, { now: 0, units: ['now', 'second'] }).formatAs).to.equal('second'); steps.unshift(firstStep); delete steps[1].granularity; }); (0, _mocha.it)('should support `minTime` object', function () { (0, _chai.expect)((0, _getStep["default"])([{ unit: 'second' }, { minTime: { "default": 10 }, unit: 'minute' }], 5, { now: 0, units: ['second', 'minute'] }).unit).to.equal('second'); (0, _chai.expect)((0, _getStep["default"])([{ unit: 'second' }, { minTime: { "default": 10 }, unit: 'minute' }], 10, { now: 0, units: ['second', 'minute'] }).unit).to.equal('minute'); (0, _chai.expect)((0, _getStep["default"])([{ id: 'seconds', unit: 'second' }, { minTime: { seconds: 20, "default": 10 }, unit: 'minute' }], 10, { now: 0, units: ['second', 'minute'] }).unit).to.equal('second'); }); (0, _mocha.it)('should support legacy `threshold()` function', function () { (0, _chai.expect)((0, _getStep["default"])([{ unit: 'second' }, { threshold: function threshold() { return 10; }, unit: 'minute' }], 5, { now: 0, units: ['second', 'minute'] }).unit).to.equal('second'); (0, _chai.expect)((0, _getStep["default"])([{ unit: 'second' }, { threshold: function threshold() { return 10; }, unit: 'minute' }], 10, { now: 0, units: ['second', 'minute'] }).unit).to.equal('minute'); }); (0, _mocha.it)('should stop when reaching a step that has no "minTime" and for which "minTime" could not be deduced', function () { (0, _chai.expect)((0, _getStep["default"])([{ formatAs: 'second' }, { formatAs: 'unsupported-time-unit' }], 10, { now: 0, units: ['second', 'unsupported-time-unit'] }).formatAs).to.equal('second'); }); }); //# sourceMappingURL=getStep.test.js.map