UNPKG

2.9 kBJavaScriptView Raw
1// Copyright IBM Corp. 2017,2019. All Rights Reserved.
2// Node module: loopback-datasource-juggler
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6'use strict';
7
8require('should');
9
10const DateString = require('../lib/date-string');
11const fmt = require('util').format;
12const inspect = require('util').inspect;
13const os = require('os');
14
15describe('DateString', function() {
16 describe('constructor', function() {
17 it('should support a valid date string', function() {
18 const theDate = '2015-01-01';
19 const date = new DateString(theDate);
20 date.should.not.eql(null);
21 date.when.should.eql(theDate);
22 date.toString().should.eql(theDate);
23 });
24
25 testValidInput('should allow date with time', '2015-01-01 02:00:00');
26 testValidInput('should allow full UTC datetime', '2015-06-30T20:00:00.000Z');
27 testValidInput('should allow date with UTC offset', '2015-01-01 20:00:00 GMT-5');
28
29 testInvalidInput('should throw on non-date string', 'notadate', 'Invalid date');
30 testInvalidInput('should throw on incorrect date-like value',
31 '2015-01-01 25:00:00', 'Invalid date');
32 testInvalidInput('should throw on non-string input', 20150101,
33 'Input must be a string');
34 testInvalidInput('should throw on null input', null, 'Input must be a string');
35
36 it('should update internal date on set', function() {
37 const date = new DateString('2015-01-01');
38 date.when = '2016-01-01';
39 date.when.should.eql('2016-01-01');
40 const d = new Date('2016-01-01');
41 // The internal date representation should also be updated!
42 date._date.toString().should.eql(d.toString());
43 });
44
45 it('should accept DateString instance', function() {
46 const input = new DateString('2015-01-01');
47 const inst = new DateString(input);
48 inst.toString().should.equal('2015-01-01');
49 });
50
51 it('should return custom inspect output', function() {
52 const date = new DateString('2015-01-01');
53 const result = inspect(date);
54 result.should.not.eql(null);
55 result.should.eql(fmt('DateString ' + inspect({
56 when: date.when,
57 _date: date._date,
58 })));
59 });
60
61 it('should return JSON output', function() {
62 const date = new DateString('2015-01-01');
63 const result = date.toJSON();
64 result.should.eql(JSON.stringify({when: date.when}));
65 });
66
67 function testValidInput(msg, val) {
68 it(msg, function() {
69 const theDate = new DateString(val);
70 theDate.when.should.eql(val);
71 const d = new Date(val);
72 theDate._date.toString().should.eql(d.toString());
73 });
74 }
75
76 function testInvalidInput(msg, val, err) {
77 it(msg, () => {
78 const fn = () => {
79 const theDate = new DateString(val);
80 };
81 fn.should.throw(err);
82 });
83 }
84 });
85});