#15650 UI: Not able to select all possible dates range from the custom date picker in the incident manager (#15711)

* #15650 UI: Not able to select all possible dates range from the custom date picker in the incident manager

* added unit test
This commit is contained in:
Shailesh Parmar 2024-03-27 17:52:28 +05:30 committed by GitHub
parent bf0ec44f4b
commit 84941a7d32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 6 deletions

View File

@ -16,7 +16,6 @@ import { Button, DatePicker, Dropdown, MenuProps, Space } from 'antd';
import { RangePickerProps } from 'antd/lib/date-picker';
import { isUndefined, pick } from 'lodash';
import { DateFilterType, DateRangeObject } from 'Models';
import moment from 'moment';
import { MenuInfo } from 'rc-menu/lib/interface';
import React, { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
@ -55,7 +54,7 @@ const DatePickerMenu = ({
const { menuOptions, defaultOptions } = useMemo(() => {
const defaultOptions = pick(DEFAULT_SELECTED_RANGE, ['title', 'key']);
if (defaultDateRange && defaultDateRange.key) {
if (defaultDateRange?.key) {
defaultOptions.key = defaultDateRange.key;
if (defaultDateRange.key === 'customRange' && defaultDateRange.title) {
defaultOptions.title = defaultDateRange.title;
@ -163,10 +162,6 @@ const DatePickerMenu = ({
<DatePicker.RangePicker
bordered={false}
clearIcon={<CloseCircleOutlined />}
defaultValue={[
moment(defaultDateRange?.startTs),
moment(defaultDateRange?.endTs),
]}
format={(value) => value.utc().format('YYYY-MM-DD')}
open={isMenuOpen}
placement="bottomRight"

View File

@ -0,0 +1,70 @@
/*
* Copyright 2024 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { render, screen } from '@testing-library/react';
import React from 'react';
import DatePickerMenu from './DatePickerMenu.component';
jest.mock('../../../utils/DatePickerMenuUtils', () => ({
getDaysCount: jest.fn().mockReturnValue(3),
getTimestampLabel: jest.fn().mockReturnValue('custom range'),
}));
jest.mock('../../../constants/profiler.constant', () => ({
DEFAULT_SELECTED_RANGE: {
key: 'last3days',
title: 'Last 3 days',
days: 3,
},
PROFILER_FILTER_RANGE: {
last3days: {
days: 3,
title: 'Last 3 days',
},
last7days: {
days: 7,
title: 'Last 7 days',
},
},
}));
jest.mock('../../../utils/date-time/DateTimeUtils', () => ({
getCurrentMillis: jest.fn().mockReturnValue(1711583974000),
getEpochMillisForPastDays: jest.fn().mockReturnValue(1709424034000),
}));
describe('DatePickerMenu', () => {
const mockHandleDateRangeChange = jest.fn();
const mockHandleSelectedTimeRange = jest.fn();
it('should render component with default options', () => {
render(
<DatePickerMenu
handleDateRangeChange={mockHandleDateRangeChange}
handleSelectedTimeRange={mockHandleSelectedTimeRange}
/>
);
expect(screen.getByText('Last 3 days')).toBeInTheDocument();
});
it('should show default date range if provided via props', () => {
render(
<DatePickerMenu
defaultDateRange={{ key: 'customRange', title: 'Default Range' }}
handleDateRangeChange={mockHandleDateRangeChange}
handleSelectedTimeRange={mockHandleSelectedTimeRange}
/>
);
expect(screen.getByText('Default Range')).toBeInTheDocument();
});
});