-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.index.js
101 lines (84 loc) · 4.08 KB
/
test.index.js
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
/**
* 测试 文件
* @author ydr.me
* @create 2016-05-17 12:13
*/
'use strict';
var date = require('../src/index.js');
describe('index.js', function () {
it('.now', function () {
expect(typeof date.now()).toEqual('number');
});
it('.id', function () {
var d = new Date(2016, 0, 1);
expect(date.id(d)).toEqual(20160101);
});
it('.parse', function () {
expect(isNaN(date.parse('...'))).toEqual(false);
expect(isNaN(date.parse(new Date('...')))).toEqual(false);
expect(date.parse([]).getFullYear()).toEqual(new Date().getFullYear());
expect(date.parse([2016]).getFullYear()).toEqual(2016);
expect(date.parse([2016, 0]).getMonth()).toEqual(0);
expect(date.parse([2016, 0, 1]).getDate()).toEqual(1);
expect(date.parse([2016, 0, 1, 1]).getHours()).toEqual(1);
expect(date.parse([2016, 0, 1, 1, 1]).getMinutes()).toEqual(1);
expect(date.parse([2016, 0, 1, 1, 1, 1]).getSeconds()).toEqual(1);
expect(date.parse([2016, 0, 1, 1, 1, 1, 1]).getMilliseconds()).toEqual(1);
});
it('.format', function () {
expect(date.format()).toMatch(/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/);
expect(date.format('YYYY'), new Date()).toMatch(/^\d{4}$/);
});
it('.isLeapYear', function () {
expect(date.isLeapYear(2016)).toEqual(true);
expect(date.isLeapYear(2017)).toEqual(false);
});
it('.getDaysInMonth', function () {
expect(date.getDaysInMonth(2016, 1)).toEqual(29);
expect(date.getDaysInMonth(2017, 1)).toEqual(28);
});
it('.getDaysInYear', function () {
expect(date.getDaysInYear(2016, 0, 1)).toEqual(1);
expect(date.getDaysInYear(2016, 11, 31)).toEqual(366);
});
it('.getWeeksInYear', function () {
expect(date.getWeeksInYear(2016, 0, 1)).toEqual(1);
expect(date.getWeeksInYear(2016, 3, 11)).toEqual(16);
expect(date.getWeeksInYear(2016, 0, 1, 3)).toEqual(1);
expect(date.getWeeksInYear(2016, 3, 11, 3)).toEqual(15);
});
it('.getWeeksInMonth', function () {
expect(date.getWeeksInMonth(2016, 0, 1)).toEqual(1);
expect(date.getWeeksInMonth(2016, 3, 11)).toEqual(3);
expect(date.getWeeksInMonth(2016, 0, 1, 3)).toEqual(1);
expect(date.getWeeksInMonth(2016, 3, 11, 3)).toEqual(2);
expect(date.getWeeksInMonth(2016, 9, 30, 1)).toEqual(5);
expect(date.getWeeksInMonth(2016, 9, 31, 1)).toEqual(6);
});
it('.from', function () {
var now = date.now();
// 过去
expect(date.from(0, now)).toEqual('很久之前');
expect(date.from(now - 1, now)).toEqual('刚刚');
expect(date.from(now - 10 * 1000, now)).toEqual('10秒前');
expect(date.from(now - 30 * 60 * 1000, now)).toEqual('30分钟前');
expect(date.from(now - 60 * 60 * 1000, now)).toEqual('1小时前');
expect(date.from(now - 24 * 60 * 60 * 1000, now)).toEqual('1天前');
expect(date.from(now - 30 * 24 * 60 * 60 * 1000, now)).toEqual('1个月前');
expect(date.from(now - 12 * 30 * 24 * 60 * 60 * 1000, now)).toEqual('1年前');
expect(date.from(now - 9 * 12 * 30 * 24 * 60 * 60 * 1000, now)).toEqual('9年前');
expect(date.from(now - 10 * 12 * 30 * 24 * 60 * 60 * 1000, now)).toEqual('很久之前');
// 将来
expect(date.from(now + 1, now)).toEqual('即将');
expect(date.from(now + 10 * 1000, now)).toEqual('10秒后');
expect(date.from(now + 60 * 60 * 1000, now)).toEqual('1小时后');
expect(date.from(now + 24 * 60 * 60 * 1000, now)).toEqual('1天后');
expect(date.from(now + 30 * 24 * 60 * 60 * 1000, now)).toEqual('1个月后');
expect(date.from(now + 12 * 30 * 24 * 60 * 60 * 1000, now)).toEqual('1年后');
expect(date.from(now + 9 * 12 * 30 * 24 * 60 * 60 * 1000 + 1, now)).toEqual('9年后');
expect(date.from(now + 10 * 12 * 30 * 24 * 60 * 60 * 1000 + 1, now)).toEqual('很久之后');
});
it('.iso', function () {
expect(date.iso()).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/);
});
});