-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
native-date-adapter.spec.ts
438 lines (370 loc) · 15.9 KB
/
native-date-adapter.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import {Platform} from '@angular/cdk/platform';
import {LOCALE_ID} from '@angular/core';
import {async, inject, TestBed} from '@angular/core/testing';
import {DEC, FEB, JAN, MAR} from '@angular/material/testing';
import {DateAdapter, MAT_DATE_LOCALE, NativeDateAdapter, NativeDateModule} from './index';
const SUPPORTS_INTL = typeof Intl != 'undefined';
describe('NativeDateAdapter', () => {
let platform: Platform;
let adapter: NativeDateAdapter;
let assertValidDate: (d: Date | null, valid: boolean) => void;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [NativeDateModule]
}).compileComponents();
}));
beforeEach(inject([DateAdapter, Platform],
(dateAdapter: NativeDateAdapter, _platform: Platform) => {
adapter = dateAdapter;
platform = _platform;
assertValidDate = (d: Date | null, valid: boolean) => {
expect(adapter.isDateInstance(d)).not.toBeNull(`Expected ${d} to be a date instance`);
expect(adapter.isValid(d!)).toBe(valid,
`Expected ${d} to be ${valid ? 'valid' : 'invalid'},` +
` but was ${valid ? 'invalid' : 'valid'}`);
};
}));
it('should get year', () => {
expect(adapter.getYear(new Date(2017, JAN, 1))).toBe(2017);
});
it('should get month', () => {
expect(adapter.getMonth(new Date(2017, JAN, 1))).toBe(0);
});
it('should get date', () => {
expect(adapter.getDate(new Date(2017, JAN, 1))).toBe(1);
});
it('should get day of week', () => {
expect(adapter.getDayOfWeek(new Date(2017, JAN, 1))).toBe(0);
});
it('should get long month names', () => {
expect(adapter.getMonthNames('long')).toEqual([
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'
]);
});
it('should get short month names', () => {
expect(adapter.getMonthNames('short')).toEqual([
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
]);
});
it('should get narrow month names', () => {
// Edge & IE use same value for short and narrow.
if (platform.EDGE || platform.TRIDENT) {
expect(adapter.getMonthNames('narrow')).toEqual([
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
]);
} else {
expect(adapter.getMonthNames('narrow')).toEqual([
'J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D'
]);
}
});
it('should get month names in a different locale', () => {
adapter.setLocale('ja-JP');
if (SUPPORTS_INTL) {
expect(adapter.getMonthNames('long')).toEqual([
'1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'
]);
} else {
expect(adapter.getMonthNames('long')).toEqual([
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'
]);
}
});
it('should get date names', () => {
expect(adapter.getDateNames()).toEqual([
'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17',
'18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'
]);
});
it('should get date names in a different locale', () => {
adapter.setLocale('ja-JP');
if (SUPPORTS_INTL) {
expect(adapter.getDateNames()).toEqual([
'1日', '2日', '3日', '4日', '5日', '6日', '7日', '8日', '9日', '10日', '11日', '12日',
'13日', '14日', '15日', '16日', '17日', '18日', '19日', '20日', '21日', '22日', '23日', '24日',
'25日', '26日', '27日', '28日', '29日', '30日', '31日'
]);
} else {
expect(adapter.getDateNames()).toEqual([
'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17',
'18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'
]);
}
});
it('should get long day of week names', () => {
expect(adapter.getDayOfWeekNames('long')).toEqual([
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
]);
});
it('should get short day of week names', () => {
expect(adapter.getDayOfWeekNames('short')).toEqual([
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
]);
});
it('should get narrow day of week names', () => {
// Edge & IE use two-letter narrow days.
if (platform.EDGE || platform.TRIDENT) {
expect(adapter.getDayOfWeekNames('narrow')).toEqual([
'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'
]);
} else {
expect(adapter.getDayOfWeekNames('narrow')).toEqual([
'S', 'M', 'T', 'W', 'T', 'F', 'S'
]);
}
});
it('should get day of week names in a different locale', () => {
adapter.setLocale('ja-JP');
if (SUPPORTS_INTL) {
expect(adapter.getDayOfWeekNames('long')).toEqual([
'日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日'
]);
} else {
expect(adapter.getDayOfWeekNames('long')).toEqual([
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
]);
}
});
it('should get year name', () => {
expect(adapter.getYearName(new Date(2017, JAN, 1))).toBe('2017');
});
it('should get year name in a different locale', () => {
adapter.setLocale('ja-JP');
if (SUPPORTS_INTL) {
expect(adapter.getYearName(new Date(2017, JAN, 1))).toBe('2017年');
} else {
expect(adapter.getYearName(new Date(2017, JAN, 1))).toBe('2017');
}
});
it('should get first day of week', () => {
expect(adapter.getFirstDayOfWeek()).toBe(0);
});
it('should create Date', () => {
expect(adapter.createDate(2017, JAN, 1)).toEqual(new Date(2017, JAN, 1));
});
it('should not create Date with month over/under-flow', () => {
expect(() => adapter.createDate(2017, DEC + 1, 1)).toThrow();
expect(() => adapter.createDate(2017, JAN - 1, 1)).toThrow();
});
it('should not create Date with date over/under-flow', () => {
expect(() => adapter.createDate(2017, JAN, 32)).toThrow();
expect(() => adapter.createDate(2017, JAN, 0)).toThrow();
});
it('should create Date with low year number', () => {
expect(adapter.createDate(-1, JAN, 1).getFullYear()).toBe(-1);
expect(adapter.createDate(0, JAN, 1).getFullYear()).toBe(0);
expect(adapter.createDate(50, JAN, 1).getFullYear()).toBe(50);
expect(adapter.createDate(99, JAN, 1).getFullYear()).toBe(99);
expect(adapter.createDate(100, JAN, 1).getFullYear()).toBe(100);
});
it("should get today's date", () => {
expect(adapter.sameDate(adapter.today(), new Date()))
.toBe(true, "should be equal to today's date");
});
it('should parse string', () => {
expect(adapter.parse('1/1/2017')).toEqual(new Date(2017, JAN, 1));
});
it('should parse number', () => {
let timestamp = new Date().getTime();
expect(adapter.parse(timestamp)).toEqual(new Date(timestamp));
});
it ('should parse Date', () => {
let date = new Date(2017, JAN, 1);
expect(adapter.parse(date)).toEqual(date);
expect(adapter.parse(date)).not.toBe(date);
});
it('should parse invalid value as invalid', () => {
let d = adapter.parse('hello');
expect(d).not.toBeNull();
expect(adapter.isDateInstance(d))
.toBe(true, 'Expected string to have been fed through Date.parse');
expect(adapter.isValid(d as Date))
.toBe(false, 'Expected to parse as "invalid date" object');
});
it('should format', () => {
if (SUPPORTS_INTL) {
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('1/1/2017');
} else {
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('Sun Jan 01 2017');
}
});
it('should format with custom format', () => {
if (SUPPORTS_INTL) {
expect(adapter.format(new Date(2017, JAN, 1), {
year: 'numeric',
month: 'long',
day: 'numeric'
})).toEqual('January 1, 2017');
} else {
expect(adapter.format(new Date(2017, JAN, 1), {
year: 'numeric',
month: 'long',
day: 'numeric'
})).toEqual('Sun Jan 01 2017');
}
});
it('should format with a different locale', () => {
adapter.setLocale('ja-JP');
if (SUPPORTS_INTL) {
// Edge & IE use a different format in Japanese.
if (platform.EDGE || platform.TRIDENT) {
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('2017年1月1日');
} else {
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('2017/1/1');
}
} else {
expect(adapter.format(new Date(2017, JAN, 1), {})).toEqual('Sun Jan 01 2017');
}
});
it('should throw when attempting to format invalid date', () => {
expect(() => adapter.format(new Date(NaN), {}))
.toThrowError(/NativeDateAdapter: Cannot format invalid date\./);
});
it('should add years', () => {
expect(adapter.addCalendarYears(new Date(2017, JAN, 1), 1)).toEqual(new Date(2018, JAN, 1));
expect(adapter.addCalendarYears(new Date(2017, JAN, 1), -1)).toEqual(new Date(2016, JAN, 1));
});
it('should respect leap years when adding years', () => {
expect(adapter.addCalendarYears(new Date(2016, FEB, 29), 1)).toEqual(new Date(2017, FEB, 28));
expect(adapter.addCalendarYears(new Date(2016, FEB, 29), -1)).toEqual(new Date(2015, FEB, 28));
});
it('should add months', () => {
expect(adapter.addCalendarMonths(new Date(2017, JAN, 1), 1)).toEqual(new Date(2017, FEB, 1));
expect(adapter.addCalendarMonths(new Date(2017, JAN, 1), -1)).toEqual(new Date(2016, DEC, 1));
});
it('should respect month length differences when adding months', () => {
expect(adapter.addCalendarMonths(new Date(2017, JAN, 31), 1)).toEqual(new Date(2017, FEB, 28));
expect(adapter.addCalendarMonths(new Date(2017, MAR, 31), -1)).toEqual(new Date(2017, FEB, 28));
});
it('should add days', () => {
expect(adapter.addCalendarDays(new Date(2017, JAN, 1), 1)).toEqual(new Date(2017, JAN, 2));
expect(adapter.addCalendarDays(new Date(2017, JAN, 1), -1)).toEqual(new Date(2016, DEC, 31));
});
it('should clone', () => {
let date = new Date(2017, JAN, 1);
expect(adapter.clone(date)).toEqual(date);
expect(adapter.clone(date)).not.toBe(date);
});
it('should preserve time when cloning', () => {
let date = new Date(2017, JAN, 1, 4, 5, 6);
expect(adapter.clone(date)).toEqual(date);
expect(adapter.clone(date)).not.toBe(date);
});
it('should compare dates', () => {
expect(adapter.compareDate(new Date(2017, JAN, 1), new Date(2017, JAN, 2))).toBeLessThan(0);
expect(adapter.compareDate(new Date(2017, JAN, 1), new Date(2017, FEB, 1))).toBeLessThan(0);
expect(adapter.compareDate(new Date(2017, JAN, 1), new Date(2018, JAN, 1))).toBeLessThan(0);
expect(adapter.compareDate(new Date(2017, JAN, 1), new Date(2017, JAN, 1))).toBe(0);
expect(adapter.compareDate(new Date(2018, JAN, 1), new Date(2017, JAN, 1))).toBeGreaterThan(0);
expect(adapter.compareDate(new Date(2017, FEB, 1), new Date(2017, JAN, 1))).toBeGreaterThan(0);
expect(adapter.compareDate(new Date(2017, JAN, 2), new Date(2017, JAN, 1))).toBeGreaterThan(0);
});
it('should clamp date at lower bound', () => {
expect(adapter.clampDate(
new Date(2017, JAN, 1), new Date(2018, JAN, 1), new Date(2019, JAN, 1)))
.toEqual(new Date(2018, JAN, 1));
});
it('should clamp date at upper bound', () => {
expect(adapter.clampDate(
new Date(2020, JAN, 1), new Date(2018, JAN, 1), new Date(2019, JAN, 1)))
.toEqual(new Date(2019, JAN, 1));
});
it('should clamp date already within bounds', () => {
expect(adapter.clampDate(
new Date(2018, FEB, 1), new Date(2018, JAN, 1), new Date(2019, JAN, 1)))
.toEqual(new Date(2018, FEB, 1));
});
it('should use UTC for formatting by default', () => {
if (SUPPORTS_INTL) {
expect(adapter.format(new Date(1800, 7, 14), {day: 'numeric'})).toBe('14');
} else {
expect(adapter.format(new Date(1800, 7, 14), {day: 'numeric'})).toBe('Thu Aug 14 1800');
}
});
it('should count today as a valid date instance', () => {
let d = new Date();
expect(adapter.isValid(d)).toBe(true);
expect(adapter.isDateInstance(d)).toBe(true);
});
it('should count an invalid date as an invalid date instance', () => {
let d = new Date(NaN);
expect(adapter.isValid(d)).toBe(false);
expect(adapter.isDateInstance(d)).toBe(true);
});
it('should count a string as not a date instance', () => {
let d = '1/1/2017';
expect(adapter.isDateInstance(d)).toBe(false);
});
it('should provide a method to return a valid date or null', () => {
let d = new Date();
expect(adapter.getValidDateOrNull(d)).toBe(d);
expect(adapter.getValidDateOrNull(new Date(NaN))).toBeNull();
expect(adapter.getValidDateOrNull(null)).toBeNull();
expect(adapter.getValidDateOrNull(undefined)).toBeNull();
expect(adapter.getValidDateOrNull('')).toBeNull();
expect(adapter.getValidDateOrNull(0)).toBeNull();
expect(adapter.getValidDateOrNull('Wed Jul 28 1993')).toBeNull();
expect(adapter.getValidDateOrNull('1595204418000')).toBeNull();
});
it('should create dates from valid ISO strings', () => {
assertValidDate(adapter.deserialize('1985-04-12T23:20:50.52Z'), true);
assertValidDate(adapter.deserialize('1996-12-19T16:39:57-08:00'), true);
assertValidDate(adapter.deserialize('1937-01-01T12:00:27.87+00:20'), true);
assertValidDate(adapter.deserialize('2017-01-01'), true);
assertValidDate(adapter.deserialize('2017-01-01T00:00:00'), true);
assertValidDate(adapter.deserialize('1990-13-31T23:59:00Z'), false);
assertValidDate(adapter.deserialize('1/1/2017'), false);
assertValidDate(adapter.deserialize('2017-01-01T'), false);
expect(adapter.deserialize('')).toBeNull();
expect(adapter.deserialize(null)).toBeNull();
assertValidDate(adapter.deserialize(new Date()), true);
assertValidDate(adapter.deserialize(new Date(NaN)), false);
});
it('should create an invalid date', () => {
assertValidDate(adapter.invalid(), false);
});
it('should not throw when attempting to format a date with a year less than 1', () => {
expect(() => adapter.format(new Date(-1, 1, 1), {})).not.toThrow();
});
it('should not throw when attempting to format a date with a year greater than 9999', () => {
expect(() => adapter.format(new Date(10000, 1, 1), {})).not.toThrow();
});
});
describe('NativeDateAdapter with MAT_DATE_LOCALE override', () => {
let adapter: NativeDateAdapter;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [NativeDateModule],
providers: [{provide: MAT_DATE_LOCALE, useValue: 'da-DK'}]
}).compileComponents();
}));
beforeEach(inject([DateAdapter], (d: NativeDateAdapter) => {
adapter = d;
}));
it('should take the default locale id from the MAT_DATE_LOCALE injection token', () => {
const expectedValue = SUPPORTS_INTL ?
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
expect(adapter.getDayOfWeekNames('long')).toEqual(expectedValue);
});
});
describe('NativeDateAdapter with LOCALE_ID override', () => {
let adapter: NativeDateAdapter;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [NativeDateModule],
providers: [{provide: LOCALE_ID, useValue: 'da-DK'}]
}).compileComponents();
}));
beforeEach(inject([DateAdapter], (d: NativeDateAdapter) => {
adapter = d;
}));
it('should cascade locale id from the LOCALE_ID injection token to MAT_DATE_LOCALE', () => {
const expectedValue = SUPPORTS_INTL ?
['søndag', 'mandag', 'tirsdag', 'onsdag', 'torsdag', 'fredag', 'lørdag'] :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
expect(adapter.getDayOfWeekNames('long')).toEqual(expectedValue);
});
});