Skip to content

Commit 7ea1a24

Browse files
committed
more unit tests added
1 parent 80d3f53 commit 7ea1a24

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

src/lib/strformat.test.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import {
2+
shortDateFormat,
3+
relativeDateFormat,
4+
secondsToTime,
5+
bytesToHumanSize,
6+
shortenFileName,
7+
idToColorClass,
8+
flagEmoji
9+
} from './strformat.js';
10+
11+
describe('shortDateFormat', () => {
12+
const now = new Date('2025-07-25T12:30:00');
13+
// Mock Date.now
14+
let dateNowSpy;
15+
beforeAll(() => {
16+
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => now.getTime());
17+
});
18+
afterAll(() => {
19+
dateNowSpy.mockRestore();
20+
});
21+
22+
test('should format for same day', () => {
23+
const then = new Date('2025-07-25T10:15:00');
24+
// Note: output depends on the test runner's locale and timezone.
25+
// Assuming a locale where the format is HH:mm.
26+
expect(shortDateFormat(then, 'en-US')).toMatch(/10:15/);
27+
});
28+
29+
test('should format for same year, different day', () => {
30+
const then = new Date('2025-06-20T10:15:00');
31+
expect(shortDateFormat(then, 'en-US')).toMatch(/Jun 20, 10:15/);
32+
});
33+
34+
test('should format for different year', () => {
35+
const then = new Date('2024-07-25T10:15:00');
36+
expect(shortDateFormat(then, 'en-US')).toBe('Jul 25, 2024');
37+
});
38+
});
39+
40+
describe('relativeDateFormat', () => {
41+
const now = new Date('2025-07-25T12:30:00Z');
42+
// Mock Date.now
43+
let dateNowSpy;
44+
beforeAll(() => {
45+
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => now.getTime());
46+
});
47+
afterAll(() => {
48+
dateNowSpy.mockRestore();
49+
});
50+
51+
test('should format for today', () => {
52+
const then = new Date('2025-07-25T10:00:00Z');
53+
expect(relativeDateFormat(then, 'en-US')).toBe('today');
54+
});
55+
56+
test('should format for yesterday', () => {
57+
const then = new Date('2025-07-24T10:00:00Z');
58+
expect(relativeDateFormat(then, 'en-US')).toBe('yesterday');
59+
});
60+
61+
test('should format for a past date', () => {
62+
const then = new Date('2025-07-20T10:00:00Z');
63+
expect(relativeDateFormat(then, 'en-US')).toBe('7/20/2025');
64+
});
65+
66+
test('should format for tomorrow', () => {
67+
const then = new Date('2025-07-26T10:00:00Z');
68+
expect(relativeDateFormat(then, 'en-US')).toBe('tomorrow');
69+
});
70+
});
71+
72+
describe('secondsToTime', () => {
73+
test('should format seconds only', () => {
74+
expect(secondsToTime(45)).toBe('0:45');
75+
});
76+
77+
test('should format minutes and seconds', () => {
78+
expect(secondsToTime(156)).toBe('2:36');
79+
});
80+
81+
test('should format hours, minutes, and seconds', () => {
82+
expect(secondsToTime(3756)).toBe('1:02:36');
83+
});
84+
85+
test('should format with fixed minutes', () => {
86+
expect(secondsToTime(3726, true)).toBe('1:02:06');
87+
expect(secondsToTime(126, true)).toBe('02:06');
88+
});
89+
90+
test('should handle non-number input', () => {
91+
expect(secondsToTime('abc')).toBe('');
92+
});
93+
});
94+
95+
describe('bytesToHumanSize', () => {
96+
test('should format 0 bytes', () => {
97+
expect(bytesToHumanSize(0)).toBe('0 Bytes');
98+
});
99+
100+
test('should format bytes', () => {
101+
expect(bytesToHumanSize(500)).toBe('500 Bytes');
102+
});
103+
104+
test('should format kilobytes', () => {
105+
expect(bytesToHumanSize(1500)).toBe('1.46 KB');
106+
});
107+
108+
test('should format megabytes', () => {
109+
expect(bytesToHumanSize(1500000)).toBe('1.43 MB');
110+
});
111+
112+
test('should format gigabytes', () => {
113+
expect(bytesToHumanSize(1500000000)).toBe('1.40 GB');
114+
});
115+
});
116+
117+
describe('shortenFileName', () => {
118+
test('should not shorten if not needed', () => {
119+
expect(shortenFileName('shortname.txt', 20)).toBe('shortname.txt');
120+
});
121+
122+
test('should shorten a long file name', () => {
123+
const longName = 'this_is_a_very_long_file_name_that_needs_shortening.txt';
124+
expect(shortenFileName(longName, 20)).toBe('this_is_a…ening.txt');
125+
expect(shortenFileName(longName, 20).length).toBeLessThanOrEqual(20);
126+
});
127+
128+
test('should handle non-string input', () => {
129+
expect(shortenFileName(null, 20)).toBeNull();
130+
expect(shortenFileName(undefined, 20)).toBeUndefined();
131+
});
132+
});
133+
134+
describe('idToColorClass', () => {
135+
test('should generate light background color class', () => {
136+
expect(idToColorClass('usr123', true, false)).toMatch(/lt-bg-\d+/);
137+
});
138+
139+
test('should generate dark background color class', () => {
140+
expect(idToColorClass('usr123', false, false)).toMatch(/dk-bg-\d+/);
141+
});
142+
143+
test('should generate light foreground color class', () => {
144+
expect(idToColorClass('usr123', true, true)).toMatch(/lt-fg-\d+/);
145+
});
146+
147+
test('should generate dark foreground color class', () => {
148+
expect(idToColorClass('usr123', false, true)).toMatch(/dk-fg-\d+/);
149+
});
150+
151+
test('should be deterministic', () => {
152+
expect(idToColorClass('usr123', true, false)).toBe('lt-bg-2');
153+
expect(idToColorClass('grpABC', false, true)).toBe('dk-fg-3');
154+
});
155+
});
156+
157+
describe('flagEmoji', () => {
158+
test('should convert country code to flag', () => {
159+
expect(flagEmoji('US')).toBe('🇺🇸');
160+
});
161+
162+
test('should handle lowercase country code', () => {
163+
expect(flagEmoji('jp')).toBe('🇯🇵');
164+
});
165+
});

0 commit comments

Comments
 (0)