-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathformatters.spec.tsx
320 lines (292 loc) · 13.1 KB
/
formatters.spec.tsx
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
import {
DAY,
formatAbbreviatedNumber,
formatFloat,
formatPercentage,
formatSecondsToClock,
getDuration,
getExactDuration,
MONTH,
parseClockToSeconds,
userDisplayName,
WEEK,
} from 'sentry/utils/formatters';
describe('getDuration()', function () {
it('should format durations', function () {
expect(formatSecondsToClock(0)).toBe('00:00');
expect(formatSecondsToClock(0.001)).toBe('00:00.001');
expect(formatSecondsToClock(0.01)).toBe('00:00.010');
expect(getDuration(0.1)).toBe('100 milliseconds');
expect(getDuration(0.1, 2)).toBe('100.00 milliseconds');
expect(getDuration(1)).toBe('1 second');
expect(getDuration(2)).toBe('2 seconds');
expect(getDuration(65)).toBe('1 minute');
expect(getDuration(122)).toBe('2 minutes');
expect(getDuration(3720)).toBe('1 hour');
expect(getDuration(36000)).toBe('10 hours');
expect(getDuration(86400)).toBe('1 day');
expect(getDuration(86400 * 2)).toBe('2 days');
expect(getDuration(604800)).toBe('1 week');
expect(getDuration(604800 * 4)).toBe('4 weeks');
expect(getDuration(2629800)).toBe('1 month');
expect(getDuration(604800 * 12)).toBe('3 months');
});
it('should format negative durations', function () {
expect(formatSecondsToClock(0)).toBe('00:00');
expect(formatSecondsToClock(-0.001)).toBe('00:00.001');
expect(formatSecondsToClock(-0.01)).toBe('00:00.010');
expect(getDuration(-0.1)).toBe('-100 milliseconds');
expect(getDuration(-0.1, 2)).toBe('-100.00 milliseconds');
expect(getDuration(-1)).toBe('-1 second');
expect(getDuration(-2)).toBe('-2 seconds');
expect(getDuration(-65)).toBe('-1 minute');
expect(getDuration(-122)).toBe('-2 minutes');
expect(getDuration(-3720)).toBe('-1 hour');
expect(getDuration(-36000)).toBe('-10 hours');
expect(getDuration(-86400)).toBe('-1 day');
expect(getDuration(-86400 * 2)).toBe('-2 days');
expect(getDuration(-604800)).toBe('-1 week');
expect(getDuration(-604800 * 4)).toBe('-4 weeks');
expect(getDuration(-2629800)).toBe('-1 month');
expect(getDuration(-604800 * 12)).toBe('-3 months');
});
it('should format numbers and abbreviate units', function () {
expect(getDuration(0, 2, true)).toBe('0.00ms');
expect(getDuration(0, 0, true)).toBe('0ms');
expect(getDuration(0.1, 0, true)).toBe('100ms');
expect(getDuration(0.1, 2, true)).toBe('100.00ms');
expect(getDuration(1, 2, true)).toBe('1.00s');
expect(getDuration(122, 0, true)).toBe('2min');
expect(getDuration(3600, 0, true)).toBe('1hr');
expect(getDuration(86400, 0, true)).toBe('1d');
expect(getDuration(86400 * 2, 0, true)).toBe('2d');
expect(getDuration(604800, 0, true)).toBe('1wk');
expect(getDuration(604800 * 2, 0, true)).toBe('2wk');
expect(getDuration(2629800, 0, true)).toBe('1mo');
expect(getDuration(604800 * 12, 0, true)).toBe('3mo');
});
it('should format numbers and abbreviate units with one letter', function () {
expect(getDuration(0, 2, false, true)).toBe('0.00ms');
expect(getDuration(0, 0, false, true)).toBe('0ms');
expect(getDuration(0.1, 0, false, true)).toBe('100ms');
expect(getDuration(0.1, 2, false, true)).toBe('100.00ms');
expect(getDuration(1, 2, false, true)).toBe('1.00s');
expect(getDuration(122, 0, false, true)).toBe('2m');
expect(getDuration(3600, 0, false, true)).toBe('1h');
expect(getDuration(86400, 0, false, true)).toBe('1d');
expect(getDuration(86400 * 2, 0, false, true)).toBe('2d');
expect(getDuration(604800, 0, false, true)).toBe('1w');
expect(getDuration(604800 * 2, 0, false, true)).toBe('2w');
expect(getDuration(2629800, 0, false, true)).toBe('4w');
expect(getDuration(604800 * 12, 0, false, true)).toBe('12w');
});
it('should format negative durations with absolute', function () {
expect(formatSecondsToClock(0)).toBe('00:00');
expect(formatSecondsToClock(-0.001)).toBe('00:00.001');
expect(formatSecondsToClock(-0.01)).toBe('00:00.010');
expect(getDuration(-0.1, 0, false, false, true)).toBe('100 milliseconds');
expect(getDuration(-0.1, 2, false, false, true)).toBe('100.00 milliseconds');
expect(getDuration(-1, 0, false, false, true)).toBe('1 second');
expect(getDuration(-2, 0, false, false, true)).toBe('2 seconds');
expect(getDuration(-65, 0, false, false, true)).toBe('1 minute');
expect(getDuration(-122, 0, false, false, true)).toBe('2 minutes');
expect(getDuration(-3720, 0, false, false, true)).toBe('1 hour');
expect(getDuration(-36000, 0, false, false, true)).toBe('10 hours');
expect(getDuration(-86400, 0, false, false, true)).toBe('1 day');
expect(getDuration(-86400 * 2, 0, false, false, true)).toBe('2 days');
expect(getDuration(-604800, 0, false, false, true)).toBe('1 week');
expect(getDuration(-604800 * 4, 0, false, false, true)).toBe('4 weeks');
expect(getDuration(-2629800, 0, false, false, true)).toBe('1 month');
expect(getDuration(-604800 * 12, 0, false, false, true)).toBe('3 months');
});
});
describe('formatSecondsToClock', function () {
it('should format durations', function () {
expect(formatSecondsToClock(0)).toBe('00:00');
expect(formatSecondsToClock(0.1)).toBe('00:00.100');
expect(formatSecondsToClock(1)).toBe('00:01');
expect(formatSecondsToClock(2)).toBe('00:02');
expect(formatSecondsToClock(65)).toBe('01:05');
expect(formatSecondsToClock(65.123)).toBe('01:05.123');
expect(formatSecondsToClock(122)).toBe('02:02');
expect(formatSecondsToClock(3720)).toBe('01:02:00');
expect(formatSecondsToClock(36000)).toBe('10:00:00');
expect(formatSecondsToClock(86400)).toBe('24:00:00');
expect(formatSecondsToClock(86400 * 2)).toBe('48:00:00');
});
it('should format negative durations', function () {
expect(formatSecondsToClock(-0)).toBe('00:00');
expect(formatSecondsToClock(-0.1)).toBe('00:00.100');
expect(formatSecondsToClock(-1)).toBe('00:01');
expect(formatSecondsToClock(-2)).toBe('00:02');
expect(formatSecondsToClock(-65)).toBe('01:05');
expect(formatSecondsToClock(-65.123)).toBe('01:05.123');
expect(formatSecondsToClock(-122)).toBe('02:02');
expect(formatSecondsToClock(-3720)).toBe('01:02:00');
expect(formatSecondsToClock(-36000)).toBe('10:00:00');
expect(formatSecondsToClock(-86400)).toBe('24:00:00');
expect(formatSecondsToClock(-86400 * 2)).toBe('48:00:00');
});
it('should not pad when padAll:false is set', function () {
const padAll = false;
expect(formatSecondsToClock(0, {padAll})).toBe('0:00');
expect(formatSecondsToClock(0.1, {padAll})).toBe('0:00.100');
expect(formatSecondsToClock(1, {padAll})).toBe('0:01');
expect(formatSecondsToClock(65, {padAll})).toBe('1:05');
expect(formatSecondsToClock(3720, {padAll})).toBe('1:02:00');
});
});
describe('parseClockToSeconds', function () {
it('should format durations', function () {
expect(parseClockToSeconds('0:00')).toBe(0);
expect(parseClockToSeconds('0:00.100')).toBe(0.1);
expect(parseClockToSeconds('0:01')).toBe(1);
expect(parseClockToSeconds('0:02')).toBe(2);
expect(parseClockToSeconds('1:05')).toBe(65);
expect(parseClockToSeconds('1:05.123')).toBe(65.123);
expect(parseClockToSeconds('2:02')).toBe(122);
expect(parseClockToSeconds('1:02:00')).toBe(3720);
expect(parseClockToSeconds('10:00:00')).toBe(36000);
expect(parseClockToSeconds('24:00:00')).toBe(DAY / 1000);
expect(parseClockToSeconds('48:00:00')).toBe((DAY * 2) / 1000);
expect(parseClockToSeconds('2:00:00:00')).toBe((DAY * 2) / 1000);
expect(parseClockToSeconds('1:00:00:00:00')).toBe(WEEK / 1000);
expect(parseClockToSeconds('1:00:00:00:00:00')).toBe(MONTH / 1000);
});
it('should ignore non-numeric input', function () {
expect(parseClockToSeconds('hello world')).toBe(0);
expect(parseClockToSeconds('a:b:c')).toBe(0);
expect(parseClockToSeconds('a:b:c.d')).toBe(0);
expect(parseClockToSeconds('a:b:10.d')).toBe(10);
expect(parseClockToSeconds('a:10:c.d')).toBe(600);
});
it('should handle as much invalid input as possible', function () {
expect(parseClockToSeconds('a:b:c.123')).toBe(0.123);
expect(parseClockToSeconds('a:b:10.d')).toBe(10);
expect(parseClockToSeconds('a:10:c.d')).toBe(600);
});
});
describe('formatAbbreviatedNumber()', function () {
it('should abbreviate numbers', function () {
expect(formatAbbreviatedNumber(0)).toBe('0');
expect(formatAbbreviatedNumber(100)).toBe('100');
expect(formatAbbreviatedNumber(1000)).toBe('1k');
expect(formatAbbreviatedNumber(10000000)).toBe('10m');
expect(formatAbbreviatedNumber(100000000000)).toBe('100b');
expect(formatAbbreviatedNumber(1000000000000)).toBe('1000b');
});
it('should abbreviate numbers that are strings', function () {
expect(formatAbbreviatedNumber('00')).toBe('0');
expect(formatAbbreviatedNumber('100')).toBe('100');
expect(formatAbbreviatedNumber('1000')).toBe('1k');
expect(formatAbbreviatedNumber('10000000')).toBe('10m');
expect(formatAbbreviatedNumber('100000000000')).toBe('100b');
expect(formatAbbreviatedNumber('1000000000000')).toBe('1000b');
});
it('should round to 1 decimal place', function () {
expect(formatAbbreviatedNumber(100.12)).toBe('100.12');
expect(formatAbbreviatedNumber(1500)).toBe('1.5k');
expect(formatAbbreviatedNumber(1213122)).toBe('1.2m');
});
it('should round to set amount of significant digits', () => {
expect(formatAbbreviatedNumber(100.12, 3)).toBe('100');
expect(formatAbbreviatedNumber(199.99, 3)).toBe('200');
expect(formatAbbreviatedNumber(1500, 3)).toBe('1.5k');
expect(formatAbbreviatedNumber(1213122, 3)).toBe('1.21m');
expect(formatAbbreviatedNumber(1500000000000, 3)).toBe('1500b');
expect(formatAbbreviatedNumber('1249.23421', 3)).toBe('1.25k');
expect(formatAbbreviatedNumber('1239567891299', 3)).toBe('1240b');
expect(formatAbbreviatedNumber('158.80421626984128', 3)).toBe('159');
});
});
describe('formatFloat()', function () {
it('should format decimals', function () {
expect(formatFloat(0, 0)).toBe(0);
expect(formatFloat(10.513434, 1)).toBe(10.5);
expect(formatFloat(10.513494, 3)).toBe(10.513);
});
it('should not round', function () {
expect(formatFloat(10.513494, 4)).toBe(10.5134);
});
});
describe('formatPercentage()', function () {
it('should format decimals', function () {
expect(formatPercentage(0.0, 0)).toBe('0%');
expect(formatPercentage(0.0, 2)).toBe('0%');
expect(formatPercentage(0.10513434, 1)).toBe('10.5%');
expect(formatPercentage(0.10513494, 3)).toBe('10.513%');
expect(formatPercentage(0.10513494, 4)).toBe('10.5135%');
});
});
describe('userDisplayName', function () {
it('should only show email, if name and email are the same', function () {
expect(
userDisplayName({
name: '[email protected]',
email: '[email protected]',
})
).toEqual('[email protected]');
});
it('should show name + email, if name and email differ', function () {
expect(
userDisplayName({
name: 'user',
email: '[email protected]',
})
).toEqual('user ([email protected])');
});
it('should show unknown author with email, if email is only provided', function () {
expect(
userDisplayName({
email: '[email protected]',
})
).toEqual('Unknown author ([email protected])');
});
it('should show unknown author, if author or email is just whitespace', function () {
expect(
userDisplayName({
// eslint-disable-next-line quotes
name: `\t\n `,
})
).toEqual('Unknown author');
expect(
userDisplayName({
// eslint-disable-next-line quotes
email: `\t\n `,
})
).toEqual('Unknown author');
});
it('should show unknown author, if user object is either not an object or incomplete', function () {
// @ts-expect-error
expect(userDisplayName()).toEqual('Unknown author');
expect(userDisplayName({})).toEqual('Unknown author');
});
});
describe('getExactDuration', () => {
it('should provide default value', () => {
expect(getExactDuration(0)).toEqual('0 milliseconds');
});
it('should format in the right way', () => {
expect(getExactDuration(2.030043848568126)).toEqual('2 seconds 30 milliseconds');
expect(getExactDuration(0.2)).toEqual('200 milliseconds');
expect(getExactDuration(13)).toEqual('13 seconds');
expect(getExactDuration(60)).toEqual('1 minute');
expect(getExactDuration(121)).toEqual('2 minutes 1 second');
expect(getExactDuration(234235435)).toEqual(
'387 weeks 2 days 1 hour 23 minutes 55 seconds'
);
});
it('should format negative durations', () => {
expect(getExactDuration(-2.030043848568126)).toEqual('-2 seconds -30 milliseconds');
expect(getExactDuration(-0.2)).toEqual('-200 milliseconds');
expect(getExactDuration(-13)).toEqual('-13 seconds');
expect(getExactDuration(-60)).toEqual('-1 minute');
expect(getExactDuration(-121)).toEqual('-2 minutes -1 second');
expect(getExactDuration(-234235435)).toEqual(
'-387 weeks -2 days -1 hour -23 minutes -55 seconds'
);
});
it('should abbreviate label', () => {
expect(getExactDuration(234235435, true)).toEqual('387wk 2d 1hr 23min 55s');
});
});