-
Notifications
You must be signed in to change notification settings - Fork 837
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
[EuiSuperDatePicker] Allow the Absolute
tab to accept standardized ISO 8601, RFC 2822, and Unix timestamps
#7331
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f9732c5
[EuiSuperDatePicker] Allow the absolute tab to accept a few other sta…
cee-chen abb1648
[perf][refactor] Avoid fallback formats earlier if possible
cee-chen 074bd86
[misc] cleanup/nits
cee-chen c3980ca
Changelog
cee-chen b2b206f
Fix tests failing on CI due to local timezone differences :facepalm:
cee-chen 1ade9f2
[docs] Add jsdoc defaults missing in props table
cee-chen 1aa1649
Merge branch 'main' into super-date-picker/format
cee-chen a7eac5f
[misc types cleanup] Remove unnecessary `event` arg
cee-chen d8c760a
Add debounce timeout to account for user typing
cee-chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/components/date_picker/super_date_picker/date_popover/absolute_tab.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { fireEvent } from '@testing-library/react'; | ||
import { render } from '../../../../test/rtl'; | ||
|
||
import { EuiAbsoluteTab } from './absolute_tab'; | ||
|
||
// Mock EuiDatePicker - 3rd party datepicker lib causes render issues | ||
jest.mock('../../date_picker', () => ({ | ||
EuiDatePicker: () => 'EuiDatePicker', | ||
})); | ||
|
||
describe('EuiAbsoluteTab', () => { | ||
const props = { | ||
dateFormat: 'MMM D, YYYY @ HH:mm:ss.SSS', | ||
timeFormat: 'HH:mm', | ||
value: '', | ||
onChange: () => {}, | ||
roundUp: false, | ||
position: 'start' as const, | ||
labelPrefix: 'Start date', | ||
}; | ||
|
||
describe('user input', () => { | ||
it('parses the passed `dateFormat` prop', () => { | ||
const { getByTestSubject } = render( | ||
<EuiAbsoluteTab {...props} dateFormat="MMM Do YY" /> | ||
); | ||
const input = getByTestSubject('superDatePickerAbsoluteDateInput'); | ||
|
||
fireEvent.change(input, { | ||
target: { value: 'Jan 31st 01' }, | ||
}); | ||
expect(input).not.toBeInvalid(); | ||
expect(input).toHaveValue('Jan 31st 01'); | ||
}); | ||
|
||
describe('allows several other common date formats, and autoformats them to the `dateFormat` prop', () => { | ||
const assertOutput = (input: HTMLInputElement) => { | ||
// Exclude hours from assertion, because moment uses local machine timezone | ||
expect(input.value).toContain('Jan 1, 1970'); | ||
}; | ||
|
||
test('ISO 8601', () => { | ||
const { getByTestSubject } = render(<EuiAbsoluteTab {...props} />); | ||
const input = getByTestSubject('superDatePickerAbsoluteDateInput'); | ||
|
||
fireEvent.change(input, { | ||
target: { value: '1970-01-01T12:00:00+00:00' }, | ||
}); | ||
expect(input).not.toBeInvalid(); | ||
assertOutput(input as HTMLInputElement); | ||
}); | ||
|
||
test('RFC 2822', () => { | ||
const { getByTestSubject } = render(<EuiAbsoluteTab {...props} />); | ||
const input = getByTestSubject('superDatePickerAbsoluteDateInput'); | ||
|
||
fireEvent.change(input, { | ||
target: { value: 'Thu, 1 Jan 1970 12:00:00 +0000' }, | ||
}); | ||
expect(input).not.toBeInvalid(); | ||
assertOutput(input as HTMLInputElement); | ||
}); | ||
|
||
test('unix timestamp', () => { | ||
const { getByTestSubject } = render(<EuiAbsoluteTab {...props} />); | ||
const input = getByTestSubject('superDatePickerAbsoluteDateInput'); | ||
|
||
fireEvent.change(input, { target: { value: Date.now().toString() } }); | ||
expect(input).not.toBeInvalid(); | ||
|
||
fireEvent.change(input, { target: { value: '43200' } }); | ||
expect(input).not.toBeInvalid(); | ||
assertOutput(input as HTMLInputElement); | ||
}); | ||
}); | ||
|
||
it('flags all other date formats as invalid', () => { | ||
const { getByTestSubject } = render(<EuiAbsoluteTab {...props} />); | ||
const input = getByTestSubject('superDatePickerAbsoluteDateInput'); | ||
|
||
fireEvent.change(input, { target: { value: '01-01-1970' } }); | ||
expect(input).toHaveValue('01-01-1970'); | ||
expect(input).toBeInvalid(); | ||
|
||
fireEvent.change(input, { target: { value: 'asdfasdf' } }); | ||
expect(input).toHaveValue('asdfasdf'); | ||
expect(input).toBeInvalid(); | ||
|
||
fireEvent.change(input, { target: { value: '' } }); | ||
expect(input).toHaveValue(''); | ||
expect(input).toBeInvalid(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- For greater flexibility, `EuiSuperDatePicker` now allows users to paste ISO 8601, RFC 2822, and Unix timestamps in the `Absolute` tab input, in addition to timestamps in the `dateFormat` prop |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we also include
'x'
here to support integer millisecond-precision timestamps? I'd say they're as common as floating point timestamps, and supporting them both when using moment is really easyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried adding both, and unfortunately Moment doesn't seem to be able to handle it :( It errors on one or the other depending on the order in which you put them in the array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no, that's unfortunate :( Let's leave it as is then!