-
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.
Datepicker: Allow null value for currentDate on mounting
- Loading branch information
Showing
4 changed files
with
135 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
import moment from 'moment'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import DatePicker from '../date'; | ||
|
||
const TIMEZONELESS_FORMAT = 'YYYY-MM-DDTHH:mm:ss'; | ||
|
||
describe( 'DatePicker', () => { | ||
it( 'should pass down a moment object for currentDate', () => { | ||
const currentDate = '1986-10-18T23:00:00'; | ||
const wrapper = shallow( <DatePicker currentDate={ currentDate } /> ); | ||
const date = wrapper.children().props().date; | ||
expect( moment.isMoment( date ) ).toBe( true ); | ||
expect( date.isSame( moment( currentDate ) ) ).toBe( true ); | ||
} ); | ||
|
||
it( 'should pass down a null date when currentDate is set to null', () => { | ||
const wrapper = shallow( <DatePicker currentDate={ null } /> ); | ||
expect( wrapper.children().props().date ).toBeNull(); | ||
} ); | ||
|
||
it( 'should pass down a moment object for now when currentDate is undefined', () => { | ||
const wrapper = shallow( <DatePicker /> ); | ||
const date = wrapper.children().props().date; | ||
expect( moment.isMoment( date ) ).toBe( true ); | ||
expect( date.isSame( moment(), 'second' ) ).toBe( true ); | ||
} ); | ||
|
||
describe( 'getMomentDate', () => { | ||
it( 'should return a Moment object representing a given date string', () => { | ||
const currentDate = '1986-10-18T23:00:00'; | ||
const wrapper = shallow( <DatePicker /> ); | ||
const momentDate = wrapper.instance().getMomentDate( currentDate ); | ||
|
||
expect( moment.isMoment( momentDate ) ).toBe( true ); | ||
expect( momentDate.isSame( moment( currentDate ) ) ).toBe( true ); | ||
} ); | ||
|
||
it( 'should return null when given a null agrument', () => { | ||
const currentDate = null; | ||
const wrapper = shallow( <DatePicker /> ); | ||
const momentDate = wrapper.instance().getMomentDate( currentDate ); | ||
|
||
expect( momentDate ).toBeNull(); | ||
} ); | ||
|
||
it( 'should return a Moment object representing now when given an undefined argument', () => { | ||
const wrapper = shallow( <DatePicker /> ); | ||
const momentDate = wrapper.instance().getMomentDate(); | ||
|
||
expect( moment.isMoment( momentDate ) ).toBe( true ); | ||
expect( momentDate.isSame( moment(), 'second' ) ).toBe( true ); | ||
} ); | ||
} ); | ||
|
||
describe( 'onChangeMoment', () => { | ||
it( 'should call onChange with a formated date of the input', () => { | ||
const onChangeSpy = jest.fn(); | ||
const currentDate = '1986-10-18T11:00:00'; | ||
const wrapper = shallow( <DatePicker currentDate={ currentDate } onChange={ onChangeSpy } /> ); | ||
const newDate = moment(); | ||
|
||
wrapper.instance().onChangeMoment( newDate ); | ||
|
||
expect( onChangeSpy ).toHaveBeenCalledWith( newDate.format( TIMEZONELESS_FORMAT ) ); | ||
} ); | ||
|
||
it( 'should call onChange with hours, minutes, seconds of the current time when currentDate is undefined', () => { | ||
const onChangeSpy = jest.fn(); | ||
const wrapper = shallow( <DatePicker onChange={ onChangeSpy } /> ); | ||
const newDate = moment( '1986-10-18T11:00:00' ); | ||
const current = moment(); | ||
wrapper.instance().onChangeMoment( newDate ); | ||
|
||
expect( onChangeSpy ).toHaveBeenCalledWith( | ||
newDate | ||
.clone() | ||
.set( { | ||
hours: current.hours(), | ||
minutes: current.minutes(), | ||
seconds: current.seconds(), | ||
} ) | ||
.format( TIMEZONELESS_FORMAT ) | ||
); | ||
} ); | ||
|
||
it( 'should call onChange with hours, minutes, seconds of the current time when currentDate is null', () => { | ||
const onChangeSpy = jest.fn(); | ||
const wrapper = shallow( | ||
<DatePicker currentDate={ null } onChange={ onChangeSpy } /> | ||
); | ||
const newDate = moment( '1986-10-18T11:00:00' ); | ||
const current = moment(); | ||
wrapper.instance().onChangeMoment( newDate ); | ||
|
||
expect( onChangeSpy ).toHaveBeenCalledWith( | ||
newDate | ||
.clone() | ||
.set( { | ||
hours: current.hours(), | ||
minutes: current.minutes(), | ||
seconds: current.seconds(), | ||
} ) | ||
.format( TIMEZONELESS_FORMAT ) | ||
); | ||
} ); | ||
} ); | ||
} ); |