-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathformatters.spec.ts
More file actions
107 lines (95 loc) · 2.63 KB
/
formatters.spec.ts
File metadata and controls
107 lines (95 loc) · 2.63 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
import { describe, expect, it } from 'vitest'
import {
decodeHtmlEntities,
formatBytes,
formatCompactNumber,
toIsoDateString,
} from '../../../../app/utils/formatters'
describe('toIsoDateString', () => {
it('formats a date as YYYY-MM-DD', () => {
expect(toIsoDateString(new Date('2024-03-15T00:00:00Z'))).toBe('2024-03-15')
})
it('pads single-digit month and day', () => {
expect(toIsoDateString(new Date('2024-01-05T00:00:00Z'))).toBe('2024-01-05')
})
})
describe('decodeHtmlEntities', () => {
it.each([
['&', '&'],
['<', '<'],
['>', '>'],
['"', '"'],
[''', "'"],
[''', "'"],
[' ', ' '],
] as const)('%s → %s', (input, expected) => {
expect(decodeHtmlEntities(input)).toBe(expected)
})
it('decodes multiple entities in one string', () => {
expect(decodeHtmlEntities('a & b < c')).toBe('a & b < c')
})
it('leaves plain text unchanged', () => {
expect(decodeHtmlEntities('say no to bloat')).toBe('say no to bloat')
})
it('leaves unknown entities unchanged', () => {
expect(decodeHtmlEntities('&unknown;')).toBe('&unknown;')
})
})
describe('formatCompactNumber', () => {
describe('without options', () => {
it.each([
[0, '0'],
[1, '1'],
[999, '999'],
[1000, '1k'],
[1500, '2k'],
[10000, '10k'],
[1000000, '1M'],
[2500000, '3M'],
[1000000000, '1B'],
[1000000000000, '1T'],
] as const)('%d → %s', (input, expected) => {
expect(formatCompactNumber(input)).toBe(expected)
})
})
describe('with decimals', () => {
it.each([
[1500, 1, '1.5k'],
[1234567, 2, '1.23M'],
[1200000, 1, '1.2M'],
[1000, 2, '1k'],
] as const)('%d with %d decimals', (input, decimals, expected) => {
expect(formatCompactNumber(input, { decimals })).toBe(expected)
})
})
describe('with space', () => {
it('adds space before suffix', () => {
expect(formatCompactNumber(1500, { space: true })).toBe('2 k')
})
})
describe('negative values', () => {
it.each([
[-1000, '-1k'],
[-2500000, '-3M'],
[-42, '-42'],
] as const)('%d → %s', (input, expected) => {
expect(formatCompactNumber(input)).toBe(expected)
})
})
it('handles values below 1000', () => {
expect(formatCompactNumber(500)).toBe('500')
})
})
describe('formatBytes', () => {
it.each([
[0, '0 B'],
[512, '512 B'],
[1023, '1023 B'],
[1024, '1.0 kB'],
[1536, '1.5 kB'],
[1048576, '1.0 MB'],
[1572864, '1.5 MB'],
] as const)('%d → %s', (input, expected) => {
expect(formatBytes(input)).toBe(expected)
})
})