timestring/test.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-07-18 00:13:01 +02:00
var chai = require('chai');
var expect = chai.expect;
2015-05-01 18:31:25 +02:00
2016-01-15 11:00:12 +01:00
var timestring = require('./index');
2014-07-18 00:13:01 +02:00
describe('timestring', function() {
2015-05-01 18:31:25 +02:00
it('can parse a timestring', function() {
2016-07-30 16:56:51 +02:00
expect(timestring('500ms')).to.equal(0.5);
2016-01-15 21:05:30 +01:00
expect(timestring('1s')).to.equal(1);
expect(timestring('1m')).to.equal(60);
expect(timestring('1h')).to.equal(3600);
expect(timestring('1d')).to.equal(86400);
expect(timestring('1w')).to.equal(604800);
expect(timestring('1mth')).to.equal(2419200);
expect(timestring('1y')).to.equal(29030400);
});
2015-05-01 18:31:25 +02:00
it('can return a value in a specified unit', function() {
2016-07-30 16:56:51 +02:00
expect(timestring('1m', 'ms')).to.equal(60000);
2016-01-15 21:05:30 +01:00
expect(timestring('5m', 's')).to.equal(300);
expect(timestring('5m', 'm')).to.equal(5);
});
2016-01-15 12:00:03 +01:00
it('uses the passed options instead of the defaults', function() {
var opts = {
hoursPerDay: 1,
daysPerWeek: 2,
weeksPerMonth: 3,
monthsPerYear: 4
};
2016-01-15 21:05:30 +01:00
expect(timestring('1d', 'h', opts)).to.equal(1);
expect(timestring('1w', 'd', opts)).to.equal(2);
expect(timestring('1mth', 'w', opts)).to.equal(3);
expect(timestring('1y', 'mth', opts)).to.equal(4);
});
2015-05-01 18:31:25 +02:00
it('throws an error when an invalid unit is used in the timestring', function() {
2016-01-15 21:05:30 +01:00
expect(function() { timestring('1g'); }).to.throw(Error);
});
2015-05-01 18:31:25 +02:00
it('can parse a messy time string', function() {
2016-01-15 21:05:30 +01:00
expect(timestring('5 D a YS 4 h 2 0 mI nS')).to.equal(447600);
});
2014-07-18 00:13:01 +02:00
});