UNPKG

@tdb/util

Version:
67 lines 2.78 kB
"use strict"; var _this = this; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var chai_1 = require("chai"); var libs_1 = require("../../libs"); var _1 = require("."); var FORMAT = 'YYYY-MM-DD hh:mm:ss'; var format = function (date) { return libs_1.moment(date).format(FORMAT); }; describe('timer', function () { it('starts with current date', function () { var now = libs_1.moment().format(FORMAT); var timer = _1.time.timer(); chai_1.expect(format(timer.startedAt)).to.eql(now); }); it('starts with given date', function () { var start = libs_1.moment() .add(1, 'd') .toDate(); var timer = _1.time.timer(start); chai_1.expect(format(timer.startedAt)).to.eql(format(start)); chai_1.expect(format(timer.startedAt)).to.not.eql(format(new Date())); }); it('reports elapsed milliseconds', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var timer; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: timer = _1.time.timer(); chai_1.expect(timer.elapsed()).to.lessThan(5); return [4, _1.time.wait(10)]; case 1: _a.sent(); chai_1.expect(timer.elapsed()).to.greaterThan(6); chai_1.expect(timer.elapsed('ms')).to.greaterThan(6); chai_1.expect(timer.elapsed('msec')).to.greaterThan(6); return [2]; } }); }); }); it('reports elapsed seconds (no decimal)', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var start, timer; return tslib_1.__generator(this, function (_a) { start = libs_1.moment() .subtract(1, 'minute') .subtract(30, 'seconds') .toDate(); timer = _1.time.timer(start); chai_1.expect(timer.elapsed('s')).to.eql(90); chai_1.expect(timer.elapsed('sec')).to.eql(90); return [2]; }); }); }); it('reports elapsed seconds (1 decimal place)', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var start, timer; return tslib_1.__generator(this, function (_a) { start = libs_1.moment() .subtract(123, 'milliseconds') .toDate(); timer = _1.time.timer(start); chai_1.expect(timer.elapsed('s')).to.eql(0.1); chai_1.expect(timer.elapsed('sec')).to.eql(0.1); return [2]; }); }); }); }); //# sourceMappingURL=timer.test.js.map