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

fix(DGHT-180): fix InputDateTimeRangePicker gets wrong error message for multiple errors #5388

Merged
merged 1 commit into from
Aug 12, 2024
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
5 changes: 5 additions & 0 deletions .changeset/eighty-dryers-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@talend/react-components': patch
---

fix InputDateTimeRangePicker gets wrong error message for multiple errors
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function DateTimeRangeManager(props) {
endDateTime,
};
const [state, setState] = useState(initialState);
const [startDateErrors, setStartDateErrors] = useState([]);
const [endDateErrors, setEndDateErrors] = useState([]);

useEffect(() => {
if (!isEqual(state.startDateTime, startDateTime) || !isEqual(state.endDateTime, endDateTime)) {
Expand All @@ -29,7 +31,12 @@ function DateTimeRangeManager(props) {
function onRangeChange(event, nextState, origin) {
const errors = [...(nextState.errors || [])];

if (nextState.startDateTime && nextState.endDateTime) {
if (
nextState.startDateTime &&
nextState.endDateTime &&
!isNaN(nextState.startDateTime) &&
!isNaN(nextState.endDateTime)
) {
if (!isBefore(nextState.startDateTime, nextState.endDateTime)) {
errors.push(
new DateTimeRangePickerException(
Expand All @@ -49,13 +56,17 @@ function DateTimeRangeManager(props) {
}

function onStartChange(event, { datetime, errors }) {
const nextState = { ...state, startDateTime: datetime, errors };
setStartDateErrors(errors);
const allErrors = [...(errors || []), ...(endDateErrors || [])];
const nextState = { ...state, startDateTime: datetime, errors: allErrors };
setState(nextState);
onRangeChange(event, nextState, 'RANGE_START');
}

function onEndChange(event, { datetime, errors }) {
const nextState = { ...state, endDateTime: datetime, errors };
setEndDateErrors(errors);
const allErrors = [...(startDateErrors || []), ...(errors || [])];
const nextState = { ...state, endDateTime: datetime, errors: allErrors };
setState(nextState);
onRangeChange(event, nextState, 'RANGE_END');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen, within } from '@testing-library/react';
import { fireEvent, render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import InputDateTimeRangePicker from './InputDateTimeRangePicker.component';
Expand Down Expand Up @@ -75,4 +75,54 @@ describe('InputDateTimeRangePicker', () => {
new Date(new Date(today.getFullYear(), today.getMonth(), 13, 10, 0, 0)),
);
});
it('should show correct error message', async () => {
const TIME_ERROR = 'Time is required';
const DATE_ERROR = 'Date is required';
const user = userEvent.setup();

// GIVEN render with a default start time and end time
const onChange = jest.fn();
render(
<InputDateTimeRangePicker
id="my-picker"
onChange={onChange}
startDateTime="2024-08-10 00:00:00"
endDateTime="2024-08-12 23:59:59"
useSeconds
/>,
);
const start = screen.getByTestId('range-start');
const end = screen.getByTestId('range-end');

// WHEN remove the date from end time
const endDateControl = within(end).getByTestId('date-picker');
await user.click(endDateControl);
await user.clear(endDateControl);
fireEvent.blur(endDateControl);
// THEN should get missing date error for end date
const payload0 = onChange.mock.calls[0][1];
expect(payload0.errors.length).toBe(1);
expect(payload0.errorMessage).toBe(DATE_ERROR);

// WHEN remove the time from start time
const startTimeControl = within(start).getByTestId('time-picker');
await user.click(startTimeControl);
await user.clear(startTimeControl);
fireEvent.blur(startTimeControl);
// THEN should get missing time error for start time
const payload1 = onChange.mock.calls[1][1];
expect(payload1.errors.length).toBe(2);
expect(payload1.errors[0].message).toBe(TIME_ERROR);
expect(payload1.errors[1].message).toBe(DATE_ERROR);
expect(payload1.errorMessage).toBe(TIME_ERROR);

// WHEN input valid time for start time
await user.click(startTimeControl);
await user.type(startTimeControl, '08:20:10');
fireEvent.blur(startTimeControl);
// THEN should get the remaining date error
const payload2 = onChange.mock.calls[2][1];
expect(payload2.errors.length).toBe(1);
expect(payload2.errorMessage).toBe(DATE_ERROR);
});
});
Loading