Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more unit tests. #460

Merged
merged 2 commits into from
Dec 23, 2023
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
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');
});
Loading