forked from betsol/time-delta
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
173 lines (139 loc) · 4.32 KB
/
tests.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
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
const expect = require('expect.js');
const timeDelta = require('../lib/time-delta.js');
describe('Time delta module', function () {
it('exposes factory', function () {
expect(timeDelta).to.have.property('create');
expect(timeDelta.create).to.be.a('function');
});
it('initializes correctly', function () {
const instance = timeDelta.create();
expect(instance).to.be.an('object');
});
describe('instance', function () {
let instance, date1, date2, delimiter, span;
before(function () {
// Difference between these two dates is:
// 1 hour, 17 minutes and 03 seconds.
date1 = new Date('2015-04-01T21:00:00');
date2 = new Date('2015-04-01T22:17:03');
delimiter = ',';
span = 2;
instance = timeDelta.create({
span: span,
delimiter: delimiter
});
});
it('provides formatting function', function () {
expect(instance).to.have.property('format');
});
describe('formatting function', function () {
it('returns result as a string', function () {
const result = instance.format(date1, date2);
expect(result).to.be.a('string');
});
it('respects span option', function () {
const span = 3;
const result = instance.format(date1, date2, {
span: span
});
const parts = result.split(delimiter);
expect(parts).to.have.length(span);
});
it('pluralizes correctly with long English type', function () {
// 1 hour, 17 minutes and 03 seconds.
testUnitTypes(['hour', 'minutes'], {
locale: 'en',
unitType: 'long'
});
});
it('pluralizes correctly with long Russian type', function () {
// 1 hour, 17 minutes and 03 seconds.
testUnitTypes(['час', 'минут', 'секунды'], {
locale: 'ru'
});
});
it('pluralizes correctly with short Russian type', function () {
// 1 hour, 17 minutes and 03 seconds.
testUnitTypes(['ч', 'мин', 'сек'], {
locale: 'ru',
unitType: 'short'
});
});
it('pluralizes correctly with unit type fallback', function () {
// 1 hour, 17 minutes and 03 seconds.
testUnitTypes(['час', 'минут', 'секунды'], {
locale: 'ru',
unitType: 'missing',
unitTypeLookupOrder: ['foo', 'bar', 'long']
});
});
/**
* Tests pluralized unit types.
*
* @param {Array} validUnits
* @param {object} config
*/
function testUnitTypes (validUnits, config) {
const result = instance.format(date1, date2, config);
const parts = result.split(delimiter);
parts.forEach(function (part, key) {
const subParts = part.split(' ');
const timeUnit = subParts[1];
expect(timeUnit).to.be(validUnits[key]);
});
}
});
it('provides formatting function', function () {
expect(instance).to.have.property('format');
});
it('satisfies test cases', () => {
const date1 = new Date('2015-04-01T21:00:00');
const date2 = new Date('2015-04-01T23:17:10');
const date3 = new Date('2016-07-12T14:10:10');
const instance = timeDelta.create({
locale: 'en',
});
expect(instance.format(date1, date2, {
span: 3,
}))
.to.equal('2 hours, 17 minutes, 10 seconds')
;
expect(instance.format(date1, date2, {
span: 3,
unitType: 'short',
}))
.to.equal('2 hr, 17 min, 10 sec')
;
expect(instance.format(date1, date2, {
span: 3,
unitType: 'narrow',
delimiter: ' ',
}))
.to.equal('2h 17m 10s')
;
expect(instance.format(date1, date3, {
unitType: 'short',
span: 2,
}))
.to.equal('1 yr, 4 mths')
;
expect(instance.format(date1, date3, {
unitType: 'long',
span: 7,
}))
.to.equal(
'1 year, 4 months, 2 weeks, 5 days, ' +
'17 hours, 10 minutes, 10 seconds'
)
;
expect(instance.format(date1, date2, {
locale: 'ru',
span: 3,
unitType: 'long',
delimiter: '; ',
}))
.to.equal('2 часа; 17 минут; 10 секунд')
;
});
});
});