Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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