index.test.js
1.61 KB
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
import { localISOdt } from '.';
describe('strings', () => {
test('YYYY-MM-DD', () => {
expect(localISOdt('2017-07-15')).toBe('2017-07-15');
expect(localISOdt('2017-07-15T00:11:22')).toBe('2017-07-15T00:11:22');
});
});
describe('numbers', () => {
test('seconds', () => {
// The [25] is to account for rare timezones at half-hour boundaries
expect(localISOdt(1500123456)).toMatch(/2017-07-1[456]T[01]\d:[25]7:36/);
});
test('fractional seconds', () => {
expect(localISOdt(1500123456.123)).toMatch(/2017-07-1[456]T[01]\d:[25]7:36/);
});
test('milliseconds', () => {
expect(localISOdt(1500123456789)).toMatch(/2017-07-1[456]T[01]\d:[25]7:36/);
});
test('microseconds', () => {
expect(localISOdt(1500123456789000)).toMatch(/2017-07-1[456]T[01]\d:[25]7:36/);
});
test('nanoseconds', () => {
expect(localISOdt(1500123456789000123)).toMatch(/2017-07-1[456]T[01]\d:[25]7:36/);
});
});
describe('dates', () => {
test('date object', () => {
const d = new Date('2017-07-15T00:11:22-07:00');
expect(localISOdt(d)).toMatch(/2017-07-1[456]T[01]\d:[14]1:22/);
});
});
describe('corner cases', () => {
test('now', () => {
if (localISOdt() === localISOdt(new Date())) {
expect(true).toBe(true);
} else {
// Give it another chance if we compared EXACTLY around the second changing
expect(localISOdt()).toBe(localISOdt(new Date()));
}
});
test('falsy', () => {
for (const falsy of [0, '', false, undefined]) {
if (localISOdt(falsy) === localISOdt()) expect(true).toBe(true);
else expect(localISOdt(falsy) === localISOdt());
}
});
});