2020-01-21 18:12:14 +00:00
|
|
|
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
|
2017-04-26 00:10:07 +00:00
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('should');
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const DateString = require('../lib/date-string');
|
|
|
|
const fmt = require('util').format;
|
|
|
|
const inspect = require('util').inspect;
|
|
|
|
const os = require('os');
|
2017-04-26 00:10:07 +00:00
|
|
|
|
|
|
|
describe('DateString', function() {
|
|
|
|
describe('constructor', function() {
|
|
|
|
it('should support a valid date string', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const theDate = '2015-01-01';
|
|
|
|
const date = new DateString(theDate);
|
2017-04-26 00:10:07 +00:00
|
|
|
date.should.not.eql(null);
|
|
|
|
date.when.should.eql(theDate);
|
|
|
|
date.toString().should.eql(theDate);
|
|
|
|
});
|
|
|
|
|
|
|
|
testValidInput('should allow date with time', '2015-01-01 02:00:00');
|
|
|
|
testValidInput('should allow full UTC datetime', '2015-06-30T20:00:00.000Z');
|
|
|
|
testValidInput('should allow date with UTC offset', '2015-01-01 20:00:00 GMT-5');
|
|
|
|
|
|
|
|
testInvalidInput('should throw on non-date string', 'notadate', 'Invalid date');
|
|
|
|
testInvalidInput('should throw on incorrect date-like value',
|
|
|
|
'2015-01-01 25:00:00', 'Invalid date');
|
|
|
|
testInvalidInput('should throw on non-string input', 20150101,
|
|
|
|
'Input must be a string');
|
|
|
|
testInvalidInput('should throw on null input', null, 'Input must be a string');
|
|
|
|
|
|
|
|
it('should update internal date on set', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const date = new DateString('2015-01-01');
|
2017-04-26 00:10:07 +00:00
|
|
|
date.when = '2016-01-01';
|
|
|
|
date.when.should.eql('2016-01-01');
|
2018-12-07 14:54:29 +00:00
|
|
|
const d = new Date('2016-01-01');
|
2017-04-26 00:10:07 +00:00
|
|
|
// The internal date representation should also be updated!
|
|
|
|
date._date.toString().should.eql(d.toString());
|
|
|
|
});
|
2019-05-13 13:21:00 +00:00
|
|
|
|
|
|
|
it('should accept DateString instance', function() {
|
|
|
|
const input = new DateString('2015-01-01');
|
|
|
|
const inst = new DateString(input);
|
|
|
|
inst.toString().should.equal('2015-01-01');
|
|
|
|
});
|
|
|
|
|
2017-04-26 00:10:07 +00:00
|
|
|
it('should return custom inspect output', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const date = new DateString('2015-01-01');
|
|
|
|
const result = inspect(date);
|
2017-04-26 00:10:07 +00:00
|
|
|
result.should.not.eql(null);
|
|
|
|
result.should.eql(fmt('DateString ' + inspect({
|
|
|
|
when: date.when,
|
|
|
|
_date: date._date,
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return JSON output', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const date = new DateString('2015-01-01');
|
|
|
|
const result = date.toJSON();
|
2017-04-26 00:10:07 +00:00
|
|
|
result.should.eql(JSON.stringify({when: date.when}));
|
|
|
|
});
|
|
|
|
|
|
|
|
function testValidInput(msg, val) {
|
|
|
|
it(msg, function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const theDate = new DateString(val);
|
2017-04-26 00:10:07 +00:00
|
|
|
theDate.when.should.eql(val);
|
2018-12-07 14:54:29 +00:00
|
|
|
const d = new Date(val);
|
2017-04-26 00:10:07 +00:00
|
|
|
theDate._date.toString().should.eql(d.toString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function testInvalidInput(msg, val, err) {
|
|
|
|
it(msg, () => {
|
2018-12-07 14:54:29 +00:00
|
|
|
const fn = () => {
|
|
|
|
const theDate = new DateString(val);
|
2017-04-26 00:10:07 +00:00
|
|
|
};
|
|
|
|
fn.should.throw(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|