-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatting.test.ts
More file actions
485 lines (382 loc) · 15 KB
/
formatting.test.ts
File metadata and controls
485 lines (382 loc) · 15 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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import {
formatPhoneNumber,
formatPercentage,
numberFormatter,
formatNumber,
formatDateForLocale,
getTime,
FUTURE,
PAST,
getUtcStartOfDay,
formatUtcDate,
calculateRelativeDate,
getDatePartsForLocale,
formatDateForChat,
} from '@/utils/formatting';
const EMPTY_STRING = '';
describe('formatPhoneNumber', () => {
it('returns combined prefix and number when both are provided', () => {
expect(formatPhoneNumber('+1', '123456789')).toBe('+1 123456789');
});
it('trims extra spaces from prefix and number', () => {
expect(formatPhoneNumber(' +1 ', ' 123456789 ')).toBe('+1 123456789');
});
it('returns only prefix if number is undefined', () => {
expect(formatPhoneNumber('+1', undefined)).toBe('+1');
});
it('returns only number if prefix is undefined', () => {
expect(formatPhoneNumber(undefined, '123456789')).toBe('123456789');
});
it('returns empty string if both prefix and number are undefined', () => {
expect(formatPhoneNumber(undefined, undefined)).toBe('');
});
it('returns empty string if both prefix and number are empty strings', () => {
expect(formatPhoneNumber('', '')).toBe('');
});
it('returns trimmed prefix if number is empty string', () => {
expect(formatPhoneNumber(' +1 ', '')).toBe('+1');
});
it('returns trimmed number if prefix is empty string', () => {
expect(formatPhoneNumber('', ' 123456789 ')).toBe('123456789');
});
it('handles whitespace-only strings', () => {
expect(formatPhoneNumber(' ', ' ')).toBe('');
expect(formatPhoneNumber(' ', ' 123 ')).toBe('123');
expect(formatPhoneNumber(' +1 ', ' ')).toBe('+1');
});
it('handles numeric string inputs', () => {
expect(formatPhoneNumber('1', '123')).toBe('1 123');
});
});
describe('formatPercentage', () => {
it('should return EMPTY_STRING when numberToFormat is undefined', () => {
expect(formatPercentage(undefined)).toBe(EMPTY_STRING);
});
it('should format number with default 2 decimals', () => {
expect(formatPercentage(12.3456)).toBe('12.35%');
expect(formatPercentage(12)).toBe('12.00%');
});
it('should format number with specified decimals', () => {
expect(formatPercentage(12.3456, 1)).toBe('12.3%');
expect(formatPercentage(12.3456, 3)).toBe('12.346%');
});
it('should handle zero correctly', () => {
expect(formatPercentage(0)).toBe('');
});
it('should handle negative numbers', () => {
expect(formatPercentage(-5.6789)).toBe('-5.68%');
});
it('should handle very small numbers', () => {
expect(formatPercentage(0.0001, 4)).toBe('0.0001%');
expect(formatPercentage(0.00012345, 6)).toBe('0.000123%');
});
it('should handle very large numbers', () => {
expect(formatPercentage(1234567.89, 2)).toBe('1234567.89%');
});
});
describe('numberFormatter', () => {
it('returns a NumberFormat instance', () => {
const nf = numberFormatter(2, 'en-GB');
expect(nf.format).toBeDefined();
expect(typeof nf.format).toBe('function');
});
it('formats number with correct fraction digits', () => {
const nf = numberFormatter(3, 'en-GB');
expect(nf.format(1.23456)).toBe('1.235'); // rounded
});
it('defaults language to en-GB when undefined', () => {
const nf = numberFormatter(2, '');
expect(nf.format(1234.56)).toBe('1,234.56');
});
it('works for other locales', () => {
const nf = numberFormatter(2, 'de-DE');
expect(nf.format(1234.56)).toBe('1.234,56');
});
});
describe('formatNumber', () => {
const language = 'en';
it('formats valid numbers correctly', () => {
expect(formatNumber(1234.567, 2, language)).toBe('1,234.57');
/**
* note: 0 should be treated as empty (platform behaviour)
*/
expect(formatNumber(0, 2, language)).toBe('');
});
it('returns EMPTY_STRING for undefined', () => {
expect(formatNumber(undefined, 2, language)).toBe('');
});
it('returns EMPTY_STRING for NaN', () => {
expect(formatNumber(NaN, 2, language)).toBe('');
});
it('respects fraction digits', () => {
expect(formatNumber(1.234567, 0, language)).toBe('1');
expect(formatNumber(1.234567, 4, language)).toBe('1.2346');
});
it('respects different locales', () => {
expect(formatNumber(1234.56, 2, 'de-DE')).toBe('1.234,56');
});
it('handles negative numbers', () => {
expect(formatNumber(-1234.56, 2, language)).toBe('-1,234.56');
});
});
describe('formatDateForLocale', () => {
const locale = 'en-US';
it('returns EMPTY_STRING when isoDate is empty string', () => {
expect(formatDateForLocale('', locale)).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING when isoDate is null', () => {
expect(formatDateForLocale(null as unknown as string, locale)).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING when isoDate is undefined', () => {
expect(formatDateForLocale(undefined as unknown as string, locale)).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING when isoDate is invalid', () => {
expect(formatDateForLocale('invalid-date', locale)).toBe(EMPTY_STRING);
expect(formatDateForLocale('2025-13-01T00:00:00Z', locale)).toBe(EMPTY_STRING);
});
it('formats a valid ISO date correctly', () => {
expect(formatDateForLocale('2025-04-23T11:06:29.000Z', locale)).toBe('23 Apr 2025');
expect(formatDateForLocale('2023-12-05T00:00:00.000Z', locale)).toBe('05 Dec 2023');
});
it('formats different locales correctly - for example German and French', () => {
expect(formatDateForLocale('2025-04-23T11:06:29.000Z', 'de-DE')).toBe('23 Apr. 2025');
expect(formatDateForLocale('2025-04-23T11:06:29.000Z', 'fr-FR')).toBe('23 avr. 2025');
});
it('returns EMPTY_STRING if Intl.DateTimeFormat returns parts with missing day, month, or year', () => {
const originalFormatToParts = Intl.DateTimeFormat.prototype.formatToParts;
Intl.DateTimeFormat.prototype.formatToParts = jest.fn(() => [
{ type: 'month', value: 'Apr' },
{ type: 'year', value: '2025' },
]);
expect(formatDateForLocale('2025-04-23T11:06:29.000Z', locale)).toBe(EMPTY_STRING);
Intl.DateTimeFormat.prototype.formatToParts = originalFormatToParts;
});
it('pads day with leading zero correctly', () => {
expect(formatDateForLocale('2025-04-05T11:06:29.000Z', locale)).toBe('05 Apr 2025');
});
});
describe('getTime', () => {
describe('valid ISO dates (UTC)', () => {
it('returns correct UTC time (HH:mm)', () => {
expect(getTime('2026-02-17T09:55:24.190Z')).toBe('09:55');
});
it('pads single digit hours and minutes', () => {
expect(getTime('2026-02-17T05:07:00.000Z')).toBe('05:07');
});
it('handles midnight correctly', () => {
expect(getTime('2026-02-17T00:00:00.000Z')).toBe('00:00');
});
it('handles end of day correctly', () => {
expect(getTime('2026-02-17T23:59:59.999Z')).toBe('23:59');
});
});
describe('invalid input', () => {
it('returns EMPTY_STRING for empty string', () => {
expect(getTime('')).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING for invalid date string', () => {
expect(getTime('not-a-date')).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING for malformed ISO string', () => {
expect(getTime('2026-99-99T99:99:99Z')).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING for null (runtime edge case)', () => {
expect(getTime(null as unknown as string)).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING for undefined (runtime edge case)', () => {
expect(getTime(undefined as unknown as string)).toBe(EMPTY_STRING);
});
});
});
describe('Date Utilities', () => {
describe('getUtcStartOfDay', () => {
it('should return UTC midnight for a given date', () => {
const date = new Date('2026-02-26T15:30:45Z'); // 3:30 PM UTC
const result = getUtcStartOfDay(date);
expect(result.getUTCHours()).toBe(0);
expect(result.getUTCMinutes()).toBe(0);
expect(result.getUTCSeconds()).toBe(0);
expect(result.getUTCMilliseconds()).toBe(0);
expect(result.getUTCDate()).toBe(26);
expect(result.getUTCMonth()).toBe(1); // Feb = 1
expect(result.getUTCFullYear()).toBe(2026);
});
it('should handle end of month correctly', () => {
const date = new Date('2026-02-28T23:59:59Z');
const result = getUtcStartOfDay(date);
expect(result.getUTCDate()).toBe(28);
expect(result.getUTCMonth()).toBe(1);
expect(result.getUTCFullYear()).toBe(2026);
});
it('should handle leap year February correctly', () => {
const date = new Date('2024-02-29T12:00:00Z');
const result = getUtcStartOfDay(date);
expect(result.getUTCDate()).toBe(29);
expect(result.getUTCMonth()).toBe(1);
expect(result.getUTCFullYear()).toBe(2024);
});
});
describe('formatUtcDate', () => {
it('should format date as YYYY-MM-DD', () => {
const date = new Date(Date.UTC(2026, 1, 26)); // Feb 26, 2026
expect(formatUtcDate(date)).toBe('2026-02-26');
});
it('should pad month and day with leading zeros', () => {
const date = new Date(Date.UTC(2026, 0, 5)); // Jan 5, 2026
expect(formatUtcDate(date)).toBe('2026-01-05');
});
it('should handle December correctly', () => {
const date = new Date(Date.UTC(2026, 11, 31)); // Dec 31, 2026
expect(formatUtcDate(date)).toBe('2026-12-31');
});
});
describe('calculateRelativeDate', () => {
const baseDate = new Date(Date.UTC(2026, 1, 26)); // Feb 26, 2026
it('should calculate past date correctly with 0 adjustment', () => {
const result = calculateRelativeDate('5', baseDate, PAST);
// past offset = -5 + 0 adjustment = -5 days → 2026-02-21
expect(result).toBe('2026-02-21');
});
it('should calculate future date correctly with +1 adjustment', () => {
const result = calculateRelativeDate('5', baseDate, FUTURE);
// future offset = 5 + 1 adjustment = 6 days → 2026-03-04
expect(result).toBe('2026-03-04');
});
it('should handle zero days for past', () => {
const result = calculateRelativeDate('0', baseDate, PAST);
expect(result).toBe('2026-02-26');
});
it('should handle zero days for future', () => {
const result = calculateRelativeDate('0', baseDate, FUTURE);
// future adjustment adds 1 day → 2026-02-27
expect(result).toBe('2026-02-27');
});
it('should handle negative day string for past', () => {
const result = calculateRelativeDate('-3', baseDate, PAST);
// parsedDays = -3, direction = -1 → -3 * -1 = 3, PAST_ADJUSTMENT=0 → +3 → 2026-03-01
expect(result).toBe('2026-03-01');
});
it('should handle negative day string for future', () => {
const result = calculateRelativeDate('-2', baseDate, FUTURE);
// parsedDays=-2, direction=1 → -2*1=-2, FUTURE_ADJUSTMENT=1 → -2+1=-1 → 2026-02-25
expect(result).toBe('2026-02-25');
});
it('should return empty string for invalid day string', () => {
const result = calculateRelativeDate('abc', baseDate, FUTURE);
expect(result).toBe('');
});
it('should handle large day offsets correctly', () => {
const result = calculateRelativeDate('100', baseDate, FUTURE);
// 100*1 + 1 = 101 days → baseDate + 101 days
expect(result).toBe('2026-06-07');
});
it('should handle leap year when adding days', () => {
const leapBase = new Date(Date.UTC(2024, 1, 28)); // Feb 28, 2024
const result = calculateRelativeDate('1', leapBase, FUTURE);
// 1*1 + 1 = 2 days → Feb 28 + 2 days = Mar 1, 2024
expect(result).toBe('2024-03-01');
});
});
});
describe('getDatePartsForLocale', () => {
const enLocale = 'en-GB';
const frLocale = 'fr-FR';
const isoDate = '2026-03-10T15:45:30Z';
it('returns null for undefined input', () => {
expect(getDatePartsForLocale(undefined, enLocale)).toBeNull();
});
it('returns null for invalid date string', () => {
expect(getDatePartsForLocale('invalid-date', enLocale)).toBeNull();
});
it('returns correct date parts for English locale', () => {
const result = getDatePartsForLocale(isoDate, enLocale);
expect(result).not.toBeNull();
if (!result) return;
expect(result.weekday).toBe('Tue');
expect(result.day).toBe('10');
expect(result.month).toBe('Mar');
expect(result.year).toBe('2026');
});
it('returns correct date parts for French locale', () => {
const result = getDatePartsForLocale(isoDate, frLocale);
expect(result).not.toBeNull();
if (!result) return;
expect(result.weekday).toBe('mar.');
expect(result.day).toBe('10');
expect(result.month).toBe('mars');
expect(result.year).toBe('2026');
});
it('works for another valid date', () => {
const iso = '2026-12-25T08:05:00Z';
const result = getDatePartsForLocale(iso, enLocale);
expect(result).not.toBeNull();
if (!result) return;
expect(result.weekday).toBe('Fri');
expect(result.day).toBe('25');
expect(result.month).toBe('Dec');
expect(result.year).toBe('2026');
});
});
describe('formatDateForChat', () => {
const locale = 'en-GB';
const NOW = new Date('2026-03-10T12:00:00Z');
beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(NOW);
});
afterAll(() => {
jest.useRealTimers();
});
describe('invalid input', () => {
it('returns EMPTY_STRING when isoDate is undefined', () => {
expect(formatDateForChat(undefined, locale)).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING when isoDate is empty string', () => {
expect(formatDateForChat('', locale)).toBe(EMPTY_STRING);
});
it('returns EMPTY_STRING when isoDate is invalid', () => {
expect(formatDateForChat('invalid-date', locale)).toBe(EMPTY_STRING);
});
});
describe('within last 24 hours', () => {
it('returns time (HH:mm)', () => {
const iso = '2026-03-10T09:30:00Z';
expect(formatDateForChat(iso, locale)).toBe('09:30');
});
});
describe('within last 7 days', () => {
it('returns weekday', () => {
const iso = '2026-03-08T10:00:00Z';
const result = formatDateForChat(iso, locale);
expect(result).toBe('Sun');
});
});
describe('within current year but older than 7 days', () => {
it('returns DD Mon', () => {
const iso = '2026-02-01T10:00:00Z';
expect(formatDateForChat(iso, locale)).toBe('01 Feb');
});
it('pads day with leading zero', () => {
const iso = '2026-01-05T10:00:00Z';
expect(formatDateForChat(iso, locale)).toBe('05 Jan');
});
});
describe('older than current year', () => {
it('returns DD Mon YYYY', () => {
const iso = '2025-12-25T10:00:00Z';
expect(formatDateForChat(iso, locale)).toBe('25 Dec 2025');
});
});
describe('different locales', () => {
it('formats weekday using locale rules', () => {
const iso = '2026-03-08T10:00:00Z';
const result = formatDateForChat(iso, 'fr-FR');
expect(result).toBe('dim.');
});
it('formats month using locale rules', () => {
const iso = '2026-02-01T10:00:00Z';
const result = formatDateForChat(iso, 'fr-FR');
expect(result).toBe('01 févr.');
});
});
});