Skip to content

Commit d77a8dc

Browse files
committed
hotfix/mocked-locale-for-convertUnixToTime-test
1 parent 3073af3 commit d77a8dc

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/lib/formatters.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,5 @@ export function convertUnixToTime(seconds) {
2424
if (!seconds) return '';
2525
const date = new Date(seconds * 1000);
2626
const utcDate = new Date(date.toUTCString().slice(0, -4));
27-
const localDateTime = utcDate.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
28-
29-
return localDateTime.replace(/am|pm/, time => time.toUpperCase());
27+
return utcDate.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
3028
}

src/tests/lib/formatters.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
22
import { convertUnixToTime, formatLastUpdated, formatTime } from '$lib/formatters';
33

44
describe('convertUnixToTime', () => {
5+
beforeEach(() => {
6+
vi.useFakeTimers();
7+
vi.setSystemTime(new Date('2024-01-16T12:00:00Z'));
8+
vi.spyOn(Date.prototype, 'toLocaleTimeString').mockImplementation(function () {
9+
const hours = this.getUTCHours();
10+
const minutes = this.getUTCMinutes();
11+
const ampm = hours >= 12 ? 'PM' : 'AM';
12+
const hour12 = hours % 12 || 12;
13+
const minutesPadded = minutes.toString().padStart(2, '0');
14+
return `${hour12}:${minutesPadded} ${ampm}`;
15+
});
16+
});
17+
18+
afterEach(() => {
19+
vi.useRealTimers();
20+
vi.restoreAllMocks();
21+
});
22+
523
it('returns a blank string when its input is null', () => {
624
expect(convertUnixToTime(null)).toBe('');
725
});
@@ -11,7 +29,7 @@ describe('convertUnixToTime', () => {
1129
});
1230

1331
it('converts a Unix timestamp to a locale-specific formatted time', () => {
14-
expect(convertUnixToTime(1727442050)).toBe('01:00 PM');
32+
expect(convertUnixToTime(1727442050)).toBe('7:30 AM');
1533
});
1634
});
1735

0 commit comments

Comments
 (0)