Skip to content

Commit 53158f0

Browse files
committed
fix: use UTC date parsing for contribution tooltips
1 parent ea059a6 commit 53158f0

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

components/InteractiveViewer.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { render, screen, fireEvent } from '@testing-library/react';
33
import { describe, it, expect, beforeEach, vi } from 'vitest';
4-
import InteractiveViewer from './InteractiveViewer';
4+
import InteractiveViewer, { formatDate } from './InteractiveViewer';
55

66
// getBoundingClientRect is not implemented in jsdom — mock it so mouse-position
77
// tests can assert normalized values without relying on a real layout engine.
@@ -23,6 +23,23 @@ beforeEach(() => {
2323
Element.prototype.releasePointerCapture = vi.fn();
2424
});
2525

26+
describe('formatDate', () => {
27+
it('formats valid UTC date strings correctly', () => {
28+
expect(formatDate('2025-06-15')).toBe('Jun 15, 2025');
29+
expect(formatDate('2025-01-01')).toBe('Jan 1, 2025');
30+
expect(formatDate('2025-12-31')).toBe('Dec 31, 2025');
31+
});
32+
33+
it('returns empty string for empty input', () => {
34+
expect(formatDate('')).toBe('');
35+
});
36+
37+
it('returns original string for malformed input', () => {
38+
expect(formatDate('2025-06')).toBe('2025-06');
39+
expect(formatDate('invalid-date-string')).toBe('invalid-date-string');
40+
});
41+
});
42+
2643
describe('InteractiveViewer', () => {
2744
// ── Existing behaviour ────────────────────────────────────────────────────
2845

components/InteractiveViewer.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ interface ActiveTooltipState {
5656
y: number;
5757
}
5858

59-
const formatDate = (dateStr: string): string => {
59+
export const formatDate = (dateStr: string): string => {
6060
if (!dateStr) return '';
6161
const parts = dateStr.split('-');
6262
if (parts.length !== 3) return dateStr;
@@ -65,8 +65,10 @@ const formatDate = (dateStr: string): string => {
6565
const day = parseInt(parts[2], 10);
6666
if (isNaN(year) || isNaN(month) || isNaN(day)) return dateStr;
6767
try {
68-
const date = new Date(year, month - 1, day);
68+
const date = new Date(`${dateStr}T00:00:00Z`);
69+
if (isNaN(date.getTime())) return dateStr;
6970
const formatted = date.toLocaleDateString('en-US', {
71+
timeZone: 'UTC',
7072
month: 'short',
7173
day: 'numeric',
7274
year: 'numeric',

package-lock.json

Lines changed: 3 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)