|
| 1 | +// Copyright IBM Corp. 2014,2016. 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 | +/* global describe,it */ |
| 7 | +/* jshint expr:true */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +require('should'); |
| 12 | + |
| 13 | +var DateString = require('../lib/date-string'); |
| 14 | +var fmt = require('util').format; |
| 15 | +var inspect = require('util').inspect; |
| 16 | +var os = require('os'); |
| 17 | + |
| 18 | +describe('DateString', function() { |
| 19 | + describe('constructor', function() { |
| 20 | + it('should support a valid date string', function() { |
| 21 | + var theDate = '2015-01-01'; |
| 22 | + var date = new DateString(theDate); |
| 23 | + date.should.not.eql(null); |
| 24 | + date.when.should.eql(theDate); |
| 25 | + date.toString().should.eql(theDate); |
| 26 | + }); |
| 27 | + |
| 28 | + testValidInput('should allow date with time', '2015-01-01 02:00:00'); |
| 29 | + testValidInput('should allow full UTC datetime', '2015-06-30T20:00:00.000Z'); |
| 30 | + testValidInput('should allow date with UTC offset', '2015-01-01 20:00:00 GMT-5'); |
| 31 | + |
| 32 | + testInvalidInput('should throw on non-date string', 'notadate', 'Invalid date'); |
| 33 | + testInvalidInput('should throw on incorrect date-like value', |
| 34 | + '2015-01-01 25:00:00', 'Invalid date'); |
| 35 | + testInvalidInput('should throw on non-string input', 20150101, |
| 36 | + 'Input must be a string'); |
| 37 | + testInvalidInput('should throw on null input', null, 'Input must be a string'); |
| 38 | + |
| 39 | + it('should update internal date on set', function() { |
| 40 | + var date = new DateString('2015-01-01'); |
| 41 | + date.when = '2016-01-01'; |
| 42 | + date.when.should.eql('2016-01-01'); |
| 43 | + var d = new Date('2016-01-01'); |
| 44 | + // The internal date representation should also be updated! |
| 45 | + date._date.toString().should.eql(d.toString()); |
| 46 | + }); |
| 47 | + it('should return custom inspect output', function() { |
| 48 | + var date = new DateString('2015-01-01'); |
| 49 | + var result = inspect(date); |
| 50 | + result.should.not.eql(null); |
| 51 | + result.should.eql(fmt('DateString ' + inspect({ |
| 52 | + when: date.when, |
| 53 | + _date: date._date, |
| 54 | + }))); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return JSON output', function() { |
| 58 | + var date = new DateString('2015-01-01'); |
| 59 | + var result = date.toJSON(); |
| 60 | + result.should.eql(JSON.stringify({when: date.when})); |
| 61 | + }); |
| 62 | + |
| 63 | + function testValidInput(msg, val) { |
| 64 | + it(msg, function() { |
| 65 | + var theDate = new DateString(val); |
| 66 | + theDate.when.should.eql(val); |
| 67 | + var d = new Date(val); |
| 68 | + theDate._date.toString().should.eql(d.toString()); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + function testInvalidInput(msg, val, err) { |
| 73 | + it(msg, () => { |
| 74 | + var fn = () => { |
| 75 | + var theDate = new DateString(val); |
| 76 | + }; |
| 77 | + fn.should.throw(err); |
| 78 | + }); |
| 79 | + } |
| 80 | + }); |
| 81 | +}); |
0 commit comments