From 43134f5df65e0ba8b06c19e52396a70c657ce105 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Tue, 20 Feb 2024 16:42:26 -0400 Subject: [PATCH] test(helper): addDate and searchQueryString --- src/utils/helpers.test.ts | 29 +++++++++++++++++++++++++++++ src/utils/helpers.ts | 21 +++++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index ebaa908e7..7ccfada94 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -5,6 +5,8 @@ import { getCommentId, getLatestDiscussionCommentId, isEnterpriseHost, + addHours, + formatSearchQueryString, } from './helpers'; import { mockedSingleNotification, @@ -147,4 +149,31 @@ describe('utils/helpers.ts', () => { expect(result).toBe('https://github.manos.im/api/v3/'); }); }); + + describe('addHours', () => { + // Example test using Jest + test('adds hours correctly for positive values', () => { + const result = addHours('2024-02-20T12:00:00.000Z', 3); + expect(result).toBe('2024-02-20T15:00:00.000Z'); + }); + + test('adds hours correctly for negative values', () => { + const result = addHours('2024-02-20T12:00:00.000Z', -2); + expect(result).toBe('2024-02-20T10:00:00.000Z'); + }); + }); + + describe('formatSearchQueryString', () => { + test('formats search query string correctly', () => { + const result = formatSearchQueryString( + 'exampleRepo', + 'exampleTitle', + '2024-02-20T12:00:00.000Z', + ); + + expect(result).toBe( + `exampleTitle in:title repo:exampleRepo updated:>2024-02-20T10:00:00.000Z`, + ); + }); + }); }); diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index e84fd3a56..fb71af3de 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -65,13 +65,22 @@ export function generateGitHubWebUrl( return newUrl + comment; } -const addHours = (date: string, hours: number) => - new Date(new Date(date).getTime() + hours * 36e5).toISOString(); +export function addHours(date: string, hours: number): string { + return new Date(new Date(date).getTime() + hours * 36e5).toISOString(); +} -const queryString = (repo: string, title: string, lastUpdated: string) => - `${title} in:title repo:${repo} updated:>${addHours(lastUpdated, -2)}`; +export function formatSearchQueryString( + repo: string, + title: string, + lastUpdated: string, +): string { + return `${title} in:title repo:${repo} updated:>${addHours(lastUpdated, -2)}`; +} -async function getReleaseTagWebUrl(notification: Notification, token: string) { +export async function getReleaseTagWebUrl( + notification: Notification, + token: string, +) { const response = await apiRequestAuth(notification.subject.url, 'GET', token); return { @@ -89,7 +98,7 @@ async function getDiscussionUrl( token, { query: `{ - search(query:"${queryString( + search(query:"${formatSearchQueryString( notification.repository.full_name, notification.subject.title, notification.updated_at,