Skip to content

Commit

Permalink
fix: add datetime error handling (#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy authored Jun 2, 2024
1 parent 1872745 commit f2daee4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
43 changes: 43 additions & 0 deletions src/utils/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import * as apiRequests from './api/request';
import {
formatForDisplay,
formatNotificationUpdatedAt,
generateGitHubWebUrl,
generateNotificationReferrerId,
isEnterpriseHost,
Expand Down Expand Up @@ -534,5 +535,47 @@ describe('utils/helpers.ts', () => {
'Not Planned Issue',
);
});

describe('formatNotificationUpdatedAt', () => {
it('should use last_read_at if available', () => {
const notification = {
...mockSingleNotification,
last_read_at: '2021-06-23T16:00:00Z',
updated_at: '2021-06-23T17:00:00Z',
};

expect(formatNotificationUpdatedAt(notification)).toContain('ago');
});

it('should use updated_at if last_read_at is null', () => {
const notification = {
...mockSingleNotification,
last_read_at: null,
updated_at: '2021-06-23T17:00:00Z',
};

expect(formatNotificationUpdatedAt(notification)).toContain('ago');
});

it('should return empty if all dates are null', () => {
const notification = {
...mockSingleNotification,
last_read_at: null,
updated_at: null,
};

expect(formatNotificationUpdatedAt(notification)).toBe('');
});

it('should return empty if unable to parse dates', () => {
const notification = {
...mockSingleNotification,
last_read_at: 'not an iso date',
updated_at: 'not an iso date',
};

expect(formatNotificationUpdatedAt(notification)).toBe('');
});
});
});
});
10 changes: 7 additions & 3 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ export function formatNotificationUpdatedAt(
): string {
const date = notification.last_read_at ?? notification.updated_at;

return formatDistanceToNow(parseISO(date), {
addSuffix: true,
});
try {
return formatDistanceToNow(parseISO(date), {
addSuffix: true,
});
} catch (e) {}

return '';
}

export async function openInBrowser(
Expand Down

0 comments on commit f2daee4

Please sign in to comment.