-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: improve datetime widget (#7261)
- don't display the current date by default or when the field is empty (#3679) - **potentially breaking change** - add `default: '{{now}}'` option to enable the current behavior of displaying the current time (using the specified format) - add UTC indicator when `picker_utc: true` - improve how `Z` in format and `picker_utc: true` work together - reorder format importance: if `format` is set, `date_format` and `time_format` (if strings) are ignored (#7250) BREAKING CHANGE: The datetime field is empty by default, from now on, but it was prefilled with the current date until now. Use `default: '{{now}}'` to prefill the field with the current date.
- Loading branch information
1 parent
f16f736
commit 94993be
Showing
11 changed files
with
133 additions
and
50 deletions.
There are no files selected for viewing
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
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
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
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
60 changes: 60 additions & 0 deletions
60
packages/decap-cms-widget-datetime/src/__tests__/DateTimeControl.spec.js
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,60 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import dayjs from 'dayjs'; | ||
|
||
import DateTimeControl from '../DateTimeControl'; | ||
|
||
function setup(propsOverrides = {}) { | ||
const props = { | ||
forID: 'test-datetime', | ||
onChange: jest.fn(), | ||
classNameWrapper: 'classNameWrapper', | ||
setActiveStyle: jest.fn(), | ||
setInactiveStyle: jest.fn(), | ||
value: '', | ||
t: key => key, | ||
isDisabled: false, | ||
field: { | ||
get: jest.fn().mockReturnValue('DD.MM.YYYY'), | ||
}, | ||
...propsOverrides, | ||
}; | ||
|
||
const utils = render(<DateTimeControl {...props} />); | ||
const input = utils.getByTestId('test-datetime'); | ||
const nowButton = utils.getByTestId('now-button'); | ||
const clearButton = utils.getByTestId('clear-button'); | ||
|
||
return { | ||
...utils, | ||
props, | ||
input, | ||
nowButton, | ||
clearButton, | ||
}; | ||
} | ||
|
||
describe('DateTimeControl', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('renders the component with input, now button, and clear button', () => { | ||
const { getByTestId } = setup(); | ||
expect(getByTestId('test-datetime')).toBeInTheDocument(); | ||
expect(getByTestId('now-button')).toBeInTheDocument(); | ||
expect(getByTestId('clear-button')).toBeInTheDocument(); | ||
}); | ||
|
||
test('set value to current date if now button is clicked', () => { | ||
const { nowButton, props } = setup(); | ||
fireEvent.click(nowButton); | ||
expect(props.onChange).toHaveBeenCalledWith(dayjs().format('DD.MM.YYYY')); | ||
}); | ||
|
||
test('set value to empty string if clear button is clicked', () => { | ||
const { clearButton, props } = setup({ value: '1970-01-01' }); | ||
fireEvent.click(clearButton); | ||
expect(props.onChange).toHaveBeenCalledWith(''); | ||
}); | ||
}); |