Skip to content

Commit f2daee4

Browse files
authored
fix: add datetime error handling (#1167)
1 parent 1872745 commit f2daee4

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/utils/helpers.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import * as apiRequests from './api/request';
99
import {
1010
formatForDisplay,
11+
formatNotificationUpdatedAt,
1112
generateGitHubWebUrl,
1213
generateNotificationReferrerId,
1314
isEnterpriseHost,
@@ -534,5 +535,47 @@ describe('utils/helpers.ts', () => {
534535
'Not Planned Issue',
535536
);
536537
});
538+
539+
describe('formatNotificationUpdatedAt', () => {
540+
it('should use last_read_at if available', () => {
541+
const notification = {
542+
...mockSingleNotification,
543+
last_read_at: '2021-06-23T16:00:00Z',
544+
updated_at: '2021-06-23T17:00:00Z',
545+
};
546+
547+
expect(formatNotificationUpdatedAt(notification)).toContain('ago');
548+
});
549+
550+
it('should use updated_at if last_read_at is null', () => {
551+
const notification = {
552+
...mockSingleNotification,
553+
last_read_at: null,
554+
updated_at: '2021-06-23T17:00:00Z',
555+
};
556+
557+
expect(formatNotificationUpdatedAt(notification)).toContain('ago');
558+
});
559+
560+
it('should return empty if all dates are null', () => {
561+
const notification = {
562+
...mockSingleNotification,
563+
last_read_at: null,
564+
updated_at: null,
565+
};
566+
567+
expect(formatNotificationUpdatedAt(notification)).toBe('');
568+
});
569+
570+
it('should return empty if unable to parse dates', () => {
571+
const notification = {
572+
...mockSingleNotification,
573+
last_read_at: 'not an iso date',
574+
updated_at: 'not an iso date',
575+
};
576+
577+
expect(formatNotificationUpdatedAt(notification)).toBe('');
578+
});
579+
});
537580
});
538581
});

src/utils/helpers.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,13 @@ export function formatNotificationUpdatedAt(
174174
): string {
175175
const date = notification.last_read_at ?? notification.updated_at;
176176

177-
return formatDistanceToNow(parseISO(date), {
178-
addSuffix: true,
179-
});
177+
try {
178+
return formatDistanceToNow(parseISO(date), {
179+
addSuffix: true,
180+
});
181+
} catch (e) {}
182+
183+
return '';
180184
}
181185

182186
export async function openInBrowser(

0 commit comments

Comments
 (0)