From de6b90244af9dcb870d6816ec12516afe837c0b9 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sat, 1 Jun 2024 08:43:27 -0500 Subject: [PATCH 1/3] fix: catch date formatting errors --- src/utils/helpers.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 310213a87..4bf435ecf 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -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( From 0f6a56e06572b1980b0dfa7cd80ed27c163f7f37 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sat, 1 Jun 2024 08:43:55 -0500 Subject: [PATCH 2/3] fix: catch date formatting errors --- src/utils/helpers.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 4bf435ecf..4548c02f7 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -178,9 +178,9 @@ export function formatNotificationUpdatedAt( return formatDistanceToNow(parseISO(date), { addSuffix: true, }); - } catch (e) { - return ''; - } + } catch (e) {} + + return ''; } export async function openInBrowser( From db27dad53bf73aec1b4c60cb2c47449692a224dd Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sat, 1 Jun 2024 09:14:36 -0500 Subject: [PATCH 3/3] add tests --- src/utils/helpers.test.ts | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index a55a4f25c..e989213a7 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -8,6 +8,7 @@ import { import * as apiRequests from './api/request'; import { formatForDisplay, + formatNotificationUpdatedAt, generateGitHubWebUrl, generateNotificationReferrerId, isEnterpriseHost, @@ -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(''); + }); + }); }); });