From acefa19783c139a9cd495d7cce09629cd833300e Mon Sep 17 00:00:00 2001 From: Mike Auteri Date: Sat, 23 Dec 2023 15:45:29 -0500 Subject: [PATCH 1/2] Added more unit tests. --- src/helpers/datetime.js | 14 ++++++ test/unit/js/src/helpers/datetime.test.js | 54 ++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/helpers/datetime.js b/src/helpers/datetime.js index 4cf9a5c56..546bd3a1d 100644 --- a/src/helpers/datetime.js +++ b/src/helpers/datetime.js @@ -145,6 +145,13 @@ export const defaultDateTimeEnd = moment .add(2, 'hours') .format(dateTimeMomentFormat); +/** + * Retrieves the start date and time for an event, formatted based on the plugin's timezone. + * If the start date and time is not set, it defaults to a predefined value. + * The formatted datetime is then stored in the global settings for future access. + * + * @returns {string} The formatted start date and time for the event. + */ export const getDateTimeStart = () => { let dateTime = getFromGlobal('event_datetime.datetime_start'); @@ -158,6 +165,13 @@ export const getDateTimeStart = () => { return dateTime; }; +/** + * Retrieves the end date and time for an event, formatted based on the plugin's timezone. + * If the end date and time is not set, it defaults to a predefined value. + * The formatted datetime is then stored in the global settings for future access. + * + * @returns {string} The formatted end date and time for the event. + */ export const getDateTimeEnd = () => { let dateTime = getFromGlobal('event_datetime.datetime_end'); diff --git a/test/unit/js/src/helpers/datetime.test.js b/test/unit/js/src/helpers/datetime.test.js index 2399eef20..478cee9dd 100644 --- a/test/unit/js/src/helpers/datetime.test.js +++ b/test/unit/js/src/helpers/datetime.test.js @@ -7,11 +7,13 @@ import { expect, test } from '@jest/globals'; * Internal dependencies. */ import { + getDateTimeEnd, + getDateTimeStart, getTimeZone, getUtcOffset, maybeConvertUtcOffsetForDatabase, - maybeConvertUtcOffsetForSelect, maybeConvertUtcOffsetForDisplay, + maybeConvertUtcOffsetForSelect, } from '../../../../../src/helpers/datetime'; /** @@ -90,6 +92,24 @@ test('maybeConvertUtcOffsetForDatabase converts UTC-1.75 to correct format', () expect(maybeConvertUtcOffsetForDatabase(offset)).toBe('-01:45'); }); +test('maybeConvertUtcOffsetForDatabase converts UTC-1.75 to correct format', () => { + const offset = 'UTC-1.75'; + + expect(maybeConvertUtcOffsetForDatabase(offset)).toBe('-01:45'); +}); + +test('maybeConvertUtcOffsetForDatabase converts UTC+12 to correct format', () => { + const offset = 'UTC+12'; + + expect(maybeConvertUtcOffsetForDatabase(offset)).toBe('+12:00'); +}); + +test('maybeConvertUtcOffsetForDatabase does not convert default empty argument', () => { + const offset = ''; + + expect(maybeConvertUtcOffsetForDatabase(offset)).toBe(''); +}); + /** * Coverage for maybeConvertUtcOffsetForSelect. */ @@ -116,3 +136,35 @@ test('maybeConvertUtcOffsetForSelect does not convert non-pattern', () => { expect(maybeConvertUtcOffsetForSelect(offset)).toBe('UTC'); }); + +test('maybeConvertUtcOffsetForSelect does not convert non-pattern (default empty argument)', () => { + const offset = ''; + + expect(maybeConvertUtcOffsetForSelect(offset)).toBe(''); +}); + +/** + * Coverage for getDateTimeStart. + */ +test('getDateTimeStart converts format of date/time start from global', () => { + global.GatherPress = { + event_datetime: { + datetime_start: '2023-12-28 12:26:00', + }, + }; + + expect(getDateTimeStart()).toBe('2023-12-28T12:26:00'); +}); + +/** + * Coverage for getDateTimeEnd. + */ +test('getDateTimeEnd converts format of date/time end from global', () => { + global.GatherPress = { + event_datetime: { + datetime_end: '2023-12-28 12:26:00', + }, + }; + + expect(getDateTimeEnd()).toBe('2023-12-28T12:26:00'); +}); From 28c9bbaf871dec62b7caaebb875e120d96a84610 Mon Sep 17 00:00:00 2001 From: Mike Auteri Date: Sat, 23 Dec 2023 15:49:02 -0500 Subject: [PATCH 2/2] Fix linting. --- src/helpers/datetime.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/datetime.js b/src/helpers/datetime.js index 546bd3a1d..d17642b17 100644 --- a/src/helpers/datetime.js +++ b/src/helpers/datetime.js @@ -150,7 +150,7 @@ export const defaultDateTimeEnd = moment * If the start date and time is not set, it defaults to a predefined value. * The formatted datetime is then stored in the global settings for future access. * - * @returns {string} The formatted start date and time for the event. + * @return {string} The formatted start date and time for the event. */ export const getDateTimeStart = () => { let dateTime = getFromGlobal('event_datetime.datetime_start'); @@ -170,7 +170,7 @@ export const getDateTimeStart = () => { * If the end date and time is not set, it defaults to a predefined value. * The formatted datetime is then stored in the global settings for future access. * - * @returns {string} The formatted end date and time for the event. + * @return {string} The formatted end date and time for the event. */ export const getDateTimeEnd = () => { let dateTime = getFromGlobal('event_datetime.datetime_end');