Skip to content

Commit

Permalink
Merge pull request #460 from GatherPress/mauteri-updates-4
Browse files Browse the repository at this point in the history
Added more unit tests.
  • Loading branch information
mauteri authored Dec 23, 2023
2 parents 8140dce + 28c9bba commit 2a4bd41
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/helpers/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* @return {string} The formatted start date and time for the event.
*/
export const getDateTimeStart = () => {
let dateTime = getFromGlobal('event_datetime.datetime_start');

Expand All @@ -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.
*
* @return {string} The formatted end date and time for the event.
*/
export const getDateTimeEnd = () => {
let dateTime = getFromGlobal('event_datetime.datetime_end');

Expand Down
54 changes: 53 additions & 1 deletion test/unit/js/src/helpers/datetime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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');
});

0 comments on commit 2a4bd41

Please sign in to comment.