Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng authored and timopollmeier committed Jul 19, 2024
1 parent 6ce68be commit 559ef88
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 23 deletions.
20 changes: 15 additions & 5 deletions src/web/pages/performance/__tests__/startendtimeselection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import StartTimeSelection from '../startendtimeselection';

const timezone = 'CET';
const startDate = MomentDate('2019-01-01T12:00Z').tz(timezone);
const endDate = MomentDate('2019-01-01T13:00Z').tz(timezone);
const endDate = MomentDate('2019-02-01T13:00Z').tz(timezone);

const handleChange = testing.fn();

Expand All @@ -45,24 +45,34 @@ describe('StartTimeSelection tests', () => {
labelText,
buttonName,
buttonContent,
timePickerLabel,
timePickerValue,
) => {
const label = screen.getByLabelText(labelText);
expect(label).toBeVisible();

const button = screen.getByRole('button', {name: buttonName});
expect(button).toBeVisible();
expect(button).toHaveTextContent(buttonContent);

const timePicker = screen.getByLabelText(timePickerLabel);
expect(timePicker).toBeVisible();
expect(timePicker).toHaveValue(timePickerValue);
};

checkElementVisibilityAndContent(
'Start Date',
'01/01/2019',
'01/01/2019',
'Start Time',
'Jan 01, 2019, 01:00:00 PM',
'Jan 01, 2019, 01:00:00 PM',
'13:00',
);
checkElementVisibilityAndContent(
'End Date',
'01/02/2019',
'01/02/2019',
'End Time',
'Jan 01, 2019, 02:00:00 PM',
'Jan 01, 2019, 02:00:00 PM',
'14:00',
);
});

Expand Down
6 changes: 3 additions & 3 deletions src/web/pages/performance/startendtimeselection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {TimePicker} from '@greenbone/opensight-ui-components';

import Column from 'web/components/layout/column';
import Row from 'web/components/layout/row';
import {formatTimeForTimePicker} from 'web/utils/timePickerHelpers';

const StartTimeSelection = props => {
const {
Expand All @@ -39,11 +40,10 @@ const StartTimeSelection = props => {
const [startDate, setStartDate] = useState(initialStartDate);
const [endDate, setEndDate] = useState(initialEndDate);
const [startTime, setStartTime] = useState(
`${startDate.hours().toString().padStart(2, '0')}:${startDate.minutes().toString().padStart(2, '0')}`,
formatTimeForTimePicker(initialStartDate),
);

const [endTime, setEndTime] = useState(
`${endDate.hours().toString().padStart(2, '0')}:${endDate.minutes().toString().padStart(2, '0')}`,
formatTimeForTimePicker(initialEndDate),
);

useEffect(() => {
Expand Down
28 changes: 19 additions & 9 deletions src/web/pages/schedules/__tests__/dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ const checkElementVisibilityAndContent = (
labelText,
buttonName,
buttonContent,
timePickerLabel,
timePickerValue,
) => {
const label = screen.getByLabelText(labelText);
expect(label).toBeVisible();

const button = screen.getByRole('button', {name: buttonName});
expect(button).toBeVisible();
expect(button).toHaveTextContent(buttonContent);
const button = screen.getAllByRole('button', {name: buttonName});
expect(button[0]).toBeVisible();
expect(button[0]).toHaveTextContent(buttonContent);

const timePicker = screen.getByLabelText(timePickerLabel);
expect(timePicker).toBeVisible();
expect(timePicker).toHaveValue(timePickerValue);
};

import {
Expand Down Expand Up @@ -120,14 +126,18 @@ describe('ScheduleDialog component tests', () => {
expect(defaultTimezone).toHaveValue('Coordinated Universal Time/UTC');

checkElementVisibilityAndContent(
'First Run',
'Feb 08, 2021, 04:00:00 PM',
'Feb 08, 2021, 04:00:00 PM',
'Start Date',
'08/02/2021',
'08/02/2021',
'Start Time',
'15:00',
);
checkElementVisibilityAndContent(
'End Run',
'Feb 08, 2021, 08:45:00 PM',
'Feb 08, 2021, 08:45:00 PM',
'End Date',
'08/02/2021',
'08/02/2021',
'End Time',
'19:45',
);

expect(baseElement).toHaveTextContent('5 hours');
Expand Down
7 changes: 3 additions & 4 deletions src/web/pages/schedules/dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import TimeUnitSelect from './timeunitselect';
import WeekDaySelect, {WeekDaysPropType} from './weekdayselect';
import DaySelect from './dayselect';
import MonthDaysSelect from './monthdaysselect';
import {formatTimeForTimePicker} from 'web/utils/timePickerHelpers';

const RECURRENCE_ONCE = 'once';
const RECURRENCE_HOURLY = ReccurenceFrequency.HOURLY;
Expand Down Expand Up @@ -87,7 +88,7 @@ const ScheduleDialog = ({
const [startDate, setStartDate] = useState(initialStartDate);

const [startTime, setStartTime] = useState(
`${startDate.hours().toString().padStart(2, '0')}:${startDate.minutes().toString().padStart(2, '0')}`,
formatTimeForTimePicker(startDate),
);

const [endOpen, setEndOpen] = useState(!isDefined(duration));
Expand All @@ -97,9 +98,7 @@ const ScheduleDialog = ({
: initialStartDate.clone().add(1, 'hour'),
);

const [endTime, setEndTime] = useState(
`${endDate.hours().toString().padStart(2, '0')}:${endDate.minutes().toString().padStart(2, '0')}`,
);
const [endTime, setEndTime] = useState(formatTimeForTimePicker(endDate));

const [timezone, setTimezone] = useState(initialTimezone);

Expand Down
56 changes: 56 additions & 0 deletions src/web/utils/__tests__/timePickerHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {formatSplitTime, formatTimeForTimePicker} from '../timePickerHelpers';
import {describe, test, expect} from '@gsa/testing';

describe('timePickerHelpers', () => {
describe('formatSplitTime', () => {
test('formats single-digit hours and minutes correctly', () => {
expect(formatSplitTime(1, 5)).toBe('01:05');
});

test('formats double-digit hours and minutes correctly', () => {
expect(formatSplitTime(12, 30)).toBe('12:30');
});

test('handles hours and minutes at the edge of valid ranges', () => {
expect(formatSplitTime(23, 59)).toBe('23:59');
});

test('returns a string in HH:MM format', () => {
const result = formatSplitTime(9, 15);
expect(result).toMatch(/^\d{2}:\d{2}$/);
});
});

describe('formatTimeForTimePicker', () => {
test('formats time correctly for a given date object', () => {
// Mock date object with hours and minutes methods
const mockDate = {
hours: () => 9,
minutes: () => 30,
};
const expected = '09:30';
const result = formatTimeForTimePicker(mockDate);
expect(result).toBe(expected);
});

test('pads single-digit hours and minutes with leading zeros', () => {
const mockDate = {
hours: () => 5,
minutes: () => 4,
};
const expected = '05:04';
const result = formatTimeForTimePicker(mockDate);
expect(result).toBe(expected);
});

test('handles times at the edge of valid ranges', () => {
const mockDate = {
hours: () => 23,
minutes: () => 59,
};
const expected = '23:59';
const result = formatTimeForTimePicker(mockDate);
expect(result).toBe(expected);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ export const formatSplitTime = (start_hour, start_minute) => {
const formattedStartMinute = start_minute.toString().padStart(2, '0');
return `${formattedStartHour}:${formattedStartMinute}`;
};

export const formatTimeForTimePicker = date =>
`${date.hours().toString().padStart(2, '0')}:${date.minutes().toString().padStart(2, '0')}`;
8 changes: 7 additions & 1 deletion src/web/wizard/__tests__/advancedtaskwizard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,20 @@ describe('AdvancedTaskWizard component tests', () => {
const radioInputs = getRadioInputs();
const radioTitles = getRadioTitles();

const selectedDate = 'Jan 01, 2020, 01:10:00 PM';
const selectedDate = '01/01/2020';
const datePickerLabel = screen.getByLabelText('Start Date');
const startDateButton = screen.getByRole('button', {name: selectedDate});

const selectedTime = '12:10';
const timePickerLabel = screen.getByLabelText('Start Time');

expect(startDateButton).toBeVisible();
expect(datePickerLabel).toBeVisible();
expect(startDateButton).toHaveTextContent(selectedDate);

expect(timePickerLabel).toBeVisible();
expect(timePickerLabel).toHaveValue(selectedTime);

expect(formGroups[0]).toHaveTextContent('Task Name');

expect(formGroups[1]).toHaveTextContent('Scan Config');
Expand Down
8 changes: 7 additions & 1 deletion src/web/wizard/__tests__/modifytaskwizard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ describe('ModifyTaskWizard component tests', () => {
const radioInputs = getRadioInputs();
const radioTitles = getRadioTitles();

const selectedDate = 'Jan 01, 2020, 01:10:00 PM';
const selectedDate = '01/01/2020';
const datePickerLabel = screen.getByLabelText('Start Date');
const startDateButton = screen.getByRole('button', {name: selectedDate});

const selectedTime = '12:10';
const timePickerLabel = screen.getByLabelText('Start Time');

expect(startDateButton).toBeVisible();
expect(datePickerLabel).toBeVisible();
expect(startDateButton).toHaveTextContent(selectedDate);

expect(timePickerLabel).toBeVisible();
expect(timePickerLabel).toHaveValue(selectedTime);

expect(baseElement).toHaveTextContent('Setting a start time');
expect(baseElement).toHaveTextContent('Setting an email Address');

Expand Down

0 comments on commit 559ef88

Please sign in to comment.