-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor DatePicker component to react hooks and function component (#…
…36835) * Refactor DatePicker component to use react hooks and change it from class component to function component. It was already done as a part of different PR #22897. After code review it looks like the original author is not able to find time to reply or address review comments. I'm doing takeover from here and will take care of PR until it gets merged. * add changelog entry * add `onMonthPreviewed` in README.md * Remove `getMomentDate` test case Since in real word scenario, in UI we never deal with `getMomentDate` directly so in the test case we cannot write some event or simulate any behaviour that will give access to `getMomentDate` method. If we take help of `currentDate` props then test cases are already in the place. * Fix `onChangeMoment` test cases with help of `onDateChange` props * Move `getMomentDate` to a separate `utils.js` file - moved the `getMomentDate` tests under a new file, eg `test/utils.js` file - changed the `getMomentDate` unit tests to avoid rendering `DatePicker` and using enzyme * Remove extra spaces from the changelog file Co-authored-by: Marco Ciampini <[email protected]> * Descriptive documentation on `onMonthPreviewed ` props Co-authored-by: Marco Ciampini <[email protected]> Co-authored-by: Marco Ciampini <[email protected]>
- Loading branch information
1 parent
2b35b60
commit 92923c6
Showing
6 changed files
with
128 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import moment from 'moment'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getMomentDate } from '../utils'; | ||
|
||
describe( 'getMomentDate', () => { | ||
it( 'should return a Moment object representing a given date string', () => { | ||
const currentDate = '1986-10-18T23:00:00'; | ||
const momentDate = getMomentDate( currentDate ); | ||
|
||
expect( moment.isMoment( momentDate ) ).toBe( true ); | ||
expect( momentDate.isSame( moment( currentDate ) ) ).toBe( true ); | ||
} ); | ||
|
||
it( 'should return null when given a null argument', () => { | ||
const currentDate = null; | ||
const momentDate = getMomentDate( currentDate ); | ||
|
||
expect( momentDate ).toBeNull(); | ||
} ); | ||
|
||
it( 'should return a Moment object representing now when given an undefined argument', () => { | ||
const momentDate = getMomentDate(); | ||
|
||
expect( moment.isMoment( momentDate ) ).toBe( true ); | ||
} ); | ||
} ); |
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,18 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import moment from 'moment'; | ||
|
||
/** | ||
* Create a Moment object from a date string. With no date supplied, default to a Moment | ||
* object representing now. If a null value is passed, return a null value. | ||
* | ||
* @param {?string} date Date representing the currently selected date or null to signify no selection. | ||
* @return {?moment.Moment} Moment object for selected date or null. | ||
*/ | ||
export const getMomentDate = ( date ) => { | ||
if ( null === date ) { | ||
return null; | ||
} | ||
return date ? moment( date ) : moment(); | ||
}; |