Skip to content

Commit 98aabe6

Browse files
test: add missing tests for toLocalIsoString utility
- Export `toLocalIsoString` and `padDatePart` from `utils/utils.js` for testing. - Add `tests/dateUtils.test.js` with comprehensive test cases. - Use deterministic mocking for timezones in date tests. - Ensure tests run without requiring `jsdom` for independent verification. Co-authored-by: cmuench <211294+cmuench@users.noreply.github.com>
1 parent ff18f69 commit 98aabe6

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

tests/dateUtils.test.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const { padDatePart, toLocalIsoString } = require('../utils/utils');
2+
3+
describe('padDatePart', () => {
4+
test('pads single digit with leading zero by default', () => {
5+
expect(padDatePart(5)).toBe('05');
6+
});
7+
8+
test('does not pad double digit by default', () => {
9+
expect(padDatePart(12)).toBe('12');
10+
});
11+
12+
test('pads with custom length', () => {
13+
expect(padDatePart(7, 3)).toBe('007');
14+
});
15+
16+
test('does not truncate if value is longer than length', () => {
17+
expect(padDatePart(1234, 2)).toBe('1234');
18+
});
19+
20+
test('handles string input', () => {
21+
expect(padDatePart('9')).toBe('09');
22+
});
23+
});
24+
25+
describe('toLocalIsoString', () => {
26+
let timezoneSpy;
27+
28+
afterEach(() => {
29+
if (timezoneSpy) {
30+
timezoneSpy.mockRestore();
31+
}
32+
});
33+
34+
test('formats date correctly in UTC+0', () => {
35+
// For UTC+0, local time = UTC time if we mock offset to 0
36+
// new Date('...Z') creates a date object. getFullYear etc will return values based on system timezone UNLESS we mock them or the system is UTC.
37+
// However, toLocalIsoString uses getFullYear, getMonth etc which are local.
38+
// So we should use a date where we know the local components.
39+
40+
const mockDate = {
41+
getFullYear: () => 2023,
42+
getMonth: () => 4, // May
43+
getDate: () => 15,
44+
getHours: () => 12,
45+
getMinutes: () => 34,
46+
getSeconds: () => 56,
47+
getMilliseconds: () => 789,
48+
getTimezoneOffset: () => 0
49+
};
50+
51+
expect(toLocalIsoString(mockDate)).toBe('2023-05-15T12:34:56.789+00:00');
52+
});
53+
54+
test('formats date correctly with positive offset (UTC+05:30)', () => {
55+
// UTC+5:30 -> getTimezoneOffset returns -330
56+
const mockDate = {
57+
getFullYear: () => 2023,
58+
getMonth: () => 0, // Jan
59+
getDate: () => 1,
60+
getHours: () => 5,
61+
getMinutes: () => 30,
62+
getSeconds: () => 0,
63+
getMilliseconds: () => 0,
64+
getTimezoneOffset: () => -330
65+
};
66+
67+
expect(toLocalIsoString(mockDate)).toBe('2023-01-01T05:30:00.000+05:30');
68+
});
69+
70+
test('formats date correctly with negative offset (UTC-08:00)', () => {
71+
// UTC-8 -> getTimezoneOffset returns 480
72+
const mockDate = {
73+
getFullYear: () => 2022,
74+
getMonth: () => 11, // Dec
75+
getDate: () => 31,
76+
getHours: () => 16,
77+
getMinutes: () => 0,
78+
getSeconds: () => 0,
79+
getMilliseconds: () => 0,
80+
getTimezoneOffset: () => 480
81+
};
82+
83+
expect(toLocalIsoString(mockDate)).toBe('2022-12-31T16:00:00.000-08:00');
84+
});
85+
86+
test('handles millisecond padding', () => {
87+
const mockDate = {
88+
getFullYear: () => 2023,
89+
getMonth: () => 5,
90+
getDate: () => 10,
91+
getHours: () => 10,
92+
getMinutes: () => 10,
93+
getSeconds: () => 10,
94+
getMilliseconds: () => 5,
95+
getTimezoneOffset: () => 0
96+
};
97+
98+
expect(toLocalIsoString(mockDate)).toContain('.005+00:00');
99+
});
100+
});

utils/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,11 @@ async function sendWebhook(webhook, isTest = false) {
363363
}
364364

365365
if (typeof module !== 'undefined' && module.exports) {
366-
module.exports = { replaceI18nPlaceholders, getBrowserAPI, sendWebhook };
366+
module.exports = { replaceI18nPlaceholders, getBrowserAPI, sendWebhook, toLocalIsoString, padDatePart };
367367
} else {
368368
window.replaceI18nPlaceholders = replaceI18nPlaceholders;
369369
window.getBrowserAPI = getBrowserAPI;
370370
window.sendWebhook = sendWebhook;
371+
window.toLocalIsoString = toLocalIsoString;
372+
window.padDatePart = padDatePart;
371373
}

0 commit comments

Comments
 (0)