source: trip-planner-front/node_modules/date-format/test/parse-test.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.3 KB
Line 
1"use strict";
2
3require("should");
4var dateFormat = require("../lib");
5
6describe("dateFormat.parse", function() {
7 it("should require a pattern", function() {
8 (function() {
9 dateFormat.parse();
10 }.should.throw(/pattern must be supplied/));
11 (function() {
12 dateFormat.parse(null);
13 }.should.throw(/pattern must be supplied/));
14 (function() {
15 dateFormat.parse("");
16 }.should.throw(/pattern must be supplied/));
17 });
18
19 describe("with a pattern that has no replacements", function() {
20 it("should return a new date when the string matches", function() {
21 dateFormat.parse("cheese", "cheese").should.be.a.Date();
22 });
23
24 it("should throw if the string does not match", function() {
25 (function() {
26 dateFormat.parse("cheese", "biscuits");
27 }.should.throw(/String 'biscuits' could not be parsed as 'cheese'/));
28 });
29 });
30
31 describe("with a full pattern", function() {
32 var pattern = "yyyy-MM-dd hh:mm:ss.SSSO";
33
34 it("should return the correct date if the string matches", function() {
35 var testDate = new Date();
36 testDate.setUTCFullYear(2018);
37 testDate.setUTCMonth(8);
38 testDate.setUTCDate(13);
39 testDate.setUTCHours(18);
40 testDate.setUTCMinutes(10);
41 testDate.setUTCSeconds(12);
42 testDate.setUTCMilliseconds(392);
43
44 dateFormat
45 .parse(pattern, "2018-09-14 04:10:12.392+1000")
46 .getTime()
47 .should.eql(testDate.getTime())
48 ;
49 });
50
51 it("should throw if the string does not match", function() {
52 (function() {
53 dateFormat.parse(pattern, "biscuits");
54 }.should.throw(
55 /String 'biscuits' could not be parsed as 'yyyy-MM-dd hh:mm:ss.SSSO'/
56 ));
57 });
58 });
59
60 describe("with a partial pattern", function() {
61 var testDate = new Date();
62 dateFormat.now = function() {
63 return testDate;
64 };
65
66 /**
67 * If there's no timezone in the format, then we verify against the local date
68 */
69 function verifyLocalDate(actual, expected) {
70 actual.getFullYear().should.eql(expected.year || testDate.getFullYear());
71 actual.getMonth().should.eql(expected.month || testDate.getMonth());
72 actual.getDate().should.eql(expected.day || testDate.getDate());
73 actual.getHours().should.eql(expected.hours || testDate.getHours());
74 actual.getMinutes().should.eql(expected.minutes || testDate.getMinutes());
75 actual.getSeconds().should.eql(expected.seconds || testDate.getSeconds());
76 actual
77 .getMilliseconds()
78 .should.eql(expected.milliseconds || testDate.getMilliseconds());
79 }
80
81 /**
82 * If a timezone is specified, let's verify against the UTC time it is supposed to be
83 */
84 function verifyDate(actual, expected) {
85 actual.getUTCFullYear().should.eql(expected.year || testDate.getUTCFullYear());
86 actual.getUTCMonth().should.eql(expected.month || testDate.getUTCMonth());
87 actual.getUTCDate().should.eql(expected.day || testDate.getUTCDate());
88 actual.getUTCHours().should.eql(expected.hours || testDate.getUTCHours());
89 actual.getUTCMinutes().should.eql(expected.minutes || testDate.getUTCMinutes());
90 actual.getUTCSeconds().should.eql(expected.seconds || testDate.getUTCSeconds());
91 actual
92 .getMilliseconds()
93 .should.eql(expected.milliseconds || testDate.getMilliseconds());
94 }
95
96 it("should return a date with missing values defaulting to current time", function() {
97 var date = dateFormat.parse("yyyy-MM", "2015-09");
98 verifyLocalDate(date, { year: 2015, month: 8 });
99 });
100
101 it("should use a passed in date for missing values", function() {
102 var missingValueDate = new Date(2010, 1, 8, 22, 30, 12, 100);
103 var date = dateFormat.parse("yyyy-MM", "2015-09", missingValueDate);
104 verifyLocalDate(date, {
105 year: 2015,
106 month: 8,
107 day: 8,
108 hours: 22,
109 minutes: 30,
110 seconds: 12,
111 milliseconds: 100
112 });
113 });
114
115 it("should handle variations on the same pattern", function() {
116 var date = dateFormat.parse("MM-yyyy", "09-2015");
117 verifyLocalDate(date, { year: 2015, month: 8 });
118
119 date = dateFormat.parse("yyyy MM", "2015 09");
120 verifyLocalDate(date, { year: 2015, month: 8 });
121
122 date = dateFormat.parse("MM, yyyy.", "09, 2015.");
123 verifyLocalDate(date, { year: 2015, month: 8 });
124 });
125
126 describe("should match all the date parts", function() {
127 it("works with dd", function() {
128 var date = dateFormat.parse("dd", "21");
129 verifyLocalDate(date, { day: 21 });
130 });
131
132 it("works with hh", function() {
133 var date = dateFormat.parse("hh", "12");
134 verifyLocalDate(date, { hours: 12 });
135 });
136
137 it("works with mm", function() {
138 var date = dateFormat.parse("mm", "34");
139 verifyLocalDate(date, { minutes: 34 });
140 });
141
142 it("works with ss", function() {
143 var date = dateFormat.parse("ss", "59");
144 verifyLocalDate(date, { seconds: 59 });
145 });
146
147 it("works with ss.SSS", function() {
148 var date = dateFormat.parse("ss.SSS", "23.452");
149 verifyLocalDate(date, { seconds: 23, milliseconds: 452 });
150 });
151
152 it("works with hh:mm O (+1000)", function() {
153 var date = dateFormat.parse("hh:mm O", "05:23 +1000");
154 verifyDate(date, { hours: 19, minutes: 23 });
155 });
156
157 it("works with hh:mm O (-200)", function() {
158 var date = dateFormat.parse("hh:mm O", "05:23 -200");
159 verifyDate(date, { hours: 7, minutes: 23 });
160 });
161
162 it("works with hh:mm O (+0930)", function() {
163 var date = dateFormat.parse("hh:mm O", "05:23 +0930");
164 verifyDate(date, { hours: 19, minutes: 53 });
165 });
166 });
167 });
168
169 describe("with a date formatted by this library", function() {
170 describe("should format and then parse back to the same date", function() {
171 function testDateInitWithUTC() {
172 var td = new Date();
173 td.setUTCFullYear(2018);
174 td.setUTCMonth(8);
175 td.setUTCDate(13);
176 td.setUTCHours(18);
177 td.setUTCMinutes(10);
178 td.setUTCSeconds(12);
179 td.setUTCMilliseconds(392);
180 return td;
181 }
182
183 it("works with ISO8601_WITH_TZ_OFFSET_FORMAT", function() {
184 // For this test case to work, the date object must be initialized with
185 // UTC timezone
186 var td = testDateInitWithUTC();
187 var d = dateFormat(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, td);
188 dateFormat.parse(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, d)
189 .should.eql(td);
190 });
191
192 it("works with ISO8601_FORMAT", function() {
193 var td = new Date();
194 var d = dateFormat(dateFormat.ISO8601_FORMAT, td);
195 var actual = dateFormat.parse(dateFormat.ISO8601_FORMAT, d);
196 actual.should.eql(td);
197 });
198
199 it("works with DATETIME_FORMAT", function() {
200 var testDate = new Date();
201 dateFormat
202 .parse(
203 dateFormat.DATETIME_FORMAT,
204 dateFormat(dateFormat.DATETIME_FORMAT, testDate)
205 )
206 .should.eql(testDate);
207 });
208
209 it("works with ABSOLUTETIME_FORMAT", function() {
210 var testDate = new Date();
211 dateFormat
212 .parse(
213 dateFormat.ABSOLUTETIME_FORMAT,
214 dateFormat(dateFormat.ABSOLUTETIME_FORMAT, testDate)
215 )
216 .should.eql(testDate);
217 });
218 });
219 });
220});
Note: See TracBrowser for help on using the repository browser.