feat: custom time frame is updated (#2564)

This commit is contained in:
Palash Gupta 2023-04-12 13:50:24 +05:30 committed by GitHub
parent 813eeb6d5a
commit fb1e823e6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 12 deletions

View File

@ -0,0 +1,42 @@
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import CustomDateTimeModal from './index';
describe('CustomDateTimeModal', () => {
const handleCreate = jest.fn();
const handleCancel = jest.fn();
beforeEach(() => {
render(
<CustomDateTimeModal
visible
onCreate={handleCreate}
onCancel={handleCancel}
/>,
);
});
afterEach(() => {
jest.clearAllMocks();
});
it('renders the modal with title and buttons', () => {
expect(screen.getByText('Chose date and time range')).toBeInTheDocument();
expect(screen.getByText('Apply')).toBeInTheDocument();
expect(screen.getByText('Cancel')).toBeInTheDocument();
});
it('donot calls onCreate when the Apply button is clicked without selecting dates', () => {
fireEvent.click(screen.getByText('Apply'));
expect(handleCreate).toHaveBeenCalledTimes(0);
expect(handleCreate).not.toHaveBeenCalledWith(undefined);
});
it('calls onCancel when Cancel button is clicked', () => {
fireEvent.click(screen.getByText('Cancel'));
expect(handleCancel).toHaveBeenCalledTimes(1);
});
});

View File

@ -1,4 +1,3 @@
/* eslint-disable react/jsx-no-bind */
import { DatePicker, Modal } from 'antd'; import { DatePicker, Modal } from 'antd';
import dayjs, { Dayjs } from 'dayjs'; import dayjs, { Dayjs } from 'dayjs';
import React, { useState } from 'react'; import React, { useState } from 'react';
@ -12,18 +11,21 @@ function CustomDateTimeModal({
onCreate, onCreate,
onCancel, onCancel,
}: CustomDateTimeModalProps): JSX.Element { }: CustomDateTimeModalProps): JSX.Element {
const [customDateTimeRange, setCustomDateTimeRange] = useState(); const [selectedDate, setDateTime] = useState<DateTimeRangeType>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any const onModalOkHandler = (date_time: any): void => {
function handleRangePickerOk(date_time: any): void { onCreate(date_time);
setCustomDateTimeRange(date_time); setDateTime(date_time);
} };
// eslint-disable-next-line @typescript-eslint/no-explicit-any const disabledDate = (current: Dayjs): boolean => {
function disabledDate(current: any): boolean {
const currentDay = dayjs(current); const currentDay = dayjs(current);
return currentDay.isAfter(dayjs()); return currentDay.isAfter(dayjs());
} };
const onOk = (): void => {
if (selectedDate) onCreate(selectedDate);
};
return ( return (
<Modal <Modal
@ -32,13 +34,14 @@ function CustomDateTimeModal({
okText="Apply" okText="Apply"
cancelText="Cancel" cancelText="Cancel"
onCancel={onCancel} onCancel={onCancel}
style={{ position: 'absolute', top: 60, right: 40 }} onOk={onOk}
onOk={(): void => onCreate(customDateTimeRange || null)}
> >
<RangePicker <RangePicker
disabledDate={disabledDate} disabledDate={disabledDate}
onOk={handleRangePickerOk} allowClear
onOk={onModalOkHandler}
showTime showTime
onCalendarChange={onModalOkHandler}
/> />
</Modal> </Modal>
); );