DateTimePicker: Fixes issue with date picker showing invalid date (#97888)

* DateTimePicker: Fixes issue with date picker showing invalid date

* Fix lint
This commit is contained in:
Torkel Ödegaard 2024-12-13 19:23:26 +01:00 committed by GitHub
parent 0a9852c64b
commit d93a5a7c53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import { fireEvent, render, RenderResult } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { dateTimeParse, TimeRange } from '@grafana/data';
import { dateTimeParse, systemDateFormats, TimeRange } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { TimeRangeContent } from './TimeRangeContent';
@ -93,6 +93,34 @@ describe('TimeRangeForm', () => {
expect(getByLabelText('To')).toHaveValue('2021-06-19 19:59:00');
});
describe('Given custom system date format', () => {
const originalFullDate = systemDateFormats.fullDate;
beforeEach(() => {
systemDateFormats.fullDate = 'DD.MM.YYYY HH:mm:ss';
});
afterAll(() => {
systemDateFormats.fullDate = originalFullDate;
});
it('should parse UTC iso strings and render in current timezone', () => {
const { getByLabelText } = setup(
{
from: defaultTimeRange.from,
to: defaultTimeRange.to,
raw: {
from: defaultTimeRange.from.toISOString(),
to: defaultTimeRange.to.toISOString(),
},
},
'America/New_York'
);
expect(getByLabelText('From')).toHaveValue('16.06.2021 20:00:00');
expect(getByLabelText('To')).toHaveValue('19.06.2021 19:59:00');
});
});
it('should close calendar when clicking the close icon', () => {
const { queryByLabelText, getAllByRole, getByRole } = setup();
const { TimePicker } = selectors.components;

View File

@ -269,7 +269,7 @@ function valueAsString(value: DateTime | string, timeZone?: TimeZone): string {
}
if (value.endsWith('Z')) {
const dt = dateTimeParse(value, { timeZone: 'utc' });
const dt = dateTimeParse(value, { timeZone: 'utc', format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' });
return dateTimeFormat(dt, { timeZone });
}